/**
* 下载文件 处理下载中心的数据包
*
* @author wangrr
*/
protected void DownLoadFile(HttpServletResponse response ,HttpServletRequest request, String Path)
throws IOException
{
String filepath= Path.substring(0,Path.lastIndexOf("/")+1);
String filename = Path.substring(Path.lastIndexOf("/")+1);
//解决中文文件名乱码问题(火狐,ie,谷歌都兼容)
String fileName = java.net.URLEncoder.encode(filename, "UTF-8");
if (fileName.length() > 150) {
String guessCharset ="gb2312"; /*根据request的locale 得出可能的编码,中文操作系统通常是gb2312*/
fileName = new String(filename.getBytes(guessCharset), "ISO8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.setHeader("Connection", "close");
ServletOutputStream sos = response.getOutputStream();
FileInputStream fis = null;
File d = new File(filepath);
if (d.exists())
{
fis = new FileInputStream(filepath+filename);//
byte b[] = new byte[1000];
int j;
while ((j = fis.read(b)) != -1)
{
try
{
sos.write(b, 0, j);
}
catch (IOException exp)
{
}
}
fis.close();
sos.flush();
sos.close();
}
}