@RequestMapping(value = "/dow.do")
@ResponseBody
public void dow(HttpServletRequest request , HttpServletResponse response){
String file = "E:"+ File.separator+"Oppo+Vendor+Agreement_encrypted_.pdf";
try {
// file = URLEncoder.encode(file,"UTF-8");
/* File file1 = new File(file); *//*查找该文件*//*
InputStream inputStream = new FileInputStream(file1);
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment;filename=" + file);
OutputStream outputStream = null;
outputStream = response.getOutputStream();
int i =0;
byte[] buff=new byte[1024*10];//可以自己 指定缓冲区的大小
while ((i= inputStream.read(buff))>-1){
outputStream.write(buff,0,i);
}
inputStream.close();
outputStream.close();*/
File file1 = new File(file); /*查找该文件*/
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file1));
BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//response.setContentType("application/x-msdownload;");
response.setContentType("application/pdf");/*预览方式*/
// response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getBytes("UTF-8"), "ISO-8859-1")); 以下载的方式打开文件,无法预览
response.setCharacterEncoding("UTF-8");
byte[] bt = new byte[(int)file1.length()];
int i = 0;
while ((i=inputStream.read(bt)) >-1){
outputStream.write(bt,0,i);
}
outputStream.flush();
inputStream.close();
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
文件下载预览
最新推荐文章于 2023-05-06 18:57:03 发布