7
我在從我的Android應用上傳.doc文件到.Net WCF時遇到了問題。我能夠發送文件,但它不支持WCF端。 這裏是我的上傳方法:從Android上傳MS Word文件到.Net 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";
}
請發送鏈接或代碼或任何東西。
如果你沒有想法關於這個請排列了這個問題.. 感謝
確切的問題是什麼?運行代碼時是否收到任何錯誤消息或異常? – Kangkan
先生,在wcf結束我得到一個word文件。當我嘗試打開它時,顯示錯誤「讀取錯誤」。但是當我在記事本或文字板中打開它時,它會顯示我這樣的符號:ÐÏࡱáX«Ç3aZ¢ÒÂà,°D0 j〜è3߶Îbãí〜i>ƒØÍ3¿\éêê/ç[ج ¶Géâ\ A!Y型烏爾克。 –
排列你的問題? – Krishnabhadra