SPRING/chapter04_MVC
UploadController.java
GAWON
2023. 7. 18. 09:15
package org.joonzis.controller;
import java.io.File;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.joonzis.domain.BoardAttachVO;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.log4j.Log4j;
@Log4j
@Controller
public class UploadController {
@GetMapping("/uploadForm")
public String uploadForm() {
log.info("uploadForm...");
return "uploadForm";
}
@PostMapping("/uploadFormAction")
public void uploadFormPost(MultipartFile[] uploadFile, Model model) {
/*
* MultipartFile의 메소드
*
* String getName() -파라미터 이름<input> 태그의 이름
* String getOriginalFileName() -업로드되는 파일의 이름
* boolean isEmpty() -파일이 존재하지 않는 경우true
* long getSize() - 업로드 파일의 크기
* byte[] getBytes() -byte[]로 파일 데이터 변환
* InputStream getInputStream() -파일 데이터와 연결된InputStream반환
* transferTO (File file) - 파일 저장
*/
//c드라브에 upload폴더 생성
String uploadFolder = "C:\\upload";
//파일 두개 업로드
for(MultipartFile multipartFile : uploadFile) {
log.info("===================");
log.info("Upload File Name: " + multipartFile.getOriginalFilename());
log.info("Upload File Size: " + multipartFile.getSize());
File saveFile = new File(uploadFolder,multipartFile.getOriginalFilename());
try {
multipartFile.transferTo(saveFile);
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
@GetMapping("/uploadAjax")
public String uploadAjax() {
log.info("upload ajax");
return "uploadAjax";
}
@PostMapping(value = "/uploadAjaxAction", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public ResponseEntity<List<BoardAttachVO>> uploadAjaxPost(MultipartFile[] uploadFile) {
log.info("upload ajax post.......");
List<BoardAttachVO> list = new ArrayList<BoardAttachVO>();
String uploadFolder = "C:\\upload";
//make folder
File uploadPath = new File(uploadFolder, getFolder());
log.info("upload path : " + uploadPath);
if(uploadPath.exists() == false) {
uploadPath.mkdirs();
}
for (MultipartFile multipartFile : uploadFile) {
log.info("---------------------");
log.info("Upload File Name: " + multipartFile.getOriginalFilename());
log.info("Upload File Size: " + multipartFile.getSize());
BoardAttachVO attachDto = new BoardAttachVO();
UUID uuid = UUID.randomUUID();
String uploadFileName = uuid.toString() + "_" + multipartFile.getOriginalFilename();
try {
File saveFile = new File(uploadPath, uploadFileName);
multipartFile.transferTo(saveFile);
attachDto.setFileName(multipartFile.getOriginalFilename());
attachDto.setUuid(uuid.toString());
attachDto.setUploadPath(getFolder());
list.add(attachDto);
} catch (Exception e) {
log.error(e.getMessage());
}
}
return new ResponseEntity<>(list, HttpStatus.OK);
}
private String getFolder() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String str = sdf.format(date);
return str.replace("-", File.separator);
}
@GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public ResponseEntity<Resource> downloadFile(String fileName){
log.info("download file : " + fileName);
Resource resource = new FileSystemResource("c:\\upload\\" + fileName);
log.info("resource : " + resource);
String resourceName = resource.getFilename();
String resourceOriginalName = resourceName.substring(resourceName.indexOf("_")+1);
HttpHeaders headers = new HttpHeaders();
try {
headers.add("Content-Disposition", "attachment; filename="+
new String(resourceOriginalName.getBytes("utf-8"), "ISO-8859-1"));
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<Resource>(resource, headers, HttpStatus.OK);
}
@PostMapping("/deleteFile")
@ResponseBody
public ResponseEntity<String> deleteFile(String fileName){
log.info("deleteFile: " + fileName);
File file = null;
try {
file = new File("c:\\upload\\" + URLDecoder.decode(fileName, "utf-8"));
file.delete();
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>("deleted", HttpStatus.OK);
}
}