응용프로그램(exe) 업로드 추가
parent
f87b12d1f3
commit
d9274cc609
@ -0,0 +1,114 @@
|
||||
package externalsystem.cleanparking.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.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 multipartPost(String sendFilePath, String sendUri) throws IOException {
|
||||
Path path = Path.of(sendFilePath);
|
||||
BigInteger randomNumber = new BigInteger(256, new Random());
|
||||
StringBuilder boundary = new StringBuilder().append(randomNumber);
|
||||
|
||||
|
||||
map.put("mfile", 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
|
||||
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>
|
||||
<form id="frmEdit--${pageName}" method="post" enctype="multipart/form-data">
|
||||
<div class="d-flex flex-row justify-content-evenly">
|
||||
|
||||
<div class="card" style="width:800px;">
|
||||
<h3>업로드</h3>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<label class="bg-lighter col-form-label w-px-150">파일</label>
|
||||
<input type="file" name="mfile" class="w-px-300" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="button" id="btnUploadAppl1" class="btn btn-primary w-px-100">업로드(local)</button>
|
||||
<button type="button" id="btnUploadAppl2" class="btn btn-primary w-px-100">업로드(dev)</button>
|
||||
<button type="button" id="btnUploadAppl3" class="btn btn-primary w-px-100">업로드(prod)</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
|
||||
|
||||
$("#btnUploadAppl1,#btnUploadAppl2,#btnUploadAppl3").on("click",function(){
|
||||
|
||||
|
||||
if($("#frmEdit--${pageName}").find("[name='mfile']").val() == ""){
|
||||
alert("첨부파일이 없습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
var formData = new FormData(document.getElementById("frmEdit--${pageName}"));
|
||||
|
||||
var callHost = "";
|
||||
if(this.id == "btnUploadAppl1"){
|
||||
formData.append("server","1");
|
||||
} else if(this.id == "btnUploadAppl2"){
|
||||
formData.append("server","2");
|
||||
} else if(this.id == "btnUploadAppl3"){
|
||||
formData.append("server","3");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
var options = {
|
||||
url : wctx.url("cleanparking/uploadAppl.do"),
|
||||
type : "POST",
|
||||
contentType : false, processData : false,
|
||||
data : formData,
|
||||
error : (xhr, options, error) => {
|
||||
console.log('에러');
|
||||
},
|
||||
complete : () => {
|
||||
|
||||
},
|
||||
success : (resp, msg, xhr) => {
|
||||
if(resp.saved){
|
||||
alert("성공!");
|
||||
} else {
|
||||
alert("실패!");
|
||||
}
|
||||
}
|
||||
};
|
||||
$.ajax(options);
|
||||
|
||||
|
||||
});
|
||||
</script>
|
||||
Loading…
Reference in New Issue