如何获取上传进度虽然上载数据从字节数组PHP服务器?数组、字节、进度、上传

2023-09-05 04:34:40 作者:爱已尽,情已断

我是从设备到PHP服务器的文件转换成字节数组上载的图像文件。我使用的是Android的异步HTTP-1.3.1.jar我的应用程序文件上传到服务器。

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/

我要的是要显示在进度条怎么字节数组的多字节已上载。现在的问题是,我怎样才能知道有多少字节已经上传而上传的进度。需要在这个问题上的想法/例子/ codeS帮助。在此先感谢你们。

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.

推荐答案

最后,我已经做到了。我已经做到了这一点使用文件输入/输出流的AsyncTask类中。下面,我已经给了code上传文件和放大器;显示,在进度条......

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

    }
}
 
精彩推荐
图片推荐