当前位置: 首页> 教育> 大学 > 外贸出口流程_各种免费源码共享网站_软件测试培训班多少钱_沐浴露营销软文

外贸出口流程_各种免费源码共享网站_软件测试培训班多少钱_沐浴露营销软文

时间:2025/7/11 9:50:12来源:https://blog.csdn.net/weixin_44088274/article/details/143004298 浏览次数:0次
外贸出口流程_各种免费源码共享网站_软件测试培训班多少钱_沐浴露营销软文

Gitee仓库

https://gitee.com/Lin_DH/system

介绍

文件上传是指将本地的图片、视频、音频等文件上传到服务器,供其他用户浏览下载的过程,文件上传在日常项目中用的非常广泛。

实现代码

第一步:在配置文件新增如下配置

application.yml

spring:servlet:multipart:max-file-size: 10MB #默认为1MB    max-request-size: 10MB #默认为10MB
file:upload:path: F:/files/

第二步:编写文件上传页面

upload.html

<!DOCTYPE html>
<html lang="en">
<head lang="en"><meta charset="UTF-8" /><title>文件上传页面</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">选择要上传的txt格式文件:<input type="file" name="file"><br><hr><input type="submit" value="提交">
</form>
</body>
</html>

第三步:编写文件上传页面和对应接口

FileController.java

package com.lm.system.controller;import com.lm.system.exception.FileException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;/*** @author DUHAOLIN* @date 2024/10/15*/
@Controller
public class FileController {private final static String FILE_FORMAT_TXT = "txt";@Value("${file.upload.path}")private String path;@GetMapping("uploadPage")public String uploadPage() {return "upload";}@PostMapping("upload")@ResponseBodypublic String upload(@RequestParam("file") MultipartFile file) throws IOException {//校验文件try {String[] fileFormats = { FILE_FORMAT_TXT };checkFile(file, fileFormats);} catch (FileException e) {e.printStackTrace();return e.getMessage();}String filename = path + file.getOriginalFilename().replace(FILE_FORMAT_TXT, "_" + System.currentTimeMillis() + FILE_FORMAT_TXT);File newFile = new File(filename);Files.copy(file.getInputStream(), newFile.toPath());return "新文件已生成," + newFile.getAbsolutePath();}private void checkFile(MultipartFile file, String[] fileFormats) {//校验文件大小checkSize(file.getSize());//校验文件名checkFilename(file.getOriginalFilename());//校验文件格式checkFileFormat(file.getOriginalFilename(), fileFormats);}private void checkSize(long size) {if (size > 10485760L) //10MBthrow new FileException("文件大于10MB");}private void checkFilename(String filename) {if (!StringUtils.hasText(filename))throw new FileException("文件名有误");}private void checkFileFormat(String filename, String[] fileFormats) {int i = filename.lastIndexOf(".");String suffix = filename.substring(i + 1); //文件后缀long c = Arrays.stream(fileFormats).filter(s -> s.equals(suffix)).count(); //判断是否存在该文件后缀if (c < 1) throw new FileException("文件格式有误,该文件类型为:" + suffix);}}

第四步:添加文件异常类

FileException.java

package com.lm.system.exception;/*** @author DUHAOLIN* @date 2024/10/15*/
public class FileException extends RuntimeException {public FileException() {super();}public FileException(String message) {super(message);}public FileException(String message, Throwable cause) {super(message, cause);}
}

效果图

上传不正确的非 .txt 格式的文件
在这里插入图片描述
上传确认的 .txt 文件
在这里插入图片描述

项目结构图

在这里插入图片描述

关键字:外贸出口流程_各种免费源码共享网站_软件测试培训班多少钱_沐浴露营销软文

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: