需要用"window.location.href"这种get请求方式请求下载方法
解决方案 »
- JSF Facelets and the Flying Saucer Renderer 例子
- 门户网站
- <form:input 无法显示html语句的问题
- 怎样得到未读邮件的个数!我只要得到未读邮件的个数就行了!怎么弄!
- JSP与ORACLE简单的问题,请前辈帮帮忙,多谢多谢!!!!!!!!!!!!!!!!!!!!!!!!!
- java群
- 请教html中<a href=""></a> 怎么跳转到“http://www.baidu.com”
- 急!乱码问题!快要自杀了!!!好心人帮一下
- JSP与JavaScript交互问题, 请高手进3
- JS 回调
- POI 读取不了含有级联下拉框的excel
- 报500错误,求教啊~~
走完 bos.flush(); 这一步,客户端应该出现弹出框
代码走完,服务器断开与客户端的tcp连接时,客户端开始解析下载文件如果断点没走到后台,那就检查下请求下载的方式,看是否请求方式有问题
1.你到tomcat下面看看文件生成没有
2.如果没有生成文件就是你的写入有问题
3.如果你生成了下载的文件 那就是你没有弹出框 建议 使用
OutputStream out = response.getOutputStream();
response.reset();
response.setHeader("content-disposition","attachment;filename=demo.xls");
response.setContentType("application/msexcel");
WritableWorkbook wwb = WritableWorkSheet.createWorkBook(out);
WritableSheet sheet = wwb.createSheet("123",0);
//写入数据
out.flush();
out.close();
我以前也遇到过,我是用的是 window.location.href ajax的方法结果不可以 ajax没试过 不知道可以不
String path=ConstanData.LOAD_EXCEL;
try
{
File file = new File(path);
if(!file.getParentFile().exists()){
file.mkdirs();
}
path = path+ "维保订单.xls";
File file2 = new File(path);
FileOutputStream fout = new FileOutputStream(file2);
wb.write(fout);
fout.close();
DownloadTool downloadtool = new DownloadTool();
response = ServletActionContext.getResponse();
request = ServletActionContext.getRequest();
// downloadtool.resetResponse(request, response, path);
downloadtool.download(request, response, path);
// downloadtool.download2(file2, response);
}
catch (Exception e)
{
e.printStackTrace();
}//从服务器下载文件到本地机器
public void download(HttpServletRequest request,HttpServletResponse response,String location) {
try {
String fileName = location.substring(location.lastIndexOf("\\")+1);//得到文件名
File file = new File(location);
if(file.exists()){
ServletOutputStream out = response.getOutputStream();
response.reset();
response.setContentType("application/x-download");//设置为下载application/x-download
fileName=new String(fileName.getBytes("GBK"),"ISO-8859-1");
// response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
response.setHeader("Content-Disposition","attachment;filename=" + fileName.substring(fileName.lastIndexOf("/")+1, fileName.length()));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(location));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[10240];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff))) {
bos.write(buff);
}
bos.flush();
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}