根据狂神的SpringMVC中的上传和下载
1.文件上传
导包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
Jsp页面 必须有enctype=“multipart/form-data”
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="upload">
</form>
文件上传配置文件
<!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 请求编码格式 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 文件上传大小限制 10485760 = 10M -->
<property name="maxUploadSize" value="10485760"/>
<property name="maxInMemorySize" value="40960"/>
</bean>
Controller
第一种:复杂的
@Controller
public class FileController {
@RequestMapping("/upload")
public String upload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//获取文件名
String originalFilename = file.getOriginalFilename();
//非空判断
if("".equals(originalFilename)){
return "redirect:/index.jsp";
}
System.out.println("上传文件名:"+originalFilename);
//设置上传路径
String path = request.getServletContext().getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if(!realPath.exists()){
realPath.mkdir();
}
System.out.println("上传文件保存路径:"+realPath);
//文件输入流
InputStream is = file.getInputStream();
//文件输出流
FileOutputStream os = new FileOutputStream(new File(realPath, originalFilename));
//读取写入
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer))!=-1){
os.write(buffer,0,len);
os.flush();
}
os.close();
is.close();
return "redirect:/index.jsp";
}
}
第二种:使用SpringMVC
@RequestMapping("/upload2")
public String upload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
File realPath = new File(path);
if (!realPath.exists()){
realPath.mkdir();
}
//上传文件的路径
System.out.println("上传文件的路径:"+realPath);
//通过CommonsMultipartFile的方法直接写文件
file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
return "redirect:/index.jsp";
}
2.文件下载
@RequestMapping("/download")
public String down(HttpServletRequest request, HttpServletResponse response) throws IOException {
//下载图片的地址
String path = request.getServletContext().getRealPath("/upload");
//下载图片的名称
String fileName = "wallhaven-3993m6.jpg";
//设置响应头
response.reset();//设置网页不缓存
response.setCharacterEncoding("utf-8");//字符编码
response.setContentType("multipart/form-data");//设置二进制传输数据
response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
File file = new File(path, fileName);
FileInputStream input = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = input.read(buff))!=-1){
out.write(buff,0,len);
out.flush();
}
out.close();
input.close();
return null;
}