需要导入jar包
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
配置文件上传解析器
<!--
解析器,将接收到客户端所上传的文件的File对象转换成MultipartFile对象
注意:文件解析器的beanid必须是multipartResolver,因为在解析的过程中底层的代码会通过getBean方法来获取此bean
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件及解析的编码,一定要与页面的pageencoding保持一致 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 设置做大上传文件的大小 -->
<property name="maxUploadSize" value="888888888"></property>
</bean>
上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="down">下载图片</a></br>
<form action="up" method="post" enctype="multipart/form-data">
头像:<input type="file" name="uploadFile"/>
描述:<input type="text" name="desc"/>
<input type="submit" value="上传">
</form>
</body>
</html>
Controller层代码
package com.blh.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class TestUploadAndDownloadController {
@RequestMapping("/down")
public ResponseEntity<byte[]> down(HttpSession session) throws IOException{
//获取下载文件的路径
String realPath = session.getServletContext().getRealPath("imgs");
String finalPath = realPath + File.separator+"1.jpg";
InputStream is = new FileInputStream(finalPath);
//available()获取输入流所读取的文件的最大字节数
byte[] b = new byte[is.available()];
is.read(b);
//设置响应头
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=abc.jpg");
//设置状态码
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b,headers,statusCode);
System.out.println(realPath);
is.close();
return entity;
}
@RequestMapping(value="/up_old",method = RequestMethod.POST)
public String up_old(String desc,MultipartFile uploadFile,HttpSession session) throws IOException {
String name = uploadFile.getName();
//获取上传文件的名称
String originalFilename = uploadFile.getOriginalFilename();
String path = session.getServletContext().getRealPath("photoes")+File.separator+originalFilename;
//获取输入流
InputStream is = uploadFile.getInputStream();
//获取输出流
File file = new File(path);
OutputStream os = new FileOutputStream(file);
int i = 0;
// while((i = is.read()) != -1) {
// os.write(i);
// }
byte[] b = new byte[1024];
while((i = is.read(b)) != -1) {
os.write(b, 0, b.length);
}
os.close();
is.close();
return "success";
}
@RequestMapping(value="/up",method = RequestMethod.POST)
public String up(String desc,MultipartFile uploadFile,HttpSession session) throws IOException {
//获取上传文件的名称
String originalFilename = uploadFile.getOriginalFilename();
String finaFileNameString = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
String path = session.getServletContext().getRealPath("photoes")+File.separator+finaFileNameString;
File file = new File(path);
uploadFile.transferTo(file);
return "success";
}
}