/***
* 下载
* @return
* @throws IOException
*/
@RequestMapping(value="/downFile.do")
public ResponseEntity<byte[]> downFile(HttpServletRequest request,HttpServletResponse response) throws IOException{
//先获取路径
String path = request.getSession().getServletContext().getRealPath("/index/2016-09-05-01-回顾.wmv");
//通过file读取文件
File file =new File(path);
//获得http请求头(通过springmvc自带下载器下载(方式一))
// HttpHeaders headers = new HttpHeaders();
// //判读是否存在
// if(file.exists()){
// //设置中文编码
// String fileName = new String("王者荣耀.wmv".getBytes("utf-8"),"iso-8859-1");
// //设置响应文件
// headers.setContentDispositionFormData("attachment", fileName);
// //设置响应方式
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// //把文件以二进制写入
// return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
// }
//通过流方式下载(方式二(通用下载方式))
if(file.exists()){
//先读取文件
InputStream is = new FileInputStream(file);
//设置输出格式
// response.reset();//去掉jsp空格行
// //设置http头
// response.setContentType("bin");
// response.addHeader("Content-Disposition","attachment;fileName"+new String(("2016-09-05-01-回顾").getBytes("utf-8"),"iso-8859-1"));
// HttpHeaders headers = new HttpHeaders();
// //设置编码
// String fileName = new String ("英雄联盟.wmv".getBytes("utf-8"),"iso-8859-1");
// headers.setContentDispositionFormData("attachement",fileName);
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//1.设置文件content类型
response.setContentType("multipart/form-data");
//2.设置文件头
response.setHeader("Content-Disposition","attachment;fileName="+new String(("英雄联盟.wnv").getBytes("utf-8"),"iso-8859-1"));
//写入
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
//开始读写
byte[] b = new byte[2048];
int leng = 0;
while((leng=is.read(b))!=-1){
os.write(b, 0, leng);
}
os.flush();
os.close();
is.close();
}
return null;
}
* 下载
* @return
* @throws IOException
*/
@RequestMapping(value="/downFile.do")
public ResponseEntity<byte[]> downFile(HttpServletRequest request,HttpServletResponse response) throws IOException{
//先获取路径
String path = request.getSession().getServletContext().getRealPath("/index/2016-09-05-01-回顾.wmv");
//通过file读取文件
File file =new File(path);
//获得http请求头(通过springmvc自带下载器下载(方式一))
// HttpHeaders headers = new HttpHeaders();
// //判读是否存在
// if(file.exists()){
// //设置中文编码
// String fileName = new String("王者荣耀.wmv".getBytes("utf-8"),"iso-8859-1");
// //设置响应文件
// headers.setContentDispositionFormData("attachment", fileName);
// //设置响应方式
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// //把文件以二进制写入
// return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
// }
//通过流方式下载(方式二(通用下载方式))
if(file.exists()){
//先读取文件
InputStream is = new FileInputStream(file);
//设置输出格式
// response.reset();//去掉jsp空格行
// //设置http头
// response.setContentType("bin");
// response.addHeader("Content-Disposition","attachment;fileName"+new String(("2016-09-05-01-回顾").getBytes("utf-8"),"iso-8859-1"));
// HttpHeaders headers = new HttpHeaders();
// //设置编码
// String fileName = new String ("英雄联盟.wmv".getBytes("utf-8"),"iso-8859-1");
// headers.setContentDispositionFormData("attachement",fileName);
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//1.设置文件content类型
response.setContentType("multipart/form-data");
//2.设置文件头
response.setHeader("Content-Disposition","attachment;fileName="+new String(("英雄联盟.wnv").getBytes("utf-8"),"iso-8859-1"));
//写入
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
//开始读写
byte[] b = new byte[2048];
int leng = 0;
while((leng=is.read(b))!=-1){
os.write(b, 0, leng);
}
os.flush();
os.close();
is.close();
}
return null;
}