/** Created on 2004-12-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates*/
packagecom.zl.common;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjavax.activation.FileTypeMap;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.cd.util.ReadPropertiesFile;importcom.zl.base.core.Constants;public class downloadfile extendsHttpServlet {/****/
private static final long serialVersionUID = 1L;/*** Default constructor.*/
publicdownloadfile() {super();
}/*** Init method.
*
*@throwsServletException
* never.*/
public void init() throwsServletException {return;
}public void service(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
service(request, response,true);
}public void service(HttpServletRequest request, HttpServletResponse response, booleanblnsyn)throwsServletException, IOException {try{
Object user=request.getSession().getAttribute(Constants.SESSION_USER);if (user != null) {
String mimeType= request.getParameter("mimeType");
String filename= request.getParameter("filename");
String oldfilename= request.getParameter("saveasfilename");if (mimeType!=null) response.setContentType(mimeType);if (oldfilename == null) {
response.setHeader("Content-Disposition", "attachment; filename=" +filename);
}else{
oldfilename= new String(oldfilename.getBytes("ISO-8859-1"),"UTF-8");;
response.setHeader("Content-Disposition", "attachment; filename="+oldfilename );//response.setHeader("Content-Disposition", "attachment;//filename=" +oldfilename);
}
String uploadfilepath= ReadPropertiesFile.getInstance().getProperty("uploadfilepath", "c:/") + "announce/";
File file= new File(uploadfilepath +filename);
System.out.println("文件路径:"+uploadfilepath +filename);if (!file.exists()) {
file.createNewFile();
}
FileTypeMap fMap=FileTypeMap.getDefaultFileTypeMap();
String filetype=fMap.getContentType(file);
response.setContentType(filetype);
response.setContentLength((int) file.length());
BufferedOutputStream bos= newBufferedOutputStream(response.getOutputStream());
BufferedInputStream bis= new BufferedInputStream(newFileInputStream(file));byte[] input = new byte[1024];boolean eof = false;while (!eof) {int length =bis.read(input);if (length == -1) {
eof= true;
}else{
bos.write(input,0, length);
}
}
bos.flush();
bis.close();
bos.close();
}
}catch(Exception ex) {
}return;
}
}