Android-HttpURLConnection不要上传多个文件

时间:2018-09-19 23:14:09

标签: java android httpurlconnection

我有这个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

0 个答案:

没有答案