文件上传高级用法文件复制

news/2025/2/19 9:16:44/

文件上传

        spring 文件上传

  1. 导包
    <!--        文件上传--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.5</version></dependency>
  2. 编写接口
    
    @Controller
    public class UploadController {@PostMapping("/upload")@ResponseBodypublic Result add(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest req) throws IOException {String newFileName = "";if (multipartFile != null && !multipartFile.isEmpty()) {/*** 设置文件上传的地址 uploadPath*/String uploadPath = req.getServletContext().getRealPath("./") + "/upload";/*** 对文件名进行操作防止文件重名*///1,获取原始文件名String originalFilename = multipartFile.getOriginalFilename();//2,截取源文件的文件名前缀,不带后缀String fileNamePrefix = originalFilename.substring(0, originalFilename.lastIndexOf("."));//3,加工处理文件名,原文件加上时间戳String newFileNamePrefix = fileNamePrefix + System.currentTimeMillis();//4,得到新文件名newFileName = newFileNamePrefix + originalFilename.substring(originalFilename.lastIndexOf("."));File file = new File(uploadPath, newFileName);
    /*** 文件上传*/multipartFile.transferTo(file);
    /*** 文件复制*/
    /*** webUploadPath 上传到web目录下的upload*/String webUploadPath = uploadPath.split("out")[0] + "/web/" + "upload" + "/";createDirectory(webUploadPath);/*** 原文件路径*/Path filePath = file.toPath();/*** 目标路径位置*/Path webPath = Paths.get(webUploadPath);Files.copy(filePath,webPath.resolve(filePath.getFileName()));}return Result.getInstance(1, "/upload/" + newFileName);}private void createDirectory(String uploadPath) {// 如果目录不存在则创建File uploadDir = new File(uploadPath);if (!uploadDir.exists()) {uploadDir.mkdir();}}}

​​​文件高级复制

  使用Path和Files

调用Files的方法 copy,使用Path 进行数值获取

使用示例:假设我们要将文件复制到目录中,给出与源文件相同的文件名:

  Path source = ...Path newdir = ...Files.copy(source, newdir.resolve(source.getFileName()); 

参数

source - 要复制的文件的路径

target - 目标文件的路径(可能与源路径的不同提供程序相关联)

options - 指定副本应如何完成的选项


http://www.ppmy.cn/news/41290.html

相关文章

numpy 常见函数总结

np.flip&#xff1a;数据按照维度数据逆序 >>> A np.arange(8).reshape((2,2,2)) >>> A array([[[0, 1],[2, 3]],[[4, 5],[6, 7]]]) >>> np.flip(A, 0) array([[[4, 5],[6, 7]],[[0, 1],[2, 3]]]) >>> np.flip(A, 1) array([[[2, 3],[0, …

binkw32dll缺失怎么办?如何解决binkw32dll修复问题

binkw32dll缺失怎么办&#xff1f;在使用某些计算机游戏或应用程序时&#xff0c;您可能遇到过binkw32dll缺失的问题。这意味着您的计算机无法找到该DLL文件&#xff0c;从而无法正常运行程序。在本文中&#xff0c;我们将探讨binkw32.dll缺失的可能原因并提供解决方案。 一.什…

【机器学习】P16 激活函数 Activation Function

对于激活函数&#xff08;Activation Function&#xff09;&#xff0c;存在两个问题&#xff1a; 为什么要用激活函数&#xff1f;如何选择用哪种激活函数&#xff1f;如何使用激活函数&#xff1f; 本博文将围绕这两个问题&#xff0c;首先介绍激活函数的作用&#xff0c;从…

Python 进阶指南(编程轻松进阶):六、编写 Python 风格的代码

原文&#xff1a;http://inventwithpython.com/beyond/chapter6.html 强大对于编程语言来说是一个没有意义的形容词。每种编程语言都称自己长处。官方 Python 教程开头就说 Python 是一种简单易学、功能强大的编程语言。但是没有一种语言可以做另一种语言不能做的算法&#xff…

城乡供水一体化信息化系统-城乡供水一体化

建设方案 城乡供水一体化信息化系统是运用云计算、大数据等信息化手段&#xff0c;借助在线监测设备&#xff0c;并依托“供水信息化平台”&#xff0c;实时感知供水系统的运行状态&#xff0c;实现对农村供水工程远程监控、在线监测、实时预警、智慧监管。 系统功能 水源地监测…

【消息队列】聊一下生产者消息发送流程

消息发送流程 1.生产者main线程调用send发送消息&#xff0c;先走拦截器&#xff0c;然后会将消息进行序列化&#xff0c;然后选择对应的分区器&#xff0c;将消息发送到RecordAccumulator中&#xff0c;默认是32m 2.Sender线程会异步读取&#xff0c;要不数据达到batch的大小 …

git 命令:工作日常使用

git start 存储分支 git start list 查看所有存储 拉取最新master 合并到自己分支&#xff1a; git remote add [远程名称] [远程仓库链接] //关联(添加)远程仓库; 第一步&#xff1a;查看分支在哪里&#xff0c;是自己的吗&#xff0c;添加暂存区&#xff0c;添加到仓…

代码随想录算法训练营第三十六天-贪心算法5| 435. 无重叠区间、763.划分字母区间、56. 合并区间

435. 无重叠区间 参考文章&#xff1a;代码随想录 参考视频&#xff1a;贪心算法&#xff0c;依然是判断重叠区间 | LeetCode&#xff1a;435.无重叠区间_哔哩哔哩_bilibili 解题思路&#xff1a; 相信很多同学看到这道题目都冥冥之中感觉要排序&#xff0c;但是究竟是按照…