WCF を実行しているサーバーに画像ファイルを送信するためのこの Android コードがあります。しかし、内部サーバー エラー (コード 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();
}
}
ここで何が問題なのですか?