gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:07:41 24 4
gpt4 key购买 nike

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

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

我想要的是在进度条上显示已上传字节数组的字节数。现在,问题是,在上传过程中,我如何才能知道上传了多少字节。在想法/示例/代码问题上需要帮助。提前谢谢大家。

最佳答案

终于,我做到了。我在 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);

}
}

关于android - 如何在从字节数组上传数据到 PHP 服务器时获取上传进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8133116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com