2011-10-22 14 views
7

Mam problem z przesłaniem pliku .doc do .Net WCF z mojej aplikacji na Androida. Jestem w stanie wysłać plik, ale nie jest on obsługiwany na końcu WCF. Oto moja metoda przesyłania:Przesyłanie plików MS Word z Android do .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"); 
     } 
    } 

tutaj jest kod .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"; 

} 

Proszę wysłać linki lub kod ani niczego.

Jeśli nie masz pojęcia o tym Proszę uszeregować się to pytanie .. dzięki

+0

Jaki jest dokładny problem? Czy po uruchomieniu kodu pojawia się komunikat o błędzie lub wyjątek? – Kangkan

+0

Sir, na końcu wcf otrzymuję plik ze słowem. Kiedy próbuję go otworzyć, pokazuje mi błąd "błąd odczytu". ale kiedy otwieram go w notatniku lub notatniku, pokazuje mi takie symbole: ÐÏÐÏ¡ ± á X «Ç3aZ ¢ à, ° D0 j ~ è3߶Îbãí ~ i> ƒØÍ3¿ \' õ? ê/ç [ج ГGéâ \ Ę! Ý-ÛRk. –

+0

ranking swoje pytanie? – Krishnabhadra

Odpowiedz

3
public void checkinstream(String rid, String filename) throws IOException 
     { 
        URL url=null; 
       HttpURLConnection conn = null; 
        DataOutputStream dos = null; 
        DataInputStream inStream = null; 
        String existingFileName= null; 

        existingFileName= "/mnt/sdcard/"+rid+".doc"; 
        int bytesRead, bytesAvailable, bufferSize; 
        byte[] buffer; 
        int maxBufferSize = Integer.MAX_VALUE; 
        String responseFromServer = ""; 


       url = new URL("http://10.66.51.241/mywcf/Service.svc/Service/uploadMyDoc");    

        try 
        { 
        //------------------ CLIENT REQUEST 
        FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 

        // Open a HTTP connection to the URL 
        conn = (HttpURLConnection) url.openConnection(); 
        // Allow Inputs 
        conn.setDoInput(true); 
        // Allow Outputs 
        conn.setDoOutput(true); 
        // Don't use a cached copy. 
        conn.setUseCaches(false); 
        // Use a post method. 
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Connection", "Keep-Alive"); 
        conn.setRequestProperty("Content-Type", "application/stream"); 
        dos = new DataOutputStream(conn.getOutputStream()); 
        // dos.writeBytes(twoHyphens + boundary + lineEnd); 
        //dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); 
        // dos.writeBytes(lineEnd); 
        // create a buffer of maximum size 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 
        // read file and write it into form... 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
        while (bytesRead > 0) 
        { 
         dos.write(buffer, 0, bufferSize); 
         bytesAvailable = fileInputStream.available(); 
         bufferSize = Math.min(bytesAvailable, maxBufferSize); 
         bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        } 
        // send multipart form data necesssary after file data... 
        dos.writeBytes(lineEnd); 


        // close streams 
        Log.e("Debug",twoHyphens + boundary + twoHyphens + lineEnd); 
        fileInputStream.close(); 
        dos.flush(); 
        dos.close(); 
        } 
        catch (MalformedURLException ex) 
        { 
         Log.e("Debug", "error: " + ex.getMessage(), ex); 
        } 
        catch (IOException ioe) 
        { 
         Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
        } 
        //------------------ read the SERVER RESPONSE 
        try { 
          inStream = new DataInputStream (conn.getInputStream()); 
          String str; 

          while ((str = inStream.readLine()) != null) 
          { 
           Log.e("Debug","Server Response "+str); 
           statuss.setText(str); 
          } 
          inStream.close(); 

        } 
        catch (IOException ioex){ 
         Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
        } 


     } 

Na koniec .net wcf stworzyć metodę, która odbiera strumień. Dzięki.