【发布时间】:2011-12-13 04:52:08
【问题描述】:
我在将 .doc 文件从我的 Android 应用程序上传到 .Net WCF 时遇到问题。我可以发送文件,但 WCF 端不支持它。 这是我的上传方法:
protected void checkinmethod(String rid) throws Exception {
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot, rid+".doc");
InputStream in = new FileInputStream(file);
byte[] bytearray=new byte[(int) file.length()];
int ab=0;
do
{
ab=in.read(bytearray, 0, bytearray.length);
} while(ab>0);
InputStream mystream= new ByteArrayInputStream(bytearray);
InputStreamEntity se=new InputStreamEntity(mystream, 10000);
HttpPost request = new HttpPost("http://10.66.52.247/tutorwcf/Service.svc/Service/updateMyDoc1");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/msword");
request.setEntity(se);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
statuss.setText(new String(buffer));
//
}
catch (Exception e) {
// TODO: handle exception
Log.e("hi", "exception is", e);
statuss.setText("exception");
}
}
这是.net代码:
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";
}
请发送链接或代码或任何东西。
如果您对此一无所知,请对这个问题进行排名.. 谢谢
【问题讨论】:
-
具体的问题是什么?运行代码时是否收到任何错误消息或异常?
-
先生,在 wcf 端,我正在获取一个 word 文件。当我尝试打开它时,它显示错误“读取错误”。但是当我在记事本或记事本中打开它时,它会显示如下符号:ÐÏࡱá X«Ç3aZ¢ÒÂà,°D0 j~è3ß¶Îbãí~i>ƒØÍ3¿`õ?ê/ç[ج¶ Géâ\Ä!ý-ÛRk.
-
你的问题解决了吗?
-
已解决但使用流式传输。但我希望它抛出字节数组
标签: android wcf post file-upload io