public ActionForward Download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("APPLICATION/vnd.ms-excel"); ............................//这部分是生成excel文件,文件能正常生成 String filename = sFileName;
filename = URLEncoder.encode(filename, "GBK"); response.setHeader("Content-Disposition", "attachment;filename=\""
+ filename + "\"");
try {
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(sCurrPath);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(os); int r = 0;
byte[] ba = new byte[4096];
while ((r = bis.read(ba)) != -1) {
bos.write(ba, 0, r);
bos.flush();
}
bos.close();
bis.close();
fis.close();
} catch (Exception e) {
log.debug("OutputStream exception");
} iFile = new File(sCurrPath);
if (iFile.exists()) {
iFile.delete();
} else {
log.debug("File not Exsits");
} logger.debug("Down Excel OK ");
return null;
}文件正常生成,但是下载不下来,也根本就没有提示保存框出来.
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("APPLICATION/vnd.ms-excel"); ............................//这部分是生成excel文件,文件能正常生成 String filename = sFileName;
filename = URLEncoder.encode(filename, "GBK"); response.setHeader("Content-Disposition", "attachment;filename=\""
+ filename + "\"");
try {
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(sCurrPath);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(os); int r = 0;
byte[] ba = new byte[4096];
while ((r = bis.read(ba)) != -1) {
bos.write(ba, 0, r);
bos.flush();
}
bos.close();
bis.close();
fis.close();
} catch (Exception e) {
log.debug("OutputStream exception");
} iFile = new File(sCurrPath);
if (iFile.exists()) {
iFile.delete();
} else {
log.debug("File not Exsits");
} logger.debug("Down Excel OK ");
return null;
}文件正常生成,但是下载不下来,也根本就没有提示保存框出来.
所有log信息也都能正常输出
File file=new File(path);
if(file.exists()){
String fileName=file.getName();
InputStream is=new FileInputStream(file);
OutputStream os=response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1){
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
}
return null;