Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何从字节数组上传数据到PHP服务器上传进度?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过将文件转换为字节数组,将图像文件从设备上传PHP服务器.我在我的应用程序中使用android-async-http-1.3.1.jar将文件上传到服务器.

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

我想要的是在进度条上显示已上载字节数组的字节数.现在,问题是,如何在上传进行过程中了解已上传的字节数.需要有关想法/示例/代码问题的帮助.先谢谢你们.

解决方法

最后,我做到了.我在AsyncTask类中使用file-input / output-stream完成了这个.下面,我已经给出了上传文件代码代码.显示正在进行中……

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

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

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

                publishProgress();

                bufferSize = Math.min(bytesAvailable,maxBufferSizE);
                bytesRead = fileInputStream.read(buffer,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);

    }
}

大佬总结

以上是大佬教程为你收集整理的android – 如何从字节数组上传数据到PHP服务器上传进度?全部内容,希望文章能够帮你解决android – 如何从字节数组上传数据到PHP服务器上传进度?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。