(文件下载,预览)inputstream跟outputstream的区别
inputstream--》输入,用于将数据变成流在管道传输
outputstream--》输出,用于承接管道中的数据,并输出到相应地点
outputstream是由路径生成的就写到这个路径,是由response生成的就写在写在页面上,即下载或预览
设置了header就是下载,没设置只要浏览器能解析的就会预览(不指明后缀名会这样,但是对word,exlcel无效),更好的方法就是五无论下载还是预览都设置头,浏览器会自动然你选择浏览还是下载
/**
* 文件下载
* 此方法用于下载(资讯信息-点钢周报)
* 在shiro中配置此路径过滤验证
* @param request
* @param response
* @param path
* @param fileName
* @return String
*/
@RequestMapping(value = "/downloadInvoiceFile")
public String downloadInvoiceFile(HttpServletRequest request, HttpServletResponse response, String path, String fileName) {
if (StringUtils.isNull(path)) {
return null;
}
if (StringUtils.isNull(fileName)) {
fileName = "附件";
}
try {
response.reset();
response.setContentType("MIME");/////
//response.setContentType("MIME");//自动识别后缀名,但是对于word,excel不准
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GB2312"), "ISO_8859_1")+path.substring(18) + "\"");
//这里设置的名字就是附件的名字及后缀名(任何文件的后缀名准确设置),注意中文要转码,否则会出现乱码或设置无效
tfsManager.fetchFile(path, "", response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
========================预览
(对于浏览器下载预览可供你选择(除了zip等浏览器知道预览不了的))======================
/*
* 放货单显示
*/
@RequestMapping(value = "/showPic")
public void showPic(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
String path = request.getParameter("path");
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GB2312"), "ISO_8859_1")+path.substring(18) + "\"");
tfsManager.fetchFile(path, "", response.getOutputStream());
}