index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<form action="upload.file" method="post" enctype="multipart/form-data">
<input type=file name="file">
<input type=submit value="업로드">
</form>
</body>
</html>
request는 가방
텍스트부분과 파일 부분을 구분 하기 위해 파트를 나눈다 : multipart
서버 측에서 꺼내는 방법
라이브러리 필요
Servlets.com | com.oreilly.servlet
servlets.com
cos.jar : 세미프로젝트 단계에서 적당
fileController.java
package kh.backend.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
@WebServlet(".file")
public class fileController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
String ctxPath = request.getContextPath();
String cmd = requestURI.substring(ctxPath.length());
String uploadPath = request.getServletContext().getRealPath("upload");
System.out.println("저장 경로 : "+ uploadPath );
// if(cmd.contentEquals("/upload.file")) {
// MutipartRequest multi = new MultipartRequest(
// request,
// uploadPath
//
// );
// }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
수정한 내용/ 업로드한 이미지가 바로 적용되지 않는 경우
프로젝트 경로에 있어도 실행시 복사가 안되면 실행되지 않음
RealPath는 실행되는 진짜 위치를 말한다
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<form action="upload.file" method="post" enctype="multipart/form-data">
<input type=file name="file">
<input type=submit value="업로드">
</form>
</body>
</html>
package kh.backend.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
@WebServlet("*.file")
public class fileController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
String ctxPath = request.getContextPath();
String cmd = requestURI.substring(ctxPath.length());
String uploadPath = request.getServletContext().getRealPath("upload");
System.out.println("저장 경로 : "+ uploadPath );
// if(cmd.contentEquals("/upload.file")) {
// MutipartRequest multi = new MultipartRequest(
// request,
// uploadPath
//
// );
// }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
upload라는 파일 없음
String uploadPath = request.getServletContext().getRealPath("upload");
if(!uploadPathF.exists()) { //경로가 없다면
uploadPathF.mkdir(); //만들어라
}
package kh.backend.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@WebServlet("*.file")
public class fileController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
String ctxPath = request.getContextPath();
String cmd = requestURI.substring(ctxPath.length());
String uploadPath = request.getServletContext().getRealPath("upload");
File uploadPathF = new File(uploadPath);
if(!uploadPathF.exists()) { //경로가 없다면
uploadPathF.mkdir(); //만들어라
}
System.out.println("저장 경로 : "+ uploadPath );
if(cmd.contentEquals("/upload.file")) {
MultipartRequest multi = new MultipartRequest(
request,
uploadPath,
1024*1024*10, //사이즈
"utf8", //인코딩
new DefaultFileRenamePolicy() // 이름 겹치면 덮어씌워짐 -> 중복 방지 필요 : 처리코드를 직접 쓸수도 있지만, 디폴트값으로
);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
'디지털 컨버전스 > Project' 카테고리의 다른 글
[LawView] Oracle - profile (0) | 2020.05.14 |
---|---|
[Project] 첨부파일 목록 (0) | 2020.05.13 |
[Project] url 줄이기 (0) | 2020.05.13 |
[Project] include / 상대 경로 / 절대 경로 (1) | 2020.05.13 |
[세미 프로젝트] Font-Awesome (0) | 2020.05.12 |