【问题标题】:Bystes expected and received does not match sending POST from Java预期和接收到的字节数与从 Java 发送 POST 不匹配
【发布时间】:2013-10-29 20:41:07
【问题描述】:

为什么当我通过邮寄方式将文件从 java 上传到服务器时,我使用以下代码。

我已经编辑并添加了函数的其余顶部代码

     public int uploadFile(String sourceFileUri) {


                  String fileName = sourceFileUri;

                  HttpURLConnection conn = null;
                  DataOutputStream dos = null;  
                  String lineEnd = "\r\n";
                  String twoHyphens = "--";
                  String boundary = "*****";
                  int bytesRead, bytesAvailable, bufferSize;
                  byte[] buffer;
                  int maxBufferSize = 1 * 1024 * 1024; 
                  File sourceFile = new File(sourceFileUri); 

                  if (!sourceFile.isFile()) {

                       runOnUiThread(new Runnable() {
                           public void run() {
                            /*   messageText.setText("Source File not exist :"
                                       +uploadFilePath + "" + uploadFileName);*/
                           }
                       }); 

                       return 0;

                  }
                  else
                  {
                       try { 

              FileInputStream fileInputStream = new FileInputStream(sourceFile);
                           URL url = new URL(upLoadServerUri);
                           int total = (int) sourceFile.length();
                           // Open a HTTP  connection to  the URL
                           conn = (HttpURLConnection) url.openConnection();
                           conn.setFixedLengthStreamingMode(total);
                           conn.setDoInput(true); // Allow Inputs
                           conn.setDoOutput(true); // Allow Outputs
                           conn.setUseCaches(false); // Don't use a Cached Copy
                           conn.setRequestMethod("POST");
                           conn.setRequestProperty("Connection", "Keep-Alive");
                           conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                           conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                           conn.setRequestProperty("uploaded_file", fileName); 
                           conn.setRequestProperty("mail", MAIL); 
                           conn.setRequestProperty("OS", "1");
                           conn.setRequestProperty("LANG", "ES");

  dos = new DataOutputStream(conn.getOutputStream());

                   dos.writeBytes(twoHyphens + boundary + lineEnd); 
                   dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                             + fileName + "\"" + 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);
                   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                   // Responses from the server (code and message)
                   serverResponseCode = conn.getResponseCode();

预期和发送的字节不匹配?我不明白为什么会这样。

Exception : expected 589715 bytes but received 589840
java.io.IOException: expected 589715 bytes but received 589840
at libcore.net.http.FixedLengthOutputStream.write(FixedLengthOutputStream.java:39)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at com.androidexample.uploadtoserver.UploadToServer.uploadFile(UploadToServer.java:152)
at com.androidexample.uploadtoserver.UploadToServer$1.run(UploadToServer.java:62)
at java.lang.Thread.run(Thread.java:856)

【问题讨论】:

    标签: java android


    【解决方案1】:

    您不会显示将内容写入流的位置。我怀疑您需要考虑边界字节长度以及文件长度。仅供参考:您不必强制转换为 int。有HttpURLConnection.setFixedLengthStreamingMode(long) 方法。您可能还想看看this other thread.

    编辑:根据例外情况,您偏离了 125 个字节(589840 - 589715)。检查 2x 边界长度(这将是来自结果字符串 twoHyphens + boundary + lineEnd 的字节数)加上 "Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + fileName + "\"" + lineEnd 字节是否为 125 字节。如果是,那么这就是您必须考虑的额外内容。

    【讨论】:

    • 仍然看不到您正在写入连接的位置。这才是最重要的。你可以说你要写 X 字节,但最终写 Y。conn.connect(); OutputStream out = conn.getOutputStream(); ... out.write(byteArary,0,bytesArray.length]; 等代码在哪里?
    • 抱歉我已经修好了
    • 我试过了,但我仍然遇到同样的问题 -> E/上传文件到服务器异常(4535):异常:预期 892819 字节但收到 892944 我的总数是 int total = (int) sourceFile.length( );.为什么它期望的比收到的少,我真的什么都不明白..
    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2011-07-11
    • 2020-03-15
    • 2010-11-13
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多