728x90
반응형
액셀 파일 업로드
Controller
@ApiOperation(value = "Excel File 저장",
notes = "✅ Excel File 저장 합니다.\n - \n " )
@PostMapping(value = "/")
public int excelData( @ApiParam( name = "filePath",
type = "MultipartFile",
value = "학습 데이터 파일 경로",
example = "",
required = true )
@RequestParam(value="filePath") MultipartFile tdiFilePath,
HttpServletRequest request) {
ExcelService service = new ExcelService();
service.writeFile(tdiFilePath, request)
return 0 ;
}
✅ Excel Service 첫번째 방법
더보기
/*
* 액셀 파일을 업로드 합니다.
*/
public String fileUpload(MultipartFile file, HttpServletRequest request ) throws IOException {
String fileName = createTitle("TRAIN_DATA_");
String rootUrl = request.getSession().getServletContext().getRealPath("/file/traindata");
File fileDir = new File(rootUrl);
if(!fileDir.exists()){
fileDir.mkdirs();
}
File fileAdd = new File(rootUrl+"/"+fileName);
file.transferTo(fileAdd);
return fileName;
}
✅ Excel Service 두번째 방법
더보기
public void fileUpload(MultipartFile file, HttpServletRequest request ) throws FileNotFoundException{
String fileName = createTitle("TRAIN_DATA1_");
String rootUrl = request.getSession().getServletContext().getRealPath("/file/traindata");
FileOutputStream fos = new FileOutputStream(rootUrl + "\\" + fileName);
try{
byte fileData[] = file.getBytes();
fos = new FileOutputStream(rootUrl + "\\" + fileName);
fos.write(fileData);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos != null){
try{
fos.close();
}catch(Exception e){}
}
}
}
위 두 방법으로 액셀 다운로드 후 파일을 열라고 하는데 아래처럼 경고창이 뜬다.
파일 형식 또는 확장명이 잘못되어 '-----' 파일을 열 수 없습니다.
세번쨰 방법으로 하니까 잘 되었다.
✅ 🙋♀️ Excel Service 세번째 방법
더보기
public String uploadFileTest(MultipartFile file, HttpServletRequest request ) throws IOException {
String rootUrl = request.getSession().getServletContext().getRealPath("/file/traindata");
String filePath = rootUrl + "/" + file.getOriginalFilename();
File dest = new File(filePath);
file.transferTo(dest);
return "uploaded";
}
728x90
반응형
'개발중 > Spring' 카테고리의 다른 글
스프링 - zip 파일 다운로드 (0) | 2021.07.05 |
---|---|
JPA 공부하기 (0) | 2021.06.23 |
@ApiParam 과 @RequestParam 의 차이점 (0) | 2021.05.27 |
REST API 의 Mapping 방법 정리 (0) | 2021.05.27 |
@RequestParam Null 잡기 (0) | 2021.05.26 |