这样修改试一试:
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download/*</url-pattern><!--here edited-->
</servlet-mapping>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download/*</url-pattern><!--here edited-->
</servlet-mapping>
解决方案 »
- “四人帮”的常规设计模式
- Hibernate
- web项目中dll文件问题
- 急!java 数据库字段中的文件下载问题!
- (急)RequestDispatcher转发的页面,无法使用css??
- jsp加密的问题
- 问个很弱智的问题在JSP中用到连接池是不是把DBConnect.class,DBConnectionManager.class,COPY就可以了
- 新手提问,关于servlet找不到的问题
- jbuilder怎样实现jsp???
- tomcat 的server.xml里的 <Engine name="Catalina" defaultHost="www.easymall.com">
- 字符型+1 在线等待,谢谢了...
- 一个小的WEB项目中的实现方法讨论
源代码:(提示这是 fuzhe(令狐虫) 在这转帖,和我修改一两个地方)import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Download extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//HttpServletRequestWrapper httpReqWrapper = (HttpServletRequestWrapper) request;
//HttpServletRequest httpRequest =(HttpServletRequest)httpReqWrapper.getRequest();
try {
downloadArtifact(request, response);
}catch (Exception e) {
e.printStackTrace();
RequestDispatcher dispatcher = this.getServletContext().
getRequestDispatcher("/Error.jsp");
dispatcher.forward(request, response);// httpServletRequest.setAttribute("message", "修改密码错误!");
// return (actionMapping.findForward("unknown-error"));
}
} public void downloadArtifact(HttpServletRequest request, HttpServletResponse response)
throws Exception {
Exception ex=null;
String fileId = (String)request.getParameter("ID");
String realFile = null;
String realFileName = "";
File uploadDir = new File("D:\\upload");
if (uploadDir.isDirectory()) {
File[] files = uploadDir.listFiles();
for (int j = 0; j < files.length; j++) {
File tempFile = files[j];
if ((tempFile.getName().indexOf(fileId)) >= 0){
//System.err.println("get The file" + tempFile.getName());
realFile = tempFile.getName();
break;
}
}
}
int seperatePos = realFile.lastIndexOf(".");
String fileAppendix = realFile.substring(seperatePos+1);
String tempFileName = realFile.substring(0,seperatePos);
int idIndex = tempFileName.lastIndexOf(fileId);
realFileName = tempFileName.substring(0,idIndex) + "." + fileAppendix; realFile = "emCnGoList.xls"; //add by ander rewrite the realFile File file = new File("D:\\upload", realFile);
if(!file.exists()) throw new Exception("Sorry, File Not Found");
int length=(new Long(file.length())).intValue();
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename=\""+realFileName+"\"");
//response.setHeader("Content-disposition", "attachment; filename=\""+file.getName()+"\"");
//System.err.println("******" + realFileName);
int bufferSize=1024;
BufferedOutputStream output = null;
BufferedInputStream input = null;
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
try {
int once = 0;
int total = 0;
byte[] buffer = new byte[bufferSize];
do {
once = input.read(buffer);
total += once;
if (once >= 0)
output.write(buffer, 0, bufferSize);
}
while ( (total < length) && (once >= 0));
response.flushBuffer();
}
catch (Exception e) {
ex = e;
} // maybe user cancelled download
finally {
if (input != null) input.close();
if(output!=null) output.close();
if(null!=ex) throw ex;
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}