一、文件上传
1、要用的基本两个包:
2、简单的表单:
<form action="/SpringDemo08/upLoad" method="post" enctype="multipart/form-data">
账号:<input type="text" name="userName"><br>
头像:<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
这里重点是:
method="post"
enctype="multipart/form-data"
必须是post方式提交,也必须设置enctype为
multipart/form-data,
controller
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UploadFile {
/**
* 文件上传
* @param file
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/upLoad")
@ResponseBody
public String upLoad(MultipartFile file) throws IllegalStateException, IOException {
// TODO Auto-generated method stub
// getOriginalFilename()获取原始文件名(在客户端时的文件名)
//getFileName() 在服务器上的文件名.
String filename = file.getOriginalFilename();
File file2 = new File("D:/imgs/"+filename);
//这里你可以进行重命名也可以把路径入库
//这里我就不做其他业务操作了
//MultipartFile中的transferTo不能使用两次
//因为http post文件流只可以接收读取一次,传输完毕则关闭流。
//可以把流保存为文件1,然后对文件1进行复制,移动等操作
//transferTo转存文件到指定路径
file.transferTo(file2);
return "上传成功";
}
}
applicationContext.xml配置:
<!-- 配置文件上传的处理器 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean>
CommonsMultipartResolver这个Bean的id必须为multipartResolver,
原因:CommonsMultipartResolver Bean是在中央控制器DispatcherServlet中加载的,而DispatcherServlet是通过名字来查找这个Bean的。而其他的,则是按照类型查找。
二、文件下载
- 方式一:基于ResponseEntity实现
@RequestMapping("/download ")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
// 需要下载的文件
File file = new File("C:\Users\Administrator\Pictures\Saved Pictures\2.jpg");
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;
}
- 方式二:通用下载实现
/**
* 文件下载
* @param req
* @param resp
* @throws IOException
*/
@RequestMapping("/downLoad")
@SuppressWarnings("resource")
public void downLoad(HttpServletRequest req,HttpServletResponse resp) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\Users\Administrator\Pictures\Saved Pictures\2.jpg");
req.setCharacterEncoding("utf-8");
resp.setContentType("multipart/form-data");
resp.setHeader("Content-Disposition", "attachment;fileName="+file.getName() );
FileInputStream inputStream = new FileInputStream(file);
ServletOutputStream outputStream = resp.getOutputStream();
int num = 0;
byte[] b = new byte[1024];
while ((num = inputStream.read(b)) != -1) {
outputStream.write(b, 0, num);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}