Last active
October 7, 2018 18:18
-
-
Save jooyunghan/5053d305859d8e47f5a7839c19f906b7 to your computer and use it in GitHub Desktop.
Java Nested Multipart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jooyunghan; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.entity.mime.content.ContentBody; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
public class EntityBody implements ContentBody { | |
private final HttpEntity entity; | |
public EntityBody(HttpEntity entity) { | |
this.entity = entity; | |
} | |
@Override | |
public String getFilename() { | |
return null; | |
} | |
@Override | |
public void writeTo(OutputStream out) throws IOException { | |
entity.writeTo(out); | |
} | |
@Override | |
public String getMimeType() { | |
return entity.getContentType().toString(); | |
} | |
@Override | |
public String getMediaType() { | |
return null; | |
} | |
@Override | |
public String getSubType() { | |
return null; | |
} | |
@Override | |
public String getCharset() { | |
return null; | |
} | |
@Override | |
public String getTransferEncoding() { | |
return "binary"; | |
} | |
@Override | |
public long getContentLength() { | |
return entity.getContentLength(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jooyunghan; | |
import com.google.gson.Gson; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.entity.ContentType; | |
import org.apache.http.entity.mime.MultipartEntityBuilder; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.TreeMap; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
Gson gson = new Gson(); | |
Map<String, Object> meta = new TreeMap<>(); | |
meta.put("key", "value"); | |
meta.put("키", "값"); | |
meta.put("서브", Map.of("키", List.of(1, true))); | |
final String filename = "한글.png"; | |
Charset utf8 = Charset.forName("utf-8"); | |
HttpEntity body = MultipartEntityBuilder.create() | |
.setLaxMode() | |
.setCharset(utf8) | |
.addTextBody("filename", filename, ContentType.TEXT_PLAIN.withCharset(utf8)) | |
.addBinaryBody("file", new File(filename)) | |
.build(); | |
HttpEntity entity = MultipartEntityBuilder.create() | |
.addTextBody("header", gson.toJson(meta), ContentType.APPLICATION_JSON) | |
.addPart("body", new EntityBody(body)) | |
.build(); | |
System.out.println(entity.getContentType() + "\n"); | |
entity.writeTo(System.out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment