解决方案 »
- SSH的一个疑问
- swfupload 上传问题
- [求助]javaBean报异常
- 欢迎加入java高级群:84459794 不怕你来,就怕你错过!
- jsp传值问题
- 用commons.fileupload控件已经实现上传功能,但是在局域网内上传速度与外网的上传速度一样,想让在局域网内上传速度更快一些
- 请大虾介绍一下目前比较流行的jsp服务器
- ‘4e0076f45f3a9000554aff01706b5927ff01’这串字符串属于什么编码啊,能反解为原来的内容吗?
- 所有SSH项目运行都是没错的,但发送action请求的时候都出错
- spring 配置问题
- sql server多表查询 habernate的级联查询
- quartz的工作流程 有大神知道不???
response.setContentType("application/vnd.ms-excel"); //改成输出excel文件
response.setHeader("Content-disposition","attachment; filename=" + 文件名 );
点击导出按钮之后你可以先ajax请求后台写好文件,然后再实现文件的下载就OK了。这样是可以弹窗提示选择下载路径的
写文件楼主应该是会了,下载功能建议楼主百度,一堆资料、代码,一个Servlet就能搞定。
我再说直白一些吧 Servlet不是有out.print()吗,输出文件流的话就是文件传输了,在页面上会自动弹出下载提示窗口,然后浏览器会帮你把文件存到你想要的位置。你需要做的就是写好文件然后做一个输出文件的Servlet
你这是B/S程序,还是C/S程序?
如果是B/S程序,可以参考1楼的
如果是C/S程序,楼上的暂时没有正解,可以参考楼下的
Annex annex=annexService.find(Annex.class, id);
File file = new File(getFileBasePath() + annex.getUrl());
File file2=new File(getFileBasePath() + File.separator+"download"+File.separator+DecoderUtil.UtfDecoder(annex.getName()));
file2.getParentFile().mkdir();
FileUtil.copyFile(file, file2);
try {
this.setFileName(new String(file2.getName().getBytes(), "ISO8859-1"));
tempStream = new java.io.FileInputStream(file2);//从系统磁盘文件读取数据
bytes = new byte[tempStream.available()];
if(tempStream != null) {
tempStream.read(bytes);
}
tempStream.close();
return new ByteArrayInputStream(bytes);
} catch(Exception e) {
return null;
}finally{
FileUtil.deleteFile(file2.toString());
}
}这个是下载的action,我写的一个简单的东西
/**
* wangjiafeng
* TODO 导出自行车电动车信息Excel
* @method educeExcel
* @return
* @throws Exception
* 2013 上午10:20:25
*/
public String educeEleCarExcel() throws Exception{
Member member=getMember();
if(member != null ){
Date date=new Date(System.currentTimeMillis());
String test=DateUtil.parseDateToString(date, "yyyy-MM-ddHH:mm:ss").replace("-", "").replace(":", "").trim();
String path = request.getSession().getServletContext().getRealPath("/") + "ElectCarExcel"+test+".xls";
List<ElectCarInfor> list=electCarInforService.queryByAdminAndFrontList(DecoderUtil.UtfDecoder(carriageId), DecoderUtil.UtfDecoder(electCarId),
DecoderUtil.UtfDecoder(brand), DecoderUtil.UtfDecoder(model), DecoderUtil.UtfDecoder(name),
DecoderUtil.UtfDecoder(personid), DecoderUtil.UtfDecoder(phoneNum1),robTime,DecoderUtil.UtfDecoder(robPlace),
DecoderUtil.UtfDecoder(regUnit),regDate, flag, null, null);
if(list != null && list.size() > 0){
ExcelUtil excelUtil=new ExcelUtil();
excelUtil.createExcel(path, list);
excelUtil.downLoadExcel(response, path);
FileUtil.deleteFile(path);
}
return null;
}else{
return "memberLogin";
}
public void downLoadExcel(HttpServletResponse response,String filePath ) throws IOException {
File f = new File(filePath);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0; response.reset(); // 非常重要 response.setContentType("application/xls;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.flush();
out.close();
}