• Java用transferTo方式实现文件上传


    import com.springbootemaildemo.entity.ResponseEntity;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    
    @RestController
    @RequestMapping("/file")
    @Api("文件操作")
    public class FileController {
        private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    
        @ApiOperation("文件上传")
        @PostMapping("/upload")
        public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) {
            long startTime = System.currentTimeMillis();
            String path = "";
            String originalFilename = file.getOriginalFilename();
            logger.info("fileName:" + originalFilename);
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String fileType = originalFilename.substring(lastIndexOf + 1);
            //文件类型判断 doc,docx,jpg,png,xls
            logger.info("截取文件名类型:{}", fileType);
            if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("dox") || fileType.equals("docx") || fileType.equals("xls")) {
                path = "D:/filesss/" + new Date().getTime() + originalFilename;
                File newFile = new File(path);
                //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
                try {
                    file.transferTo(newFile);
                    long endTime = System.currentTimeMillis();
                    logger.info("采用file.Transto的运行时间:" + String.valueOf(endTime - startTime) + "ms");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                return new ResponseEntity("500", "不允许该" + fileType + "文件类型上传", "fail");
            }
            return new ResponseEntity("200", path, "success");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    玩转Linux与运维岗(31)
    messageBox的入门学习
    HDFS总结(未完待续)
    SwiftUI AR教程之应用程序中使用 RealityKit 生成 3D 文本(教程含完整源码)
    【Mysql】EXPLAIN
    【数学建模学习笔记【集训十天】之第三天】
    RK3588平台开发系列讲解(Thermal篇)Thermal的设备树配置
    磁环选型攻略及EMC整改技巧
    【unity资源加载与优化章】Profiler优化工具详解
    【Python基础】 Python设计模式之单例模式介绍
  • 原文地址:https://blog.csdn.net/qq_45720234/article/details/126156974