
全部展开
The
jsp页面下载文档在jsp中有一个标记. 当用户单击一个标签时,将下载文件.
href属性通常用于直接指向服务器地址. 只要链接文件存在,就会出现一个弹出的保存对话框.
在请求href中指向的地址之前,单击a标记以执行onclick事件.
前端jsp:
然后在js中使用
:
功能downloadtest(id){
var url =“ <%= request.getContextPath()%> /应用/下载” +“ /” + id;
$(“#pluginurl”). attr(“ href”,url);
}
3682090fb0ee5c0ce8c76d7ed2b8a0e7
背景处理Java代码:
/ **
*下载文件
* @param id appid
* @param响应
* /
@RequestMapping(值=“ /下载/ {id}”)
公共无效下载(@PathVariable字符串ID,HttpServletResponse响应){
String filepath =“”;
结果结果= appService.getAppById(id);
App app =(App)result.getMap(). get(“ app”);
if(app == null){
返回;
93b2ce8b36891371410633e67dfd5bea
}
filepath = app.getUrl();
文件文件=新文件(文件路径);
InputStream inputStream = null;
OutputStream outputStream = null;
byte [] b =新字节[1024];
int len = 0;
尝试{
inputStream =新的FileInputStream(文件);
outputStream = response.getOutputStream();
response.setContentType(“应用程序/强制下载”);
字符串文件名= file.getName();
filename = filename.substring(36,filename.length());

response.addHeader(“ Content-Disposition”,“ attachment; filename =” =“ + URLEncoder.encode(filenamejsp开发文档jsp开发文档,” UTF-8“));
response.setContentLength((int)file.length());
while(((len = inputStream.read(b))!= -1){
outputStream.write(b,0,len);
}
}捕获(异常e){
e.printStackTrace();
}最后{
if(inputStream!= null){
尝试{
inputStream.close();
inputStream = null;
}捕获(IOException e){

e.printStackTrace();
}
}
if(outputStream!= null){
尝试{
outputStream.close();
outputStream = null;
}捕获(IOException e){
e.printStackTrace();
}
}
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-145853-1.html
巴菲特哭了
那个白色的靴子是什么鬼