现在我做了一个下载功能,超链接实现下载,但是貌似只能相对路径,绝对路径的不行,部分代码如下
页面超链接重定向:<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 { }%>但是我改成绝对路径就不行了,谁能帮我改下,实现绝对路径的下载
页面超链接重定向:<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 { }%>但是我改成绝对路径就不行了,谁能帮我改下,实现绝对路径的下载
如果用绝对路径,就有IO流的方式,把你的绝对路径处理一下。
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
<%@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;
//}
}
%>