servlet实现文件下载的实用类分享
package com;
import java.io.ioexception;
import java.io.printwriter;
import java.net.urlencoder;
import java.util.date;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.mimeutility;
/**
* 文件下载类。为了防止客户端浏览器直接打开目标文件(例如在装了ms office套件的windows中的ie浏览器可能就会直接在ie浏览器中打开你想下载的doc或者xls文件),在响应头里加入强制下载的mime类型。
*/
public class downloadfile extends httpservlet {
private static final log log = logfactory.getlog(downloadfile.class);
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception {
long timestart = 0;
if(log.isdebugenabled()){
timestart=system.currenttimemillis();
}
response.setcontenttype("application/x-download charset=utf-8");
java.io.fileinputstream fis = null;
string filepath = request.getrealpath("");
javax.servlet.servletoutputstream sos = null;
// system.out.println("downloadfile filename:" + filename);
try {
if(request.getparameter("filename")==null
||request.getparameter("showname")==null){
return;
}
string filename = request.getparameter("filename");
string showname = request.getparameter("showname");
request.setcharacterencoding("utf-8");
response.setcharacterencoding("utf-8");
java.io.file file = new java.io.file(filepath + filename);
if (!file.exists()) {
log.error(file.getabsolutepath() + " 文件不存在!");
return;
}
// 读取文件流
fis = new java.io.fileinputstream(file);
// 设置下载保存的文件名
sos = response.getoutputstream();
showname += filename.substring(filename.lastindexof("."));
string contentdisposition = "", browser = getbrowser(request);
if ("ie".equals(browser)) {
contentdisposition = "attachment; filename=" + urlencoder.encode(showname, "utf-8").replace("+", "%20");
} else if ("ch".equals(browser)) {
contentdisposition = "attachment; filename=" + mimeutility.encodetext(showname, "utf8", "b");
} else if ("sf".equals(browser)) {
contentdisposition = "attachment; filename=" + new string(showname.getbytes("utf-8"), "iso8859-1");
} else {
contentdisposition = "attachment; filename*=utf-8''" + urlencoder.encode(showname, "utf-8").replace("+", "%20");
}
response.setheader("content-disposition", contentdisposition);
int bytecount = 0;
if (fis != null) {
byte[] buff = new byte[1024];
int bytesread;
while(-1 != (bytesread = fis.read(buff, 0, buff.length))) {
sos.write(buff, 0, bytesread);
sos.flush();
bytecount += bytesread;
}
}
sos.flush();
if(log.isdebugenabled()){
log.debug("文件下载完成,文件大小:"+ bytecount +",总共用时:"+ (new date().gettime() - timestart) +"毫秒。");
}
} catch(ioexception ioe) {
ioe.printstacktrace();
} finally {
try {
if(fis!=null){
fis.close();
}
} catch (ioexception e) {
} finally {
try {
if(sos!=null){
sos.close();
}
} catch (ioexception e) {
}
}
}
}
public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype("text/html");
printwriter out = response.getwriter();
out.println("<!doctype html public \"-//w3c//dtd xhtml 1.0 transitional//en\" \"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\">");
out.println("<html>");
out.println(" <head>");
out.println(" <title>文件下载</title>");
out.println(" <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
out.println(" </head>");
out.println(" <body>");
out.print(" this is ");
out.print(this.getclass().getname());
out.println(", using the post method");
out.println(" </body>");
out.println("</html>");
out.flush();
out.close();
}
private string getbrowser(httpservletrequest request) {
string useragent = request.getheader("user-agent").tolowercase();
if (useragent != null) {
if (useragent.indexof("msie") >= 0) {
return "ie";
} else if (useragent.indexof("mozilla") >= 0) {
return "ff";
} else if (useragent.indexof("applewebkit") >= 0) {
return "ch";
} else if (useragent.indexof("safari") >= 0) {
return "sf";
} else if (useragent.indexof("opera") >= 0) {
return "op";
}
}
return null;
}
}
上一篇: CAD怎么调整界面比例?
下一篇: Maya怎么制作路径动画?