jsp页面如何实现下载文档
2个回答
2016-01-19 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
jsp页面下载文档是在jsp中有一个a标签 ,当用户点击a标签的时候下载文件。
一般采用href属性直接指向一个服务器地址,只要链接的文件存在,就会给出弹出保存对话框.
点击a标签 先执行onclick事件,再请求href中指向的地址。
前端jsp:
<a href="#" onclick="javascript:downloadtest('${app.id}')" id="pluginurl" style="color: #83AFE2;text-decoration:underline;"></a>
然后在js中:
function downloadtest(id){
var url = "<%=request.getContextPath()%>/app/download" + "/" + id;
$("#pluginurl").attr("href",url);
}
后台处理下载逻辑的java代码:
/**
* 下载文件
* @param id appid
* @param response
*/
@RequestMapping(value="/download/{id}")
public void download(@PathVariable String id, HttpServletResponse response){
String filepath = "";
Result result = appService.getAppById(id);
App app = (App) result.getMap().get("app");
if(app == null){
return;
}
filepath = app.getUrl();
File file = new File(filepath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b= new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
String filename = file.getName();
filename = filename.substring(36, filename.length());
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength( (int) file.length( ) );
while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一般采用href属性直接指向一个服务器地址,只要链接的文件存在,就会给出弹出保存对话框.
点击a标签 先执行onclick事件,再请求href中指向的地址。
前端jsp:
<a href="#" onclick="javascript:downloadtest('${app.id}')" id="pluginurl" style="color: #83AFE2;text-decoration:underline;"></a>
然后在js中:
function downloadtest(id){
var url = "<%=request.getContextPath()%>/app/download" + "/" + id;
$("#pluginurl").attr("href",url);
}
后台处理下载逻辑的java代码:
/**
* 下载文件
* @param id appid
* @param response
*/
@RequestMapping(value="/download/{id}")
public void download(@PathVariable String id, HttpServletResponse response){
String filepath = "";
Result result = appService.getAppById(id);
App app = (App) result.getMap().get("app");
if(app == null){
return;
}
filepath = app.getUrl();
File file = new File(filepath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b= new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
String filename = file.getName();
filename = filename.substring(36, filename.length());
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength( (int) file.length( ) );
while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
展开全部
在jsp页面上想把一个字符串写入到文本文件中,并且让用户下载,实现方法如下在网页页面上如下语句<a href="getData.jsp">下载数据</a>getData.jsp如下写<%@ page import="java.io.OutputStream" %><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><% String s = "data to be write in the txt file"; OutputStream os = response.getOutputStream(); response.setHeader("Content-disposition", "attachment;filename=" + "data.txt"); response.setContentType("application/txt"); byte[] byteDate = s.toString().getBytes(); os.write(byteDate); os.flush(); os.close();%>这样就行了.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |