admin 管理员组文章数量: 1086019
前端代码:
//下载用户手册
function downUserManual() {
debugger
var downLoadPath = "/system/downUserManual.do";
var url = getRootPath() + downLoadPath;
window.open(url)
后端代码:
@RequestMapping(value = "/downUserManual.do", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
FileInputStream fis = null;
byte[] bytes = null;
ServletOutputStream ouputStream = null;
ByteArrayOutputStream baos = null;
try {
ServletContext servletContext = request.getSession().getServletContext();
String filePath = servletContext.getRealPath("/downloadfile/用户手册.pdf");
File file = new File(filePath);
fis = new FileInputStream(file);
baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
bytes = baos.toByteArray();
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ouputStream != null) {
try {
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
版权声明:本文标题:SpringMVC 下载文件(直接在浏览器打开) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1743814344a2484185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论