不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2023-10-23 00:09:39  修改时间:2024-04-30 15:45:20  分类:Java框架/系统  编辑

说明

用Spring的 MultipartFile在Springboot 中实现上传单个文件(多个文件)

多文件上传,大量文件时,防止文件名相同,重新修改存储文件名。

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>

<h1>【单文件】上传</h1>
<form method="post" action="/upload.do" enctype="multipart/form-data">
	<input type="file"  name="file" /><br/><br/>
	<input type="submit" value="上传" /><br/>
</form>

<h1>【多文件】上传</h1>
<form method="post" action="/upload2.do" enctype="multipart/form-data">
	<input type="file"  name="files" multiple="multiple"/><br/><br/>
	<input type="submit" value="上传" /><br/>
</form>

</body>
</html>

controller

package com.example.springBootdemo.controller;
 
import java.io.File;
import java.io.IOException;
import java.util.UUID;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import com.example.springBootdemo.utils.FileUtils;

/**
 * 单文件、多文件上传
 * @author luolei
 * @date 2019年1月31日
 */
@RestController
public class FileOperationController {
 
	/**
	 * 【单文件】上传
	 * @param file
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 * String
	 */
	@PostMapping("/upload.do")
	//@RequestMapping(value="/upload.do", method = RequestMethod.POST)
	//上传的文件会转换成MultipartFile对象,file名字对应html中上传控件的name
	public String upload(MultipartFile file) throws IllegalStateException, IOException{
		//取得当前上传文件的文件名称
		String originalFilename = file.getOriginalFilename();
		//transferTo是保存文件,参数就是要保存到的目录和名字
		String filePath = "C:\\Users\\Administrator\\Desktop\\images\\";
		file.transferTo(new File(filePath + originalFilename));
		System.out.println("文件类型:"+file.getContentType());	
		System.out.println("原文件名:"+originalFilename);
		System.out.println("是否为空:"+file.isEmpty());
		System.out.println("文件大小:"+file.getSize());		
		return "文件上传完毕";
	}
	/**
	 * 【多文件】上传,大量文件时,防止文件名相同,重新修改存储文件名
	 * @param files
	 * @return
	 * @throws IOException
	 * String
	 */
	@PostMapping("/upload2.do")
	//@RequestMapping(value="/upload2.do", method = RequestMethod.POST)
	//上传的文件会转换成MultipartFile对象,file名字对应html中上传控件的name
	public String upload2(MultipartFile[] files) throws IOException{
		if(files.length == 0){
			return "请选择要上传的文件";
		}
		for (MultipartFile multipartFile : files) {
			if(multipartFile.isEmpty()){
				return "文件上传失败";
			}
			byte[] fileBytes = multipartFile.getBytes();
			String filePath = "C:\\Users\\Administrator\\Desktop\\images\\";
			//取得当前上传文件的文件名称
			String originalFilename = multipartFile.getOriginalFilename();
			//生成文件名
			String fileName = UUID.randomUUID() +"&"+ originalFilename;
			FileUtils.uploadFile(fileBytes, filePath, fileName);
		}
 
		return "文件上传完毕";
	}
 
}

FileUtils 文件上传工具类

package com.example.springBootdemo.utils;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件工具类
 * @author luolei
 * @date 2019年1月31日
 */
public class FileUtils {
	/**
	 * 文件上传方法
	 * @param file 文件byte[]
	 * @param filePath 文件上传路径
	 * @param fileName 文件保存路径
	 * @throws IOException 
	 * @throws Exception
	 * void
	 */
    public static void uploadFile(byte[] file, String filePath, String fileName) throws IOException{
 
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
   }
}

application.properties

在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息

在application.properties中配置 spring.resources.static-locations可以查看访问我们上传的文件。

#单文件限制大小
spring.http.multipart.maxFileSize=10MB
#一次请求限制大小
spring.http.multipart.maxRequestSize=500MB
 
#在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息
#配置静态资源目录,以逗号分隔多个目录,
#加file:是因为后面指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量
spring.resources.static-locations=classpath:/static/,file:C:/Users/Administrator/Desktop/images/