Spring MVC | POST 입력 방법 | textbox 값 입력 | radio 값 입력 | checkbox 값 입력 | combobox 값 입력 | 필터
POST 입력을 위한 Admin 컨트롤러 추가 NoticeController.java package com.newlecture.web.controller.admin.board; import org.springframework.stereotype.Controller; import org.springframework.web.b..
kjh95.tistory.com
파일 업로드
파일 전송과 멀티파트 라이브러리
application/x-www-form-urlencoded
url이 가지고 있는 QueryString을 사용하는 방식으로 인코딩해서 서버에 전달 -> 하지만 바이너리(이진법)가 포함시키기 어렵다.
multipart/form-data
url방식이 아니라 키와 값을 part로 나눈다.
파일은 바이너리
value = 300*1024*1024 (300mb)
servlet-context.xml
클래스 문서
CommonsMultipartResolver (Spring Framework 5.3.4 API)
Servlet-based MultipartResolver implementation for Apache Commons FileUpload 1.2 or above. Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as bean properties (inherited from CommonsFileUploadSupport). See corresponding ServletFil
docs.spring.io
인코딩 타입 설정
reg1.html
파일을 받기 위한 작업
reg1.html에서 name확인
NoticeController.java
maven으로 파일 업로드와 관련된 라이브러리 추가하기
commons-fileupload 1.4
다운로드. jfif라는 이름의 파일을 선택해주었다.
사이즈가 정상적으로 출력되었음을 확인
업로드 파일 원하는 위치에 저장하기
물리 경로 얻기
/home의 경로를 알아야 한다.
ServletContext 사용하기
getRealPath 홈디렉토리 기반으로 해서 경로에 있을 경우 실제로 물리적 위치를 알아내는 도구
ServletContext 얻기
@Autowired
ServletContext sc;
@Autowired
private HttpServletRequest request;
절대 경로 얻기
String realPath=sc.getRealPath("/upload");
String realPath=request.getServletContext().getRealPath("/upload");
업로드 폴더 생성
Spring으로 ServletContext 바인딩하기
NoticeController.java
File.separator 현재의 시스템에 맞게 구분자를 만들어준다.
NoticeController.java
package com.newlecture.web.controller.admin.board;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller("adminNoticeController")
@RequestMapping("/admin/board/notice/")
public class NoticeController {
@Autowired
private ServletContext ctx;
@RequestMapping("list")
public String list() {
return "";
}
@RequestMapping("reg")
@ResponseBody
public String reg(String title, String content, MultipartFile file,
String category, String[] foods, String food, HttpServletRequest request) throws IllegalStateException, IOException {
long size = file.getSize();
String fileName = file.getOriginalFilename();
System.out.printf("fileName:%s, fileSize: %d\n", fileName, size);
//ServletContext cts = request.getServletContext();
String webPath = "/static/upload";
String realPath = ctx.getRealPath(webPath);
System.out.printf("realPath : %s\n",realPath);
File savePath = new File(realPath); //realPath경로에 파일업로드하기위한 폴더가 있는지 없는지 확인
if(!savePath.exists())
savePath.mkdirs();//사이에 있는 경로에 폴더가 없으면 폴더를 만들어줌
realPath += File.separator + fileName; // "//" 시스템에 맞는 구분자 출력됨
File saveFile = new File(realPath);
file.transferTo(saveFile); //저장시키기
System.out.println(category);
for(String f : foods)
System.out.println(f);
System.out.println(food);
return String.format("title:%s<br>content:%s<br>category:%s", title, content, category);
}
@RequestMapping("adit")
public String adit() {
return "";
}
@RequestMapping("del")
public String del() {
return "";
}
}
업로드가 정상적으로 되었음을 확인