Uploading MS Word files from Android to .Net WCF?

前端 未结 1 432
迷失自我
迷失自我 2020-12-30 12:42

I have problem in uploading .doc file to .Net WCF from my Android app. I am able to send file but it is not supported on WCF end. Here is my method for uploading:

         


        
相关标签:
1条回答
  • 2020-12-30 13:18
    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);
                        }
    
    
            }
    

    On the .net end create a wcf method which receives stream. Thanks.

    0 讨论(0)
提交回复
热议问题