Android – how to upload data from byte array to PHP server?
•
Android
I upload the image file from the device to the PHP server by converting the file into a byte array. I use android-async-http-1.3.1.jar in my application to upload the file to the server
http://loopj.com/android-async-http/
What I want is to display the number of bytes of the uploaded byte array on the progress bar. Now, the problem is how to understand the number of bytes uploaded during the upload process. I need help with ideas / examples / code problems. Thank you first
resolvent:
Finally, I did it. I used file input / output stream in the asynctask class to complete this. Below, I have given the code to upload the file and code. The display is in progress
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);
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码