码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    如何在Next.js中使用react-viewer图片预览
    docker-部署 redis 主从复制(一主,一从)tag:redis:6.2.6
    ASEMI肖特基二极管SBT40100VFCT规格,SBT40100VFCT封装
    Windows 基于Visual Studio 开发Qt 6 连接MySQL 8
    2022 IDEA大会引领科技创新趋势 沈向洋团队重磅发布低空经济白皮书
    基于Java+SpringBoot+Vue前后端分离社区养老服务平台设计和实现
    MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
    webpack5 Image Minimizer减少图片体积提升打包构建速度
    精华推荐 | 【深入浅出RocketMQ原理及实战】「底层原理挖掘系列」透彻剖析贯穿RocketMQ的存储系统的实现原理和持久化机制
    荧光标记透明质酸;FITC-Hyaluronic Acid;HA-FITC定制合成
  • 原文地址:https://blog.csdn.net/qq_45720234/article/details/126156974
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号