반응형
파일 업로드
controller
//(작성) 제출 api @RequestMapping(value = "/submitVerification.do", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<Object> submitVerification(VerifyReportSubmitDTO formData) { try { int fileCount = veriReportService.saveSubmitReportData(formData); if (fileCount > 0) { return ResponseEntity.ok() .body(VerifyResContainer.Success.body("200", "ok")); } } catch(Exception e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(VerifyResContainer.Error.errorMsg("저장중 오류가 발생했습니다.")); } return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(VerifyResContainer.Error.errorMsg("데이터를 찾지 못했습니다.")); }
request vo
@Data @NoArgsConstructor @AllArgsConstructor @ToString public class VerifyReportSubmitDTO implements Serializable{ private static final long serialVersionUID = 1772260604963268207L; private String project_id; private int verify_sn; private int report_degree; private List<MultipartFile> excel_data; private List<MultipartFile> raw_data_list; private String json_oss_data_list; }
- List multipart 형태와 string 형태가 통신하는 모습.
파일 다운로드
// file_id 기준 존재하는 파일 다운로드 @RequestMapping(value = "/fileDownload.do", method = RequestMethod.GET) public ResponseEntity<Resource> fileDownload(@RequestParam String file_id) throws IOException { //파일에 실제 경로를 가저오는 작업 VerifyFileVO vo = veriReportService.getFileVerifyFile(file_id); String str = VerifyFileVO.getMyFileFullPath(vo); //파일 생성 File f = new File(str); InputStream is = FileUtils.openInputStream(f); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=\"" + f.getName() + "\""); return ResponseEntity.ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(new InputStreamResource(is)); }
반응형