简体   繁体   English

使用DataOutputStream上传文件不会显示实际进度

[英]uploading a file with DataOutputStream doesn't display real progress

I am uploading a file from android to my webserver using the DataOutputStream with the following code: 我正在使用DataOutputStream通过以下代码将文件从android上传到我的网络服务器:

public class SnapshotUploadTask extends AsyncTask<Object, Integer, Void> {

    private final File file;
    private final ProgressDialog progressDialog;
    private final Activity activity;

    public SnapshotUploadTask(File file, ProgressDialog progressDialog, Activity activity) {
        this.progressDialog = progressDialog;
        this.file = file;
        this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        progressDialog.setProgress(0);
    }

    @Override
    protected Void doInBackground(Object... arg0) {

        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        int bytesRead;
        int bytesAvailable;
        int bufferSize;

        byte[] buffer;

        int maxBufferSize = 1 * 512;

        try {

            FileInputStream fileInputStream = new FileInputStream(file);
            URL url = new URL("http://bla.bla.bla/upload");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("ENCTYPE", "multipart/form-data");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("uploaded_file", file.getName());

            DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());

            dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
            dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + file.getName() + "\"" + lineEnd);

            dataOutputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            final int hundredPercent = bytesAvailable;
            progressDialog.setMax(hundredPercent);

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

            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                dataOutputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                final int restBytes = bytesAvailable;
                final int uploadedBytes = hundredPercent - restBytes;

                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setProgress((int) uploadedBytes);
                        if (restBytes <= 0) {
                            progressDialog.setMessage(activity.getString(R.string.camera_uploading_done));
                        }
                    }
                });
             }

            dataOutputStream.writeBytes(lineEnd);
            dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            if (serverResponseCode == 200) {
                progressDialog.dismiss();
            }

            fileInputStream.close();
            dataOutputStream.flush();
            dataOutputStream.close();

        } catch (Exception e) {
            progressDialog.dismiss();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
}

Now what happens is that i update my progressDialog as soon as i have written a certain amount of bytes into the OutputStream which leads to the progressDialog reaching 100% before the data is actually received by the server. 现在发生的事情是,一旦我在OutputStream中写入了一定数量的字节,便立即更新我的progressDialog,这导致ProgressDialog在服务器实际接收到数据之前达到100%。

I only know when the transmission is complete when i get to connection.getResponseCode(); 我仅在到达connection.getResponseCode();时才知道传输完成的时间connection.getResponseCode();

What i want instead is to update the progress when the data chunks are actually received by the server. 我想要的是在服务器实际接收到数据块时更新进度。 Is there a way to do that? 有没有办法做到这一点?

You need to be aware that HttpURLConnection buffers all the output unless you set fixed-length or chunked transfer mode, which you should certainly do instead of implementing it yourself. 您需要知道HttpURLConnection缓冲所有输出,除非您设置固定长度或分块传输模式,您一定要这样做,而不是自己实现它。 It does that so it can set the Content-Length header accurately. 这样可以正确设置Content-Length标头。 If you use one of these transfer modes there is no buffering. 如果您使用这些传输模式之一,则没有缓冲。

NB You're misusing available() : see the Javadoc; 注意:您正在滥用available() :请参阅Javadoc; and you're writing junk in most cases: 在大多数情况下,您正在编写垃圾邮件:

dataOutputStream.write(buffer, 0, bufferSize);

should be 应该

dataOutputStream.write(buffer, 0, bytesRead);

You don't need all that fiddling around with available() inside the read loop either. 您也不需要在读取循环内摆弄available() Something like this is sufficient: 这样的事情就足够了:

long total = 0;
int maxBufferSize = 8192; // at least. 512 is far too small.
while ((bytesRead = fileInputStream.read(buffer, 0, bufferSize)) > 0) {
    dataOutputStream.write(buffer, 0, bytesRead);
    total += bytesRead;
    final long uploadedBytes = total;

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.setProgress(uploadedBytes);
                }
        });
    }
}
// at this point we are at end of stream
activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
progressDialog.setMessage(activity.getString(R.string.camera_uploading_done));
    }
}

E&OE E&OE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM