设置文件上传文件大小限制:
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MultipartConfig {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 置文件大小限制 ,超出此大小页面会抛出异常信息
factory.setMaxFileSize("10MB"); //KB,MB
// 设置总上传数据总大小
factory.setMaxRequestSize("10MB");
// 设置文件临时文件夹路径
// factory.setLocation("E://test//");
// 如果文件大于这个值,将以文件的形式存储,如果小于这个值文件将存储在内存中,默认为0
// factory.setMaxRequestSize(0);
return factory.createMultipartConfig();
}
}
JAVA文件上传代码:
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@RequestMapping("uploadFile")
public void upload(HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest mulRequest) {
String savePath = request.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println(savePath);
File file = new File(savePath);
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath + "目录不存在,需要创建");
file.mkdir();
}
try {
Map<String, MultipartFile> map = mulRequest.getFileMap();
Set<Entry<String, MultipartFile>> set = map.entrySet();
for (Entry<String, MultipartFile> entry : set) {
MultipartFile f = entry.getValue();
String filename = f.getOriginalFilename();
filename = filename.substring(filename.lastIndexOf("\\") + 1);
// 获取item中的上传文件的输入流
InputStream in = f.getInputStream();
// 创建一个文件输出流
System.out.println("-------------------------------------");
System.out.println(filename);
System.out.println(savePath + "\\" + filename);
System.out.println("-------------------------------------");
FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
// 创建一个缓冲区
byte buffer[] = new byte[1024];
// 判断输入流中的数据是否已经读完的标识
int len = 0;
// 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = in.read(buffer)) > 0) {
// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
out.write(buffer, 0, len);
}
// 关闭输入流
in.close();
// 关闭输出流
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
文件下载
演示所用的文件在服务端upload文件夹下存放:如下图
在JSP页面新增一个演示标签
@RequestMapping("/downloadFile")
protected void downloadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
// 第一步:设置响应类型
resp.setContentType("application/force-download");
// 第二读取文件
String path = req.getServletContext().getRealPath("/WEB-INF/upload/" + name);
System.out.println(path+"----------------path");
InputStream in = new FileInputStream(path);
// 设置响应头,编码
name = URLEncoder.encode(name, "UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + name);
resp.setContentLength(in.available());
// 开始下载
OutputStream out = resp.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
out.close();
in.close();
}