在JAVA中的HttpURLConnection中的一个帖子中发送多个文件

时间:2016-06-29 00:23:42

标签: java file post httpurlconnection

我使用HttpURLConnection通过post方法发送文件。我正在向文件发送一个参数,即' student_id'。在每个帖子请求中发送一个文件时,代码工作正常。 但是,如何更新以下代码,以便在一个帖子请求中发送多个文件,其中所有文件属于同一个' student_id'

       try{  
            File textFile2 = new File("info.txt");  

         URL url = new URL("htttp://wwww.students.com");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();         
         urlConnection.setChunkedStreamingMode(0);
         urlConnection.setDoOutput(true);   


        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

        urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);          
        OutputStream output = urlConnection.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);

        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"student_id\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF).append("25").append(CRLF).flush();


        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile2.getName() + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
        writer.append(CRLF).flush();                                                            
        Files.copy(textFile2.toPath(), output);//copies all bytes in  a file to the output stream 
        output.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush();

        writer.append("--" + boundary + "--").append(CRLF).flush();
        InputStream responseStream; 

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

我尝试添加多个'使用' newfile'中的参数但它无法正常工作

        writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"newfile\" multiple; filename=\"" + textFile2.getName() + "\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
    writer.append(CRLF).flush();                                                            
    Files.copy(textFile2.toPath(), output);//copies all bytes in  a file to the output stream 
    output.flush(); // Important before continuing with writer!
    writer.append(CRLF).flush();

    writer.append("--" + boundary + "--").append(CRLF).flush();

1 个答案:

答案 0 :(得分:0)

似乎您尝试使用multipart/form-data参数name和多个文件部分发布带有表单字段student_id个请求上传文件。

您可以通过在单独的部分中提供每个文件来发送多个文件,但所有文件都具有相同的name参数。

例如,您可以通过textFile1参数name发送第一个文件部分来上传newfile文件:

writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile1.getName()+ "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF);
writer.flush();

FileInputStream inputStream = new FileInputStream(textFile1);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();

writer.append(CRLF);
writer.flush();

然后,您可以通过发送带有textFile2 name newfile参数的文件部分来上传writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile2.getName()+ "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF); writer.flush(); FileInputStream inputStream = new FileInputStream(textFile2); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); inputStream.close(); writer.append(CRLF); writer.flush(); 的其他文件:

lck

如您所见,除了要上传的文件外,代码几乎相同。建议将代码放入方法并调用它来发送每个文件部分