jsp下载文件的问题! 怎么没有人理我啊!麻烦大家帮帮忙啊!很急! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 path应该是E:\\ebook,另外我觉得在jsp中写代码是个很不好的行为,会使你的代码难于维护,并且可能会引起并发访问方面的问题,写到servlet中是比较好的方法,只需要你在jsp中向servlet传递一个下载文件的id就可以了,这里有一些代码你看看吧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 x) { x.printStackTrace(); RequestDispatcher dispatcher = this.getServletContext(). getRequestDispatcher("/common/ShowError.jsp"); dispatcher.forward(request, response); } } public void downloadArtifact(HttpServletRequest request, HttpServletResponse response) throws Exception { Exception ex=null; String fileId = (String)request.getParameter("ID"); String realFile = null; 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; } } } 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=\""+file.getName()+"\""); 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); } 在JSP页面中上传图片,根据选中List中的一行来获取那一行的ID doPost doGet如何返回,使用重定向吗?还是用return? 获取JSP中的数据! java编程是不是每一种技术的生命期都很短? Hibernate问题 ! 急急! 新手提问:记录集中,上一条记录怎么去不过来?谢谢 怎样import一个类 简单问题:jsp页面怎样实现上传整个目录? 关于beans 用jsp那个函数可以得到用户的IP地址,急救。谢谢!!!!! 100分紧急求救,请各位大侠提供一个能用的页面上录音的控件 Structs 中如何读取配置文件 急急急!
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 x) {
x.printStackTrace();
RequestDispatcher dispatcher = this.getServletContext().
getRequestDispatcher("/common/ShowError.jsp");
dispatcher.forward(request, response);
}
}
public void downloadArtifact(HttpServletRequest request, HttpServletResponse response) throws Exception {
Exception ex=null;
String fileId = (String)request.getParameter("ID");
String realFile = null;
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;
}
}
}
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=\""+file.getName()+"\"");
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);
}