https://gitee.com/dafran/file-up-down.git

    配置

    1. server:
    2. port: 8899
    3. spring:
    4. servlet:
    5. multipart:
    6. # 最大支持文件大小
    7. max-file-size: 1024MB
    8. # 最大支持请求大小
    9. max-request-size: 1024MB
    10. #文件存储路径
    11. filepath: F:/open/

    实体类

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. public class Resp<E> {
    5. /**
    6. * 状态码
    7. */
    8. private String code;
    9. /**
    10. * 消息
    11. */
    12. private String message;
    13. /**
    14. * 特殊类型
    15. */
    16. private E body;
    17. /**
    18. * 成功
    19. * @param body
    20. * @param <E>
    21. * @return
    22. */
    23. public static <E> Resp<E> success(E body) {
    24. return new Resp("200","",body);
    25. }
    26. /**
    27. * 失败
    28. * @param code
    29. * @param message
    30. * @param <E>
    31. * @return
    32. */
    33. public static <E> Resp<E> fail(String code,String message) {
    34. return new Resp(code,message,(Object) null);
    35. }
    36. }

    Service

    1. public interface FileService {
    2. Resp<String> upload(MultipartFile file);
    3. Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException;
    4. }

    ServiceImpl

    1. @Service
    2. public class FileServerImpl implements FileService {
    3. @Override
    4. public Resp<String> upload(MultipartFile file) {
    5. // 判断文件是否为空
    6. if (file.isEmpty()) {
    7. return Resp.fail("400","文件为空!");
    8. }
    9. // 原始文件名,新文件名,文件路径(反斜杠两个第一个为转义),目标文件
    10. String originalFilename = file.getOriginalFilename();
    11. String fileName = System.currentTimeMillis()+"."+originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
    12. String filePath = "F:\\open\\";
    13. File dest = new File(filePath+fileName);
    14. // 没有文件夹,则创建
    15. if (!dest.getParentFile().exists()) {
    16. dest.getParentFile().mkdirs();
    17. }
    18. // 防止硬盘爆满或者其他情况导致失败
    19. try {
    20. file.transferTo(dest);
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. return Resp.fail("500",originalFilename + "上传失败!");
    24. }
    25. return Resp.success(fileName);
    26. }
    27. @Override
    28. public Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException {
    29. String fileName="1619692787751.pdf";
    30. String filePath = "F:\\open\\";
    31. File dest = new File(filePath+fileName);
    32. // 如果没有对应文件
    33. if (dest.exists()) {
    34. response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    35. response.setCharacterEncoding("UTF-8");
    36. // response.setContentType("application/force-download");
    37. response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
    38. byte[] buffer = new byte[1024];
    39. FileInputStream fis = null; //文件输入流
    40. BufferedInputStream bis = null;
    41. OutputStream os = null; //输出流
    42. try {
    43. os = response.getOutputStream();
    44. fis = new FileInputStream(dest);
    45. bis = new BufferedInputStream(fis);
    46. int i = bis.read(buffer);
    47. while(i != -1){
    48. os.write(buffer);
    49. i = bis.read(buffer);
    50. }
    51. } catch (Exception e) {
    52. // TODO Auto-generated catch block
    53. e.printStackTrace();
    54. }
    55. System.out.println("----------file download---" + fileName);
    56. try {
    57. bis.close();
    58. fis.close();
    59. } catch (IOException e) {
    60. // TODO Auto-generated catch block
    61. e.printStackTrace();
    62. }
    63. }
    64. return Resp.fail("501","没有对应文件");
    65. }
    66. }

    Controller

    1. @RestController
    2. @RequestMapping(value = "/file")
    3. public class FileController {
    4. @Autowired
    5. private FileService fileService;
    6. @RequestMapping(value = "/upload",method = RequestMethod.POST)
    7. private Resp<String> upload(@RequestParam("file")MultipartFile file) {
    8. return fileService.upload(file);
    9. }
    10. @RequestMapping(value = "/download",method = RequestMethod.GET)
    11. private Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException {
    12. return fileService.download(response);
    13. }
    14. }