永发信息网

jsp实现下载功能

答案:2  悬赏:80  手机版
解决时间 2021-02-26 09:23
  • 提问者网友:难遇难求
  • 2021-02-25 22:53
我目前的设计是用hibernate查询数据库的下载地址和文件名字,然后struts2将路径和文件名字传送给jsp,由jsp进行下载,现在我已经能查询到文件路径和名字了,但是我不知道怎么传送给jsp,更不知道jsp怎么处理。下载,求指点,只用了hibernate和struts2

我能查到的信息有使用servlet的,但是我忘了怎么搞了,顺便说下很残酷的事实,就是struts2我只会配置web.xml了,struts2.xml里面的东西我不知道怎么配置了。能顺便说下更好,这边的财富值我还是有的
最佳答案
  • 五星知识达人网友:行路难
  • 2021-02-25 23:58
你的下载是一个超链接,你应该下载,你只能同过超链接跳到Action然后Action里面写一些东西,我这是用Struts写的你可以参考一下:
public ActionForward fileDownLoad(ActionMapping mapping,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
String fileName = null;// 名称

String realpath = "D:/crmSite/cdoc/";//定义路径

realpath = "D:/crmSite/cdoc/"
+ adform.getAdMat().substring(1);

realpath = StrUtils.replace(realpath, "\\", "/");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
fileName = realpath.substring(realpath.lastIndexOf("/") + 1, realpath.length());
//System.out.println(realpath);
try {
response.setContentType(this.getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="
+ fileName);
fis = new FileInputStream(realpath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.close();
bis.close();
fos.close();
fis.close();
} catch (IOException e) {
response.reset();
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
System.err.print(e);
}
}
return null;
}
是否可以解决您的问题?
全部回答
  • 1楼网友:西岸风
  • 2021-02-26 00:15
1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不写示例了。 2.在服务器端把文件转换成输出流,写入到response,以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本地。(示例如下) <% response.setcontenttype(fileminitype); response.setheader("location",filename); response.setheader("cache-control", "max-age=" + cachetime); //filename应该是编码后的(utf-8) response.setheader("content-disposition", "attachment; filename=" + filename); response.setcontentlength(filelength); outputstream outputstream = response.getoutputstream(); inputstream inputstream = new fileinputstream(filepath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputstream.read(buffer)) != -1) { outputstream.write(buffer, 0, i); } outputstream.flush(); outputstream.close(); inputstream.close(); outputstream = null; %> 3.既然是jsp的话,还有一种方式就是用applet来实现文件的下载。不过客户首先得信任你的这个applet小程序,由这个程序来接受由servlet发送来的数据流,并写入到本地。 servlet端示例 public void service(httpservletrequest req, httpservletresponse res) throws servletexception, ioexception { res.setcontenttype(" text/plain "); outputstream outputstream = null; try { outputstream = res.getoutputstream(); //把文件路径为srcfile的文件写入outputstream中 popfile(srcfile, outputstream)) ; } catch (ioexception e) { e.printstacktrace(); } } japplet端示例 urlconnection con; try { //url是被调用的servlet的网址 如 *.do con = url.openconnection(); con.setusecaches(false); con.setdoinput(true); con.setdooutput(true); con.setrequestproperty("content-type", "application/octet-stream"); inputstream in = con.getinputstream(); progressmonitorinp
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯