String absPath = "D:/kqtj.xls";//文件的路径
String fileName = "kqtj.xls";//文件名
try {
fileName = new String(fileName.getBytes("ISO-8859-1"),"GBK");
absPath = new String(absPath.getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
File f = new File(absPath);
InputStream is = new FileInputStream(f);
OutputStream os=response.getOutputStream();;
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos =new BufferedOutputStream(os);
response.reset();
response.setCharacterEncoding("GBK");
//response.setContentType("application/x-msdownload");//纯下载
response.setContentType("application/msword");
/*pdf response.setContentType("application/pdf");//
html response.setContentType("text/html;charset=gbk");设置charset,文件的字符编码*/
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
int bytesRead = 0;
byte[] buffer = new byte[(int)f.length()];
while ((bytesRead = bis.read(buffer)) != -1)
{
bos.write(buffer, 0, bytesRead);//buffer read
}
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
} catch (FileNotFoundException es) {
es.printStackTrace();
}catch(IOException ed){
ed.printStackTrace();
}
request.setAttribute("list", list);
return null;
}