更改传输格式,不使用text/html格式,而是根据文件类型判断,如:
String filepath = StringUtils.trim(request.getParameter("filepath"));
if (filepath.equals(""))
return null; File file = new File(getRealFilepath(filepath)); OutputStream out = response.getOutputStream(); int l = file.getName().lastIndexOf("."); //the file suffix
String ext = ".txt";
if (l >= 0) {
ext = file.getName().substring(l).toLowerCase();
setContentType(response, ext);
} else {
response.setContentType("text/plain");
} String filename = file.getName();
filename = new String(filename.getBytes(), "8859_1");
response.setHeader(
"Content-Disposition",
"attachment;filename=\"" + filename + "\""); BufferedInputStream reader =
new BufferedInputStream(new FileInputStream(file));
//here use buffer,it can be faster then only use FileInputStream. try {
byte[] b = new byte[1024];
int len = 0;
while ((len = reader.read(b, 0, 1024)) > 0) {
out.write(b, 0, len);
}
out.flush();
} finally {
reader.close();
out.close();
}
return null;