你要下载的东西都不在web application的目录中怎么直接用链接.
如果你要把需要下载的东西放在web application以外的目录中实现下载,可以在服务器端向客户端发送流的方式。
设置响应头:response.ContentType="application/octet-stream";
设定响应头内容:response.AddHeader("Content-Disposition","attahment;filename="+java.net.UrlEncoder.encode(filename,"UTF-8"));
如果你要把需要下载的东西放在web application以外的目录中实现下载,可以在服务器端向客户端发送流的方式。
设置响应头:response.ContentType="application/octet-stream";
设定响应头内容:response.AddHeader("Content-Disposition","attahment;filename="+java.net.UrlEncoder.encode(filename,"UTF-8"));
解决方案 »
- spring properties问题,急~~~
- mysql是否支持blob 数据的 批处理插入,求教
- 给Spring的Bean配置增加新的属性,请问有何方案?
- <jsp:include 用什么注释
- java怎么引入下载的包 如SNMP4J包
- axis1.3下编写webservices的消息处理,很急
- 怎样控制struts对<,",>标记的自动转换功能呢?
- 关于带参数的JAVA调用浏览器
- 如何把一段XML发送出去,然后又能接受服务器回的XML
- 新人求助,有关使用redisTemplate缓存数据中文,结果不是二进制!!注意,不是二进制,而是正常显示中文...
- 100分寻求基于j2ee的项目需求文档
- struts中表单验证
我想下载.jar 文件 请问该如何解决?
<%@ page language="java" import="java.io.*"%>
<%!
public String getStr(String str)
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e){ e.printStackTrace();}
return "";
}
%><%
String szRoot=(String)session.getAttribute("rootpath"); //設定為你的目錄
if (szRoot==null ||szRoot.equals(""))
szRoot=application.getRealPath("/"); //取得根文件目錄String szSourceFile =getStr(request.getParameter("filename"));
if(szSourceFile==null || szSourceFile.equals("")||szPath==null){
response.sendRedirect("err.jsp");
return;
}String userpath=szRoot;//application.getRealPath("/");
userpath= userpath + "\\" + szSourceFile; //取得絕對路徑
//System.out.println("DOWNLOAD FILE:" + userpath);try{
InputStream inStream=new FileInputStream(userpath);
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + szSourceFile + "\"");
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0) response.getOutputStream().write(b,0,len);
inStream.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
%>應用:
downfile.jsp?filename=xxxx