C#&;Java-将文件从Android上载到WCF
我正在尝试将一个文件从Android上传到WCF服务。 我已经到了触发WCF上的方法并保存文件的地步。 尝试打开文件时,我收到一条错误消息,说明文件已损坏C#&;Java-将文件从Android上载到WCF,java,c#,android,wcf,Java,C#,Android,Wcf,我正在尝试将一个文件从Android上传到WCF服务。 我已经到了触发WCF上的方法并保存文件的地步。 尝试打开文件时,我收到一条错误消息,说明文件已损坏 public Result UploadUserProfilePicture(Stream image) { try { var bytes = CommonMethods.ReadToEnd(image); FileType fileType = byt
public Result UploadUserProfilePicture(Stream image)
{
try
{
var bytes = CommonMethods.ReadToEnd(image);
FileType fileType = bytes.GetFileType();
Guid guid = Guid.NewGuid();
string imageName = guid.ToString() + "." + fileType.Extension;
var buf = new byte[1024];
var path = Path.Combine(@"C:\fileUpload\" + imageName); //" ocd
File.WriteAllBytes(path, bytes);
return new Result
{
Success = true,
Message = imageName
};
}
catch(Exception ex)
{
return new Result
{
Success = false,
Message = ex.ToString()
};
}
}
我认为错误可能在HttpFileUpload类中,其中一个静态变量不正确,或者我发送的不是流,而是服务以某种方式转换为流的其他内容,从而导致损坏
使用的代码可以在下面找到
Android代码:
HttpFileUpload类:
找到HttpFileUpload代码:
移动服务:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Result UploadUserProfilePicture(Stream image);
FileType FileType=bytes.GetFileType()
是一个返回扩展名的插件。当前插件不工作,因为文件类型始终为空。
但是,图像变量可能已损坏
public Result UploadUserProfilePicture(Stream image)
{
try
{
var bytes = CommonMethods.ReadToEnd(image);
FileType fileType = bytes.GetFileType();
Guid guid = Guid.NewGuid();
string imageName = guid.ToString() + "." + fileType.Extension;
var buf = new byte[1024];
var path = Path.Combine(@"C:\fileUpload\" + imageName); //" ocd
File.WriteAllBytes(path, bytes);
return new Result
{
Success = true,
Message = imageName
};
}
catch(Exception ex)
{
return new Result
{
Success = false,
Message = ex.ToString()
};
}
}
我从未在Android/WCF REST中尝试过这种组合,但在您的代码中我注意到了一些东西 首先,您在操作契约中说,您的REST方法需要JSON并返回JSON。但是Android代码并没有发送JSON。它只是按原样发送输入文件的内容。这是我认为不正确的一件事。如果您想像浏览器表单一样将内容上传到网站,那么您使用的代码是很好的,但这与使用REST请求不同 另一件事是:您的REST方法不会接受
Stream
参数,因为它不会获得流
我要做的第一件事是设计REST POST方法,使其接受如下JSON对象:
{ imageBytes = "0dac...." }
imageBytes
是要保存的图像的base64编码版本(例如:PNG文件base64编码的字节)。然后,您可以使用其他方法来测试这是否有效
然后我会更改Android代码,使其
那事情就应该解决了。正如我在上面所说的,我自己在这种组合中还没有这样做过,因此我无法提供任何示例代码。感谢Thorsten为我指明了正确的方向。 下面是一个编码示例: Android代码:
图像上载活动:
File initialFile = new File(imagePath);
byte[] bytes = FileUtils.readFileToByteArray(initialFile);
final String base64 = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT);
Thread uploadFileThread = new Thread(new Runnable() {
@Override
public void run() {
FileToUpload fileToUpload = new FileToUpload();
fileToUpload.setFile(base64);
String[] result = ServiceConnector.UploadUserProfilePicture(fileToUpload);
}
});
ServiceConnector类:
public static String[] UploadUserProfilePicture(FileToUpload fileToUpload) {
StringBuilder sb = new StringBuilder();
String result[] = {"false", "Something went wrong"};
HttpURLConnection urlConnection = null;
try {
URL url;
DataOutputStream printout;
DataInputStream input;
url = new URL(LOCATION + "/UploadUserProfilePicture");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Host", "x.x.x.x:xxxx");
urlConnection.setConnectTimeout(1*1000*3600);
urlConnection.setReadTimeout(1*1000*3600);
urlConnection.connect();
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
// Send POST output.
printout = new DataOutputStream(urlConnection.getOutputStream());
byte[] extraInfo = gson.toJson(fileToUpload).getBytes("UTF-8");
printout.write(extraInfo);
printout.flush();
printout.close();
int HttpResult = urlConnection.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
result = CommonMethods.parseJsonToArray(sb.toString());
} else {
System.out.println("*** RESPONSE MESSAGE ***");
System.out.println(urlConnection.getResponseMessage());
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return null;
}
C#服务代码:服务: iSeries设备:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Result UploadUserProfilePicture(FileToUpload image);
服务上的Web.Config:我得到了一个400错误的请求,没有下面的代码
<bindings>
<webHttpBinding>
<binding
name="binding"
openTimeout="00:10:00" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647" transferMode="Streamed">
<readerQuotas maxDepth="250000000" maxStringContentLength="250000000" maxArrayLength="250000000" maxBytesPerRead="250000000" maxNameTableCharCount="250000000"/>
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Result UploadUserProfilePicture(FileToUpload image);
<bindings>
<webHttpBinding>
<binding
name="binding"
openTimeout="00:10:00" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647" transferMode="Streamed">
<readerQuotas maxDepth="250000000" maxStringContentLength="250000000" maxArrayLength="250000000" maxBytesPerRead="250000000" maxNameTableCharCount="250000000"/>
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>