multipart/form-data 통신 시 송수신 소스코드
받는쪽 JSP
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.json.*" %>
<%
String savePath="C:/data"; //저장경로
int sizeLimit = 10 * 1024 * 1024 ; // 용량제한 10MB
try{
MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit,"euc-kr",new DefaultFileRenamePolicy());
String helloText= multi.getParameter("helloText");
String helloFile= multi.getFilesystemName("helloFile");
String originFileName = multi.getOriginalFileName("helloFile");
String mimeType = multi.getContentType("helloFile");
//로직 처리
out.print("통신 시 결과값");
} catch(Exception e) {
out.print(e.getMessage());
}
%>
보내는쪽 (JSP version)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<Form action="받을쪽.jsp" Method="POST" enctype="multipart/form-data">
<input type="text" name="helloText" value=""/>
<input type="file" name="helloFile"/>
<input type="file" name="helloFile2"/>
<input type="submit" value="전 송"/>
</Form>
</body>
</html>
보내는쪽 (JAVA version)
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class MultipartTest {
private final static String boundary = "aJ123Af2318";
private final static String LINE_FEED = "\r\n";
private final static String charset = "euc-kr";
private OutputStream outputStream;
private PrintWriter writer;
public void addTextPart(String name, String value) throws IOException {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\""+name+"\"").append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
public void addFilePart(String name, File file) throws IOException {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\""+name+"\"; filename=\"" + file.getName() + "\"").append(LINE_FEED);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(file.getName())).append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
public void sendMultiPart(){
try{
File file = new File("경로");
File file2 = new File("경로");
URL url = new URL("받는쪽 url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "multipart/form-data;charset="+charset+";boundary=" + boundary);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(10000);
outputStream = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
//원하는 데이터 세팅
addTextPart("helloText","데이터값");
addFilePart("helloFile", file);
addFilePart("helloFile2", file2);
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
} catch (ConnectException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
'개발 > JAVA' 카테고리의 다른 글
jQuery 선택자 id, name, class 별 선택 및 다중 선택자 사용방법 (0) | 2020.06.15 |
---|---|
[Spring Mybatis] Mapped Statements collection does not contain value for ~~ 에러 이유 (0) | 2018.05.30 |