如何正确使用setFixedLengthStreamingMode(int)

时间:2013-10-29 21:50:40

标签: java android

我在使用此功能时遇到问题。我得到长度();我需要上传的文件,我得到下一个错误 - >

总是比预期更多的字节数

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)

我用它来获取文件的大小

int fixedLength = (int) fileInputStream.getChannel().size();
int total = (int) sourceFile.length();

是否可以正确发布一个有效的setFixedLengthStreamingMode(int)示例?我只能看到这个方法的问题

这里有完整的代码,是的,它可以传输一个文件或一些文件,每个可能只有15 Mb,有时我会在一些旧设备中出现内存不足。

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);
            int fixedLength = (int) fileInputStream.getChannel().size();
            URL url = new URL(upLoadServerUri);
            //int total = (int) sourceFile.length();
            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setFixedLengthStreamingMode(fixedLength);
            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");
            //conn.setChunkedStreamingMode(maxBufferSize);

            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();
            // String serverResponseMessage = conn.getResponseMessage();

            // Log.i("uploadFile", "HTTP Response is : " 
            //       + serverResponseMessage + ": " + serverResponseCode);

            if(serverResponseCode == 200) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        /*messageText.setText(msg);
                        Toast.makeText(UploadToServer.this, "File Upload Complete.", 
                                     Toast.LENGTH_SHORT).show();*/
                    }
                });
            }    

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您必须将其设置为您要传输的总字节数。很明显,你发送的不止这些。您尚未显示相关代码,因此无法进一步发表评论。

为什么要使用DataOutputStream?您不需要仅仅发送文件。

编辑:您的代码存在许多问题。

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

您不需要最大尺寸的缓冲区。一个8192字节的缓冲区就足够了。

bytesAvailable = fileInputStream.available(); 

InputStream.available() 在Javadoc 中具体描述如下: “使用此方法的返回值来分配用于保存的缓冲区永远不正确此流中的所有数据。“ 幸运的是,您可以删除此行。

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

不必要,见上文。只需使用new byte[8192]

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

这一切都是错的。这是一个误用available()的开始。在Java中复制流的标准方法如下:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

适用于任何大于零的缓冲区大小;它不关心输入多长时间;无论大小如何,它都可以正常工作。

// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

您再次没有将其视为固定长度传输大小的一部分。

但我必须说你最好使用分块传输模式并让HttpURLConnection完成所有繁重工作:你需要的就是上面的复制循环。