developer tip

Java를 사용하여 멀티 파트 / 양식 데이터 POST 요청을하려면 어떻게해야합니까?

copycodes 2020. 9. 11. 08:12
반응형

Java를 사용하여 멀티 파트 / 양식 데이터 POST 요청을하려면 어떻게해야합니까?


Apache Commons HttpClient 3.x 버전에서는 multipart / form-data POST 요청을 만들 수있었습니다 ( 2004 년의 예 ). 불행히도 이것은 HttpClient 버전 4.0 에서는 더 이상 가능하지 않습니다 .

핵심 활동 "HTTP"의 경우 multipart는 다소 범위를 벗어납니다. 우리는 범위 내에있는 다른 프로젝트에서 유지 관리하는 멀티 파트 코드를 사용하고 싶지만 어떤 것도 알지 못합니다. 우리는 몇 년 전에 멀티 파트 코드를 commons-codec으로 옮기려고했지만 거기서 벗어나지 않았습니다. Oleg는 최근 멀티 파트 파싱 코드가 있고 멀티 파트 포맷 코드에 관심이있을 수있는 또 다른 프로젝트를 언급했습니다. 나는 그것에 대한 현재 상태를 모른다. ( http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html )

multipart / form-data POST 요청을 할 수있는 HTTP 클라이언트를 작성할 수있는 Java 라이브러리를 아는 사람이 있습니까?

배경 : Zoho WriterRemote API 를 사용하고 싶습니다 .


HttpClient 4.x를 사용하여 다중 파일 게시를 만듭니다.

업데이트 : HttpClient 4.3 부터 일부 클래스가 더 이상 사용되지 않습니다. 다음은 새 API가있는 코드입니다.

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("...");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);

// This attaches the file to the POST:
File f = new File("[/path/to/upload]");
builder.addBinaryBody(
    "file",
    new FileInputStream(f),
    ContentType.APPLICATION_OCTET_STREAM,
    f.getName()
);

HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();

다음은 더 이상 사용되지 않는 HttpClient 4.0 API 가있는 원래 코드 스 니펫입니다 .

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

FileBody bin = new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

이것들은 내가 가진 Maven 종속성입니다.

자바 코드 :

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

FileBody uploadFilePart = new FileBody(uploadFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
httpPost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httpPost);

pom.xml의 Maven 종속성 :

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.0.1</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.0.1</version>
  <scope>compile</scope>
</dependency>

JAR의 크기가 중요한 경우 (예 : 애플릿의 경우), HttpClient 대신 java.net.HttpURLConnection과 함께 httpmime를 직접 사용할 수도 있습니다.

httpclient-4.2.4:      423KB
httpmime-4.2.4:         26KB
httpcore-4.2.4:        222KB
commons-codec-1.6:     228KB
commons-logging-1.1.1:  60KB
Sum:                   959KB

httpmime-4.2.4:         26KB
httpcore-4.2.4:        222KB
Sum:                   248KB

암호:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

FileBody fileBody = new FileBody(new File(fileName));
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
multipartEntity.addPart("file", fileBody);

connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
OutputStream out = connection.getOutputStream();
try {
    multipartEntity.writeTo(out);
} finally {
    out.close();
}
int status = connection.getResponseCode();
...

pom.xml의 종속성 :

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2.4</version>
</dependency>

이 코드를 사용하여 post in multipart를 사용하여 이미지 또는 기타 파일을 서버에 업로드합니다.

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class SimplePostRequestTest {

    public static void main(String[] args) throws UnsupportedEncodingException, IOException {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.0.102/uploadtest/upload_photo");

        try {
            FileBody bin = new FileBody(new File("/home/ubuntu/cd.png"));
            StringBody id = new StringBody("3");
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("upload_image", bin);
            reqEntity.addPart("id", id);
            reqEntity.addPart("image_title", new StringBody("CoolPic"));

            httppost.setEntity(reqEntity);
            System.out.println("Requesting : " + httppost.getRequestLine());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost, responseHandler);
            System.out.println("responseBody : " + responseBody);

        } catch (ClientProtocolException e) {

        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }

}

업로드하려면 아래 파일이 필요합니다.

라이브러리는 다음 httpclient-4.1.2.jar, httpcore-4.1.2.jar, httpmime-4.1.2.jar, httpclient-cache-4.1.2.jar, commons-codec.jarcommons-logging-1.1.1.jar클래스 경로에있을 수 있습니다.


HTTP 클라이언트를 기반으로 하는 REST Assured사용할 수도 있습니다 . 매우 간단합니다.

given().multiPart(new File("/somedir/file.bin")).when().post("/fileUpload");

httpcomponents-client-4.0.1 worked for me. However, I had to add the external jar apache-mime4j-0.6.jar (org.apache.james.mime4j) otherwise reqEntity.addPart("bin", bin); would not compile. Now it's working like charm.


I found this sample in Apache's Quickstart Guide. It's for version 4.5:

/**
 * Example how to use multipart/form encoded POST request.
 */
public class ClientMultipartFormPost {

    public static void main(String[] args) throws Exception {
        if (args.length != 1)  {
            System.out.println("File path not given");
            System.exit(1);
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080" +
                    "/servlets-examples/servlet/RequestInfoExample");

            FileBody bin = new FileBody(new File(args[0]));
            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("bin", bin)
                    .addPart("comment", comment)
                    .build();


            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

Here's a solution that does not require any libraries.

This routine transmits every file in the directory d:/data/mpf10 to 'urlToConnect'

String boundary = Long.toHexString(System.currentTimeMillis());


URLConnection connection = new URL(urlToConnect).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
     writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));


     File dir = new File("d:/data/mpf10");

     for (File file : dir.listFiles()) {
           if (file.isDirectory()) {
                continue;
           }

           writer.println("--" + boundary);
           writer.println("Content-Disposition: form-data; name=\"" + file.getName() + "\"; filename=\"" + file.getName() + "\"");
           writer.println("Content-Type: text/plain; charset=UTF-8");
           writer.println();
           BufferedReader reader = null;
           try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                for (String line; (line = reader.readLine()) != null;) {
                      writer.println(line);
                }
           } finally {
                if (reader != null) {
                      reader.close();
                }
           }
     }

     writer.println("--" + boundary + "--");
} finally {
     if (writer != null)
           writer.close();
}

// Connection is lazily executed whenever you request any status.
int responseCode = ((HttpURLConnection) connection).getResponseCode();

We have a pure java implementation of multipart-form submit without using any external dependencies or libraries outside jdk. Refer https://github.com/atulsm/https-multipart-purejava/blob/master/src/main/java/com/atul/MultipartPure.java

private static String body = "{\"key1\":\"val1\", \"key2\":\"val2\"}";
private static String subdata1 = "@@ -2,3 +2,4 @@\r\n";
private static String subdata2 = "<data>subdata2</data>";

public static void main(String[] args) throws Exception{        
    String url = "https://" + ip + ":" + port + "/dataupload";
    String token = "Basic "+ Base64.getEncoder().encodeToString((userName+":"+password).getBytes());

    MultipartBuilder multipart = new MultipartBuilder(url,token);       
    multipart.addFormField("entity", "main", "application/json",body);
    multipart.addFormField("attachment", "subdata1", "application/octet-stream",subdata1);
    multipart.addFormField("attachment", "subdata2", "application/octet-stream",subdata2);        
    List<String> response = multipart.finish();         
    for (String line : response) {
        System.out.println(line);
    }
}

참고URL : https://stackoverflow.com/questions/1378920/how-can-i-make-a-multipart-form-data-post-request-using-java

반응형