파일업로드 Rest full 기능 샘플 소스



@RestController
@RequestMapping ("/file")
public class FileuploadController {

     @Autowired
     private ServletContext context;

     /**
      * 파일 멀티파트 업로드 Rest full
      *
      * @param inputFile
      * @return
      */
     @RequestMapping(value = "/upload", headers = ("content-type=multipart/*" ), method = RequestMethod.POST )
     public ResponseEntity<FileInfo> upload(@RequestParam ("file") MultipartFile inputFile) {

          FileInfo fileInfo = new FileInfo();
          HttpHeaders headers = new HttpHeaders();
           if (!inputFile .isEmpty()) {

               try {

                   String oriFileNm = inputFile.getOriginalFilename();
                   File destinationFile = new File(context.getRealPath("/WEB-INF/uploaded" ) + File.separator + oriFileNm);
                    inputFile.transferTo(destinationFile );
                    headers.add("File Uploaded Successfully - ", oriFileNm);

                    fileInfo.setFileName(destinationFile .getPath());
                    fileInfo.setFileSize(inputFile .getSize());
                    return new ResponseEntity<FileInfo>(fileInfo , headers, HttpStatus.OK );

              } catch (Exception e ) {
                    return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
              }
          } else {
               return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
          }
     }
}




/**
 * 파일정보 VO
 *
 * @author 엄승하
 */
@Data
public class FileInfo {

     private String fileName;
     private long fileSize ;

}





+ Recent posts