我刚写过,记得给分哦!呵呵。 //download.jsp <%@ page import="java.util.*,java.io.*"%> <% //read the file name. String dep_Id = request.getParameter("dep_Id"); String project_Id = request.getParameter("project_Id"); String folder_Id = request.getParameter("folder_Id"); //out.println(folder_Id); String filename = request.getParameter("filename"); String sep = System.getProperty("file.separator"); String PATHSTRING = request.getRealPath("/Document") + sep + dep_Id +sep + project_Id + sep + folder_Id + sep + filename; //out.println(">>"+PATHSTRING+"<<"); File f = new File(PATHSTRING); if(!f.exists() || !f.isFile()) {out.println("sorry,file not exists.");return;} //set the content type(can be excel/word/powerpoint etc..) response.setContentType ("application/octet-stream"); //set the header and also the Name by which user will be prompted to save response.setHeader ("Content-Disposition", "attachment;filename=\""+filename+"\""); //get the file name // String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length()); //OPen an input stream to the file and post the file contents thru the //servlet output stream to the client m/c InputStream in = new FileInputStream(f); ServletOutputStream outs = response.getOutputStream(); int bit = 256; int i = 0; try { while ((bit) >= 0) { bit = in.read(); outs.write(bit); } //System.out.println("" +bit); } catch (IOException ioe) { ioe.printStackTrace(System.out); } // System.out.println( "\n" + i + " byt // es sent."); // System.out.println( "\n" + f.length( // ) + " bytes sent."); outs.flush(); outs.close(); in.close(); %>
一般都会把文件下载写在servlet里面的,点页面上的连接后调用Servlet,实现文件下载。
在Servlet里面通常这样写:
………………
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition",
"attachment; filename=" + URLUTF8Encoder.encode(fileName));
out = response.getOutputStream(); out.write(fileInfo.getFileData(), 0, fileInfo.getFileSize());
………………
不、会用winzip压缩一下吗???缩短了用户的下载时间,有解决了问题!!!!
//download.jsp
<%@ page import="java.util.*,java.io.*"%>
<%
//read the file name.
String dep_Id = request.getParameter("dep_Id");
String project_Id = request.getParameter("project_Id");
String folder_Id = request.getParameter("folder_Id");
//out.println(folder_Id);
String filename = request.getParameter("filename");
String sep = System.getProperty("file.separator");
String PATHSTRING = request.getRealPath("/Document") + sep + dep_Id +sep + project_Id + sep + folder_Id + sep + filename;
//out.println(">>"+PATHSTRING+"<<"); File f = new File(PATHSTRING);
if(!f.exists() || !f.isFile()) {out.println("sorry,file not exists.");return;}
//set the content type(can be excel/word/powerpoint etc..)
response.setContentType ("application/octet-stream");
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=\""+filename+"\""); //get the file name
// String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
//OPen an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
//System.out.println("" +bit);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
// System.out.println( "\n" + i + " byt
// es sent.");
// System.out.println( "\n" + f.length(
// ) + " bytes sent.");
outs.flush();
outs.close();
in.close();
%>
1、设置contentType。response.setContentType ("application/octet-stream");
2、设置Desciption。
3、指定下载地址。