我有这个IntentService
可以将多个文件上传到服务器:
@Override
protected void onHandleIntent(Intent intent) {
//String urlToDownload = intent.getStringExtra("url");
ArrayList<Uri> uris = (ArrayList<Uri>) intent.getSerializableExtra("uris");
ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
String ur = intent.getStringExtra("urlheader");
chiave = intent.getStringExtra("key");
String urlUpload = ur + chiave;
File sourceFile = null;
FileInputStream fileInputStream = null;
URL url = null;
HttpURLConnection connection = null;
DataOutputStream dos = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
for (Uri u : uris) {
try {
url = new URL(urlUpload);
connection = (HttpURLConnection) url.openConnection();
//connection.connect();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
//connection.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(connection.getOutputStream());
Log.d("uploadFile", "uri: " + u);
sourceFile = new File(getRealPathFromURI(u));
fileInputStream = new FileInputStream(sourceFile);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file[]\"; filename=\""
+ getName(getContentResolver(), u) + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = connection.getResponseCode();
final String serverResponseMessage = connection.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
Bundle resultData = new Bundle();
resultData.putInt("progress", 1);
receiver.send(UPDATE_PROGRESS, resultData);
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (IOException e) {
Log.e(TAG, "Errore io: " + e);
}
}
}
它可以工作,但是如您所见,它为每个文件创建了一个新连接。我想对所有文件使用单个连接。
为此,我只需要输入以下几行:
url = new URL(urlUpload);
connection = (HttpURLConnection) url.openConnection();
//connection.connect();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
//connection.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(connection.getOutputStream());
在for
循环之前,但是它将仅上传一个文件,此后不执行任何操作。为什么?我可以更改以仅通过一个连接进行多次上传吗?
请注意,使用更多文件时,日志不会打印HTTP响应。例如,具有两个以上文件的日志为:
D/uploadFile: uri: content://media/external/images/media/178689
I/uploadFile: HTTP Response is : OK: 200
D/uploadFile: uri: content://media/external/audio/media/25829