JSP页面怎么实现超链接定向绝对路径实现下载功能

zjq409421884 2012-06-29 08:26:26
现在我做了一个下载功能,超链接实现下载,但是貌似只能相对路径,绝对路径的不行,部分代码如下
页面超链接重定向:<a href="pages/download.jsp?fpath=/imgs/bg.jpg&fname=bg.jpg">xia</a>
下载页面:
<% response.setContentType("application/x-download");//设置为下载application/x-download
String fpath =(String)request.getParameter("fpath");
String fname =(String)request.getParameter("fname");
String filedownload =fpath;//即将下载的文件的相对路径

String filedisplay =fname;//下载文件时显示的文件保存名称

response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

try { RequestDispatcher dis = application.getRequestDispatcher(filedownload);

if(dis!= null) { dis.forward(request,response);

}

response.flushBuffer();

}

catch(Exception e)

{

e.printStackTrace();

}

finally { }

%>

但是我改成绝对路径就不行了,谁能帮我改下,实现绝对路径的下载
...全文
440 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjq409421884 2012-07-02
  • 打赏
  • 举报
回复
已解决

<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>
<%@page import="java.io.*"%>
<%
//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;

response.reset();//可以加也可以不加
response.setContentType("application/x-download");

//application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径

String filedownload = "E:/download/1.pptx";
String filedisplay = "1.pptx";

response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);

byte[] b = new byte[1024];
int i = 0;

while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
//
outp.flush();
//要加以下两句话,否则会报错
//java.lang.IllegalStateException: getOutputStream() has already been called for //this response
out.clear();
out = pageContext.pushBody();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
//这里不能关闭
//if(outp != null)
//{
//outp.close();
//outp = null;
//}
}
%>

zjq409421884 2012-07-02
  • 打赏
  • 举报
回复
有没有人帮我改下的,,先谢了
zjq409421884 2012-07-02
  • 打赏
  • 举报
回复
<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>

<%

String filename=request.getParameter("student.pdf");//传递过来的 文件名如:11.doc
int type=Convert.parseToInt32(request.getParameter("type"),0);

String folder="";
if(type==1)
{
folder="law";
}
else
{
folder="std";
}
out.print(folder);
File txtfile=new File(AppConfig.getDataDirBase()+"app_water_mon/archive/"+folder+"/index.txt");
String filekuozhan="pdf";//文件的扩展名 如doc txt rar
String filetrue="student"; //扩展名前面部分 如11.doc,22.txt,33.rar中的11,22,33
if(filename!=null)
{
StringTokenizer kz= new StringTokenizer(filename,".") ;
filetrue=kz.nextToken();
filekuozhan=kz.nextToken();
//out.print(filekuozhan);
}
String title=request.getParameter("filetitle");
out.print(title);
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String filedownload =filename;//文件名
String filedisplay = title+"."+filekuozhan;//下载文件时显示的文件保存名称
String filenamedisplay =java.net.URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
//文件的路径 如:d:/app_water_mon/archive/law/11.doc
File truefile=new File(AppConfig.getDataDirBase()+"download\"+folder+"\"+filename);
in = new FileInputStream(truefile);

byte[] b = new byte[1024];
int i = 0;

while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
outp.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}%>

这个是我写的IO流,,但是总是报

An error occurred at line: 6 in the jsp file: /pages/download2.jsp
Convert cannot be resolved
zjq409421884 2012-06-30
  • 打赏
  • 举报
回复
用IO流的方式,要走sevlet么
brightyq 2012-06-30
  • 打赏
  • 举报
回复
如果要用绝对路径有个问题,你想下,jsp页面发布到网上,别人下载时通过你设置的相对路径没有问题,因为相对于你web应用的路径再往下找;如果是绝对路径,比如C盘的某个路径下某个文件,那用户下载是去自己本地的C盘去找了,那当然找不到要下载的文件。
如果用绝对路径,就有IO流的方式,把你的绝对路径处理一下。
zjq409421884 2012-06-30
  • 打赏
  • 举报
回复
怎么还没有人啊,,有没有高手帮我看看内
brightyq 2012-06-30
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100730/17/c3b87d77-3f13-45de-86bf-0105b9427d03.html
zjq409421884 2012-06-29
  • 打赏
  • 举报
回复
有没有人帮我看看啊

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧