我有这个android代码将图像文件发送到运行WCF的服务器。 但它返回内部服务器错误(代码500)。我所拥有的整个代码包含配置和其他...
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="60000" maxBufferSize="60000" transferMode="Streamed" />
</webHttpBinding>
</bindings>
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="AndroidWcfService.FileUploaderService">
<endpoint address="" binding="webHttpBinding" contract="AndroidWcfService.IFileUploaderService" />
</service>
</services>
这是接口代码
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UploadFile", //"/{pointId}/{ticket}",
BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
int UploadFile(Stream image);
实施
public int UploadFile(Stream image)
{
try
{
byte[] f = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = image.Read(f, 0, f.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
byte[] t = new byte[totalBytesRead];
Array.Copy(f, t, totalBytesRead);
var fileName = string.Format("{0}.png", DateTime.Now.Ticks);
fileName = Path.Combine(Environment.CurrentDirectory, fileName);
var s = File.Create(fileName);
s.Write(t, 0, totalBytesRead);
s.Flush();
s.Close();
return 1000;
}
catch (Exception ex)
{
Logger.WriteEntry("UploadFile err:" + ex.Message);
if (ex.InnerException != null) Logger.WriteEntry("UploadFile err inner exception:" +
ex.InnerException.Message);
}
return -1000;
}
和android代码是
public void uploadFile(File file) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://test.com:16024/FileUploaderService.svc/UploadFile");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
// Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();
// Add some values
jsonObj.put("filename", file.getName());
// Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
} catch (JSONException e) {
Log.v("App", e.getMessage());
} catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}
FileBody fileBody = new FileBody(file);// , "application/octet-stream");
reqEntity.addPart("image", fileBody);
try {
postRequest.setEntity(reqEntity);
// Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);
// Check the status code, in this case "created"
if (((HttpResponse) httpResp).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
Log.v("App", "Uploaded sucessfully!");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这里有什么问题?