2013-02-21 70 views
2

我在wcf端遇到問題,而我正在將doc文件從android發送到wcf服務。問題是,當在wcf端打開上傳的文件時,doc文件的內容是一些不可讀的文本。下面是代碼: WCF代碼將文件從android上傳到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"; 

Android的代碼

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 
     } 

    } 

} 

請回復很快。

感謝

回答

0

邏輯在WCF似乎不正確,請嘗試以下修改:

FileStream fileToupload = new FileStream("D:\\myfile.doc", FileMode.Create, FileAccess.Write); 

byte[] bytearray = new byte[10000]; 
int bytesRead, totalBytesRead = 0; 

while((bytesRead = mystream.Read(bytearray)) > 0) 
{  
    fileToupload.Write(bytearray, 0, bytesRead); // Write directly to the file 
    totalBytesRead += bytesRead; 
} 


fileToupload.Close(); 
fileToupload.Dispose(); 
+0

嘿感謝你的幫助....當我上傳DOC,XLS等它工作正常...但你能告訴我,如果我要上傳一個視頻或音頻文件.. ????大小將是無限的或高達3至4 GB? – 2013-02-21 10:03:09

+0

它應該是相同的,但您需要使用更大的緩衝區大小,並在連接失敗的情況下支持「上傳簡歷」。 – iTech 2013-02-21 21:31:09

+0

感謝您的回覆。但仍然存在的問題是,當我上傳的視頻文件上傳但未打開時,我嘗試打開所有播放器,例如媒體播放器,vlc播放器等,但未打開。等待回覆.....預先感謝 – 2013-02-22 05:53:41