下载
链接
<a href="http://localhost:8080/conformitySSM/test4/qyc.do">点我下载英文名</a><br>
<a href="http://localhost:8080/conformitySSM/test5/强月城.do">点我下载中文名</a><br>
@RequestMapping("test4/{filename}")
public ResponseEntity<byte[]> test4(@PathVariable String filename,HttpSession session) throws Exception {
String realpath = session.getServletContext().getRealPath("/WEB-INF/download/"+filename);
System.out.println(realpath);
//创建下载资源的file对象
File file = new File(realpath);
//俩头一体
HttpHeaders headers = new HttpHeaders();
//附件下载 显示名称
headers.setContentDispositionFormData("attachment", filename);
//设置mime类型
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//响应体
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.OK);
return responseEntity;
}
英文浏览器自带了解析中文的功能,所以英文和中文都可以,要是浏览器不支持的话把filename转成utf-8
headers.setContentDispositionFormData("attachment", URLEncoder.encode(filename,"utf-8"));
大文件下载
因为responseEntity是直接开辟与文件大小一样大的字节数组,所以不适合大文件的下载
所以还是得回到原始方法 :流
//大文件下载
@RequestMapping("test6/{filename}")
public void test6(@PathVariable String filename,HttpServletRequest req,HttpServletResponse res) throws IOException {
//一般来说和数据库关联,表里存地址,这里就直接下载了
String pathString = "C:\\Users\\hp\\Pictures\\backiee-102533.jpg";
// String filenames = "4K壁纸";
//4步
InputStream inputStream = new FileInputStream(pathString); //读取
OutputStream outputStream = res.getOutputStream(); //写入
//俩头
//文件名
res.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8")+".jpg");
//类型
res.setHeader("Content-Type", "image/jpeg");
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
下载new in
上传new out
文件上传
DiskFileItemFactory 文件解析器
//文件上传DiskFileItemFactory
@RequestMapping("test7")
public String test7(HttpServletRequest req,HttpServletResponse res) throws FileUploadException, IOException {
//创建文件解析工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//创建文件解析对象
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//解析上传表单 一旦执行解析方法,表单内容全部读取到服务器内存
@SuppressWarnings("unchecked")
List<FileItem> list = servletFileUpload.parseRequest(req);
//基本方法
System.out.println("mime类型:"+list.get(0).getContentType());
System.out.println("文件全部名:"+list.get(0).getName());
System.out.println("大小:"+list.get(0).getSize()+" 字节");
// mime类型:image/jpeg
// 文件名:C:\Users\hp\Desktop\4K壁纸.jpg
// 大小:1953788 字节
String filenames = list.get(0).getName().substring(list.get(0).getName().lastIndexOf("\\")+1);
//IO
String path = req.getSession().getServletContext().getRealPath("WEB-INF/upload/"+filenames);
OutputStream outputStream = new FileOutputStream(path);
InputStream inputStream = list.get(0).getInputStream();
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
String path2 = "C:\\Users\\hp\\Workspaces\\MyEclipse CI\\conformitySSM\\WebRoot\\WEB-INF\\upload\\"+filenames;
OutputStream outputStream1 = new FileOutputStream(path2);
InputStream inputStream1 = list.get(0).getInputStream();
IOUtils.copy(inputStream1, outputStream1);
inputStream1.close();
outputStream1.close();
return "success";
}
注意:我故意弄额俩个路径,一个服务器的,一个工作空间的
SpringMVC支持
配置
<!-- springMCC对文件上传的支持 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<!-- 字节 -->
<property name="maxUploadSize" value="100000"></property>
</bean>
//文件上传MVC支持
@RequestMapping("test8")
public String test8(MultipartFile file,HttpServletRequest req) throws IllegalStateException, IOException {
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
// file
// c++.txt
String path = req.getSession().getServletContext().getRealPath("WEB-INF/upload/"+file.getOriginalFilename());
File localFile = new File(path);
file.transferTo(localFile);
return "success";
}
IO流
也用到了MultipartFile
//流
@RequestMapping("test9")
public String test9(MultipartFile file,HttpServletRequest req) throws IllegalStateException, IOException {
String path = req.getSession().getServletContext().getRealPath("WEB-INF/upload"+"/"+file.getContentType().replace("/", "_"));
File file2 = new File(path);
//如果路径不存在新建
if(!file2.exists()) {
file2.mkdir();
}
String pathnewString = path+"/"+UUID.randomUUID()+file.getOriginalFilename();
OutputStream outputStream = new FileOutputStream(pathnewString);
InputStream inputStream = file.getInputStream();
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
return "success";
}
- 解决了同名问题
- 解决了分类问题