file upload

pom

java 复制代码
<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.2</version>
		</dependency>
java 复制代码
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class FileUploadController {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

  

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        System.out.println(realPath);
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
            return filePath;
        } catch (IOException e) {
//            e.printStackTrace();
        }
        return "上传失败!";
    }

    @PostMapping("/uploads")
    public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) {
        for (MultipartFile uploadFile : uploadFiles) {
            String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
            System.out.println(realPath);
            String format = sdf.format(new Date());
            File folder = new File(realPath + format);
            if (!folder.isDirectory()) {
                folder.mkdirs();
            }
            String oldName = uploadFile.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            try {
                uploadFile.transferTo(new File(folder, newName));
                String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
//            return filePath;
                System.out.println(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "上传失败!";
    }
}
java 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFile" value="请选择文件" multiple>
    <input type="submit" value="上传">
</form>
</body>
</html>
java 复制代码
#是否开启文件上传 默认是true
spring.servlet.multipart.enabled=true
#表示文件 写入磁盘的阈值 默认0
spring.servlet.multipart.file-size-threshold=0
# 文件上传的临时吧奥村位置
spring.servlet.multipart.location=E:\\temp
#单个文件大小 默认是 1M
spring.servlet.multipart.max-file-size=1MB
#多文件删除文件的总大小 默认是10M
spring.servlet.multipart.max-request-size=100MB
# 文件是否延迟解析 默认是          false
spring.servlet.multipart.resolve-lazily=false
相关推荐
知其然亦知其所以然10 分钟前
JVM社招面试题:队列和栈是什么?有什么区别?我在面试现场讲了个故事…
java·后端·面试
harmful_sheep18 分钟前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
星辰大海的精灵19 分钟前
如何确保全球数据管道中的跨时区数据完整性和一致性
java·后端·架构
大大。22 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
nc_kai25 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
Codebee35 分钟前
OneCode:AI时代的先锋——注解驱动技术引领开发范式变革
java
勤奋的知更鸟35 分钟前
Java 编程之状态模式
java·开发语言·状态模式
架构个驾驾1 小时前
深入浅出MyBatis-Plus实战指南
java
SimonKing1 小时前
解锁万能文件内容分析工具:Apache Tika
java·后端·程序员