将 MS Word 文件从 Android 上传到 .Net WCF?

发布于 2024-12-11 11:50:22 字数 2349 浏览 0 评论 0原文

我在从 Android 应用程序将 .doc 文件上传到 .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";

}

请发送链接或代码或任何东西。

如果您对此不了解,请对这个问题进行排名。 谢谢

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:

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");
        }
    }

here is .net code:

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";

}

Please send links or code or any thing.

If you don't have idea about this please rank up this question..
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-12-18 11:50:22
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);
                    }


        }

在.net端创建一个接收流的wcf方法。
谢谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文