我们以Thymeleaf
页面模板引擎为例,简单介绍利用 apache fileupload 工具实现文件上传的功能。
2.1、添加相关依赖包
首先创建一个基础的 Spring Boot 项目,并引入相关的依赖包。
2.2、添加相关配置参数
2.3、文件上传示例
对应文件上传的Controller
类,示例如下:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
@Controller
public class ApacheFileController {
/**
* 定义文件上传的目录
*/
private static String FILE_DIR = "/Users/demo/file";
/**
* 访问 upload3 路径时,跳转到apacheUpload.html页面
* @return
*/
@GetMapping("/upload3")
public String index() {
return "apacheUpload";
}
/**
* 上传文件,支持多文件/表单上传
* @param request
* @throws Exception
*/
@PostMapping("/apacheFileUpload")
@ResponseBody
public String fileUpload(HttpServletRequest request) throws Exception {
// 判断上传的文件是普通的表单还是带文件的表单
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
// 终止方法运行,说明这是一个普通的表单,直接返回
return "Upload file fail";
}
// 1.创建DiskFileItemFactory对象,处理文件上传路径或者大小限制的
DiskFileItemFactory factory = getDiskFileItemFactory();
// 2.获取ServletFileUpload
ServletFileUpload upload = getServletFileUpload(factory);
// 3.处理上传的文件
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem fileItem : fileItems) {
// 判断上传的文件是普通的表单还是带文件的表单
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString("UTF-8"); // 处理乱码
System.out.println(name + ": " + value);
} else {
// 处理文件
String filePath = FILE_DIR + "/" + fileItem.getName();
try(InputStream inputStream = fileItem.getInputStream();
OutputStream outputStream = new FileOutputStream(filePath)) {
// 拷贝文件流
IOUtils.copy(inputStream, outputStream);
}
// 清除临时文件
fileItem.delete();
System.out.println("上传成功,文件名:" + fileItem.getName());
}
}
return "Upload file success";
}
/**
* 创建DiskFileItemFactory对象
* @return
*/
private DiskFileItemFactory getDiskFileItemFactory() {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置一个缓冲区大小, 当文件大于这个缓冲区大小的时候, 就会放到临时磁盘目录,防止内存崩溃
factory.setSizeThreshold(1024 * 1024);
// 设置临时磁盘目录, 接收上传的 File
factory.setRepository(new File(FILE_DIR + "/cache"));
return factory;
}
/**
* 获取ServletFileUpload
* @param factory
* @return
*/
private ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
ServletFileUpload upload = new ServletFileUpload(factory);
// 监听上传进度
upload.setProgressListener(new ProgressListener() {
@Override
public void update(long pBytesRead, long pContentLength, int pItems) {
System.out.println("总大小:" + pContentLength + ",已上传:" + pBytesRead);
}
});
// 处理乱码问题
upload.setHeaderEncoding("UTF-8");
// 设置单个文件的最大值,-1:表示无限制
upload.setFileSizeMax(-1L);
return upload;
}
}
启动服务后,访问http://localhost:8080/upload3
,可以看到如下界面:
在服务控制台,还可以看到上传的进度信息。
2.4、文件下载示例
@Controller
public class DownloadController {
private static final String SRC_PATH = "/Users/demo/file";
/**
* 通过文件名获取文件并以流的形式返回给客户端
* @param filename
* @param response
*/
@GetMapping("/download/{filename:.+}")
public void download(@PathVariable String filename, HttpServletResponse response) throws Exception {
File file = new File(SRC_PATH +'/'+ filename);
if(!file.exists()){
throw new RuntimeException("下载文件不存在");
}
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 使用缓存流,边读边写
try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
OutputStream os = response.getOutputStream();
byte[] buff = new byte[1024];
int i;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (IOException e) {
throw new RuntimeException("下载文件失败");
}
}
}
最后说一句(求关注!别白嫖!)
如果这篇文章对您有所帮助,或者有所启发的话,求一键三连:点赞、转发、在看。
关注公众号:woniuxgg,在公众号中回复:笔记 就可以获得蜗牛为你精心准备的java实战语雀笔记,回复面试、开发手册、有超赞的粉丝福利!
标签:文件,fileupload,SpringBoot,org,factory,new,apache,import,上传 From: https://blog.51cto.com/u_16502039/12092027