当我将 doc 文件从 android 发送到 wcf 服务时,我在 wcf 端遇到问题。问题是在 wcf 端打开上传的文件时,doc 文件的内容是一些不可读的文本。下面是代码: wcf 代码
FileStream fileToupload = new FileStream("D:\\myfile.doc", FileMode.Create, FileAccess.Write);
byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = mystream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
return "success";
和安卓代码
package com.example.filedemo;
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import android.os.Environment;
public class HttpUpload {
public static int res ;
public void myUploadedfile(){
try {
HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httpost = new HttpPost("http://10.160.0.18:85/Service.svc/UploadImage?fn=abc.doc");
httpost.setHeader("Content-type","application/octet-stream");
MultipartEntity entity = new MultipartEntity();
entity.addPart("fileContents", new FileBody(new File(Environment.getExternalStorageDirectory(),"abc.doc")));
httpost.setEntity(entity);
HttpResponse response;
response = httpclient.execute(httpost);
System.out.println("Response : "+response.getStatusLine().getStatusCode());
res = response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
System.out.println("Error: "+ e.getMessage());
// TODO: handle exception
}
}
}
请尽快回复。
谢谢