一个帖子请求中发送多个文件

一个帖子请求中发送多个文件

本文介绍了JAVA中HttpURLConnection中的一个帖子请求中发送多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 HttpURLConnection 通过 post 方法发送文件.我随文件一起发送了一个参数,它是student_id".在每个 post 请求中发送一个文件时,代码工作正常.但是,如何更新下面的代码以在一个帖子请求中发送多个文件,其中所有文件都属于同一个student_id"?

I am sending files via post method using HttpURLConnection. I am sending with the file a parameter which is 'student_id'. The code is working fine when sending one file in each post request. But, how can I update the code below to send multiple files in one post request where all the files belong to the same '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"中添加带有参数的multiple",但它不起作用

I tried to add 'multiple' with the parameter in 'newfile' but it is not working

        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 表单字段 发布 multipart/form-data 请求student_idname参数和多个文件部分上传文件.

Seems that you are trying to post a multipart/form-data request with 1 form field with name parameter of student_id and multiple file part to upload files.

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

You can send multiple filesby supplying each file in a separate part but all with the same name parameter.

例如,您可以通过发送带有 newfilename 参数的第一个 file part 来上传 textFile1 的文件代码>:

For example, you can upload a file of textFile1 by sending the first file part with name parameter of 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();

然后,您可以通过发送与newfile相同的name参数的文件部分来上传textFile2的另一个文件:

Then, you can upload another file of textFile2 by sending file part with same name parameter of 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();

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

As you can see, the code are almost the same except the file to upload. It is recommend to put the code into a method and call it to send each file part.

这篇关于JAVA中HttpURLConnection中的一个帖子请求中发送多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:54