从字节数组上传数据到 PHP 服务器时如何获取上传进度?

发布于 2024-12-16 13:06:41 字数 294 浏览 0 评论 0原文

我通过将文件转换为字节数组来将图像文件从设备上传到 PHP 服务器。我正在我的应用程序中使用 android-async-http-1.3.1.jar 将文件上传到服务器。

http://loopj.com/android-async-http/

我想要的是展示在进度条上显示字节数组的字节数已上传。现在的问题是,在上传过程中我如何知道已上传了多少字节。需要有关想法/示例/代码问题的帮助。预先感谢各位。

I'm uploading image files from device to a PHP server by converting the file into byte array. I'm uploading files to server using android-async-http-1.3.1.jar in my app.

http://loopj.com/android-async-http/

What i want is to show on a progress bar how much bytes of the byte array has been uploaded. Now , the problem is , how can i get to know how much bytes has been uploaded while a uploading is on progress. Need help on the issue with ideas / example / codes. Thanks in advance guys.

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

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

发布评论

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

评论(1

生死何惧 2024-12-23 13:06:41

最后,我做到了。我已经使用 AsyncTask 类中的文件输入/输出流完成了此操作。下面,我给出了上传文件和文件的代码。显示进度条......

class ImageUploadTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        pb.setVisibility(View.VISIBLE);

    }


    @Override
    protected String doInBackground(Void... unused) {

        String twoHyphens = "--";
        String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
        String lineEnd = "\r\n";

        try
        {
            FileInputStream fileInputStream = new FileInputStream(new     File(pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",     "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream(     connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data;     name=\"image\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            Log.v("Size",bytesAvailable+"");

            pb.setProgress(0);
            pb.setMax(bytesAvailable);
            //Log.v("Max",pb.getMax()+"");

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


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

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);

                bytesAvailable = fileInputStream.available();
                Log.v("Available",bytesAvailable+"");

                publishProgress();

                bufferSize = Math.min(bytesAvailable,     maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0,      bufferSize);
            }

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

            // Responses from the server (code and message)
            serverResponseCode = connection.getResponseCode();
            serverResponseMessage = connection.getResponseMessage();
            System.out.println(serverResponseMessage);

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

        }
        catch (Exception ex)
        {
        //Exception handling
        }

        //publishProgress();

        return null;

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {
        super.onProgressUpdate(unsued);

        pb.setProgress(pb.getMax()-bytesAvailable);

    }

    @Override
    protected void onPostExecute(String sResponse) {

        //if(pb.getProgress()>= pb.getMax())
        pb.setVisibility(View.INVISIBLE);

    }
}

Finally , i have done it. I have done this using file-input/output-stream inside AsyncTask class. Below, i have given the code for uploading file & showing that in progress bar ......

class ImageUploadTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        pb.setVisibility(View.VISIBLE);

    }


    @Override
    protected String doInBackground(Void... unused) {

        String twoHyphens = "--";
        String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
        String lineEnd = "\r\n";

        try
        {
            FileInputStream fileInputStream = new FileInputStream(new     File(pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",     "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream(     connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data;     name=\"image\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            Log.v("Size",bytesAvailable+"");

            pb.setProgress(0);
            pb.setMax(bytesAvailable);
            //Log.v("Max",pb.getMax()+"");

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


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

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);

                bytesAvailable = fileInputStream.available();
                Log.v("Available",bytesAvailable+"");

                publishProgress();

                bufferSize = Math.min(bytesAvailable,     maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0,      bufferSize);
            }

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

            // Responses from the server (code and message)
            serverResponseCode = connection.getResponseCode();
            serverResponseMessage = connection.getResponseMessage();
            System.out.println(serverResponseMessage);

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

        }
        catch (Exception ex)
        {
        //Exception handling
        }

        //publishProgress();

        return null;

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {
        super.onProgressUpdate(unsued);

        pb.setProgress(pb.getMax()-bytesAvailable);

    }

    @Override
    protected void onPostExecute(String sResponse) {

        //if(pb.getProgress()>= pb.getMax())
        pb.setVisibility(View.INVISIBLE);

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