일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- importjetson
- 빈생성에러
- 디저트는
- 강의리추어탕본점
- 기세이다
- 기본소스코드끌어오기
- CSS
- ദ്ദി( ◠‿◠ )
- 운암동점심
- 달달한하루
- 뷰리졸버설정
- 운암동돈까스맛집
- 다시또봐요
- 광주광역시운암동
- 전체파일내키워드찾기
- fastapi
- 소스분석
- 하나이름생각이나지않고
- 표준프레임워크
- 담백하답니다
- 자바파일업로드
- opencvwithcudano
- install에러
- 매력이다이다
- 광주꾸덕꾸덕
- 티파라찌
- 광주광역시이색카페
- root접근하기
- 소스코드보는법익히기
- 스프링부트환경설정
Archives
- Today
- Total
개발자 구겹이
단순 파일 업로드 스프링부트 자바코드 본문
StringBoot의 Servlet (HttpServletRequest)기능으로 사용해서 업로드 할 거기 때문에
CRUD 게시판을 단순히 만들어 둔 곳에 input type=file을 추가하고
환경설정 몇가지를 해주면 됩니다.
1. HTML 코드 추가
<input type="file" name="file1" id="file1">
2. application.properties 파일에 설정 추가
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-request-size=30MB
spring.servlet.multipart.max-file-size=10MB
3. @Transactional 서비스 구현 단 추가
@Autowired
private HttpServletRequest request;
@Transactional
public void uploadFile(MultipartHttpServletRequest multiRequest) throws Exception {
Map<String, MultipartFile> fileMap = multiRequest.getFileMap();
Iterator<Map.Entry<String, MultipartFile>> itr = fileMap.entrySet().iterator();
MultipartFile file = null;
String filePath = "C:\\Users\\USER\\Desktop\\userboard\\addfile\\";
while (itr.hasNext()) {
Map.Entry<String, MultipartFile> entry = itr.next();
file = entry.getValue();
String fileOriginalName = file.getOriginalFilename();
if(!fileOriginalName.isEmpty()){
File fileFolder = new File(filePath);
if(!fileFolder.exists()){
if(fileFolder.mkdir()){
System.out.println("[file.mkdirs]: Success");
}
}
File saveFile = new File(filePath + fileOriginalName);
file.transferTo(saveFile);
}
}
}
4. Controller단 추가
@PostMapping("/register")
public String register(@RequestParam("title") String title, @RequestParam("content") String content, Model model, MultipartHttpServletRequest multiRequest) throws IOException {
try{
boardService.registerBoard(title, content);
boardService.uploadFile(multiRequest);
}catch(Exception e){
throw new IOException(e);
}
return "redirect:/board";
}
기존의 title, content 파람에 이어 service간에 uploadFile 메소드 추가한 곳을 중점으로 업데이트해주시면 됩니다.
'java > springboot' 카테고리의 다른 글
[스프링부트 기본 환경설정] 뷰리졸버 설정하기 (0) | 2024.09.30 |
---|---|
[springboot]웹 구동 시 setting - default-web-mapping-root! (0) | 2024.05.28 |
[springboot] Controller (0) | 2024.05.27 |