no message
parent
85ca3af6ae
commit
8ea0e7d761
@ -0,0 +1,129 @@
|
|||||||
|
package cokr.xit.fims.nxrp.web;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class MultipartTest {
|
||||||
|
|
||||||
|
HttpClient httpClient;
|
||||||
|
|
||||||
|
HttpRequest httpRequest;
|
||||||
|
HttpResponse<String> httpResponse;
|
||||||
|
|
||||||
|
protected final Map<Object, Object> map = new HashMap<>();
|
||||||
|
private static final String LINEFEED = "\r\n";
|
||||||
|
private static final String DOUBLE_HYPHEN = "--";
|
||||||
|
private static final String QUTATE = "\"";
|
||||||
|
|
||||||
|
public void fileDown(String url, String downloadPath) throws IOException {
|
||||||
|
|
||||||
|
httpClient = HttpClient.newHttpClient();
|
||||||
|
httpRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.setHeader("Accept-Language", "ko")
|
||||||
|
.setHeader("Content-Type", "application/json")
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
HttpResponse<Path> getResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofFile(Paths.get(downloadPath)));
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void multipartPost(String sendFilePath, String fileSavePath, String sendUri) throws IOException {
|
||||||
|
Path path = Path.of(sendFilePath);
|
||||||
|
BigInteger randomNumber = new BigInteger(256, new Random());
|
||||||
|
StringBuilder boundary = new StringBuilder().append(randomNumber);
|
||||||
|
|
||||||
|
map.put("fileSavePath", fileSavePath);
|
||||||
|
map.put("file", path);
|
||||||
|
|
||||||
|
httpClient = HttpClient.newHttpClient();
|
||||||
|
multipartToByte(map, boundary.toString());
|
||||||
|
httpRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(sendUri))
|
||||||
|
.setHeader("Accept-Language", "ko")
|
||||||
|
.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary)
|
||||||
|
.POST(multipartToByte(map, boundary.toString()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequest.BodyPublisher multipartToByte (Map<Object, Object> map, String boundary) throws IOException {
|
||||||
|
List<byte[]> byteArrays = new ArrayList<>();
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for (Map.Entry<Object, Object> data : map.entrySet()) {
|
||||||
|
stringBuilder.setLength(0);
|
||||||
|
stringBuilder.append(DOUBLE_HYPHEN)
|
||||||
|
.append(boundary)
|
||||||
|
.append(LINEFEED);
|
||||||
|
|
||||||
|
if (data.getValue() instanceof Path) {
|
||||||
|
Path filePath = (Path)data.getValue();
|
||||||
|
String mimeType = Files.probeContentType(filePath);
|
||||||
|
byte[] fileByte = Files.readAllBytes(filePath);
|
||||||
|
|
||||||
|
stringBuilder.append("Content-Disposition: form-data; name=")
|
||||||
|
.append(QUTATE)
|
||||||
|
.append(data.getKey())
|
||||||
|
.append(QUTATE)
|
||||||
|
.append("; filename= ")
|
||||||
|
.append(QUTATE)
|
||||||
|
.append(data.getValue())
|
||||||
|
.append(QUTATE)
|
||||||
|
.append(LINEFEED)
|
||||||
|
.append("Content-Type: ")
|
||||||
|
.append(mimeType)
|
||||||
|
.append(LINEFEED)
|
||||||
|
.append(LINEFEED);
|
||||||
|
|
||||||
|
byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
byteArrays.add(fileByte);
|
||||||
|
byteArrays.add(LINEFEED.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} else {
|
||||||
|
stringBuilder.append("Content-Disposition: form-data; name=")
|
||||||
|
.append(QUTATE)
|
||||||
|
.append(data.getKey())
|
||||||
|
.append(QUTATE)
|
||||||
|
.append(";")
|
||||||
|
.append(LINEFEED)
|
||||||
|
.append(LINEFEED)
|
||||||
|
.append(data.getValue())
|
||||||
|
.append(LINEFEED);
|
||||||
|
byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stringBuilder.setLength(0);
|
||||||
|
stringBuilder.append(DOUBLE_HYPHEN)
|
||||||
|
.append(boundary)
|
||||||
|
.append(DOUBLE_HYPHEN);
|
||||||
|
byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
return HttpRequest.BodyPublishers.ofByteArrays(byteArrays);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue