springMVC如何实现文件的下载?

坚持2012 2013-09-22 04:07:53
我的数据库里一张表保存三个参数,分别是:id,name(文件名),path(在服务器上的文件路径);
此时对应的服务器上有对应的文件。
我要把它下载到本地计算机上,不用struts2,下载代码怎么写,controller应该怎么写?如何能实现和struts2下载功能一样的效果。
刚刚接触springMVC,以前都是用struts2的,现在用springMVC就不会了,哪位大哥给指点一下,最好能附上代码,谢谢,分会全部奉上
...全文
42976 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
y465029816 2014-05-16
  • 打赏
  • 举报
回复
怎么解决的 求指导
gloomyfish 2013-09-23
  • 打赏
  • 举报
回复
直接写到respone body里面不就拉倒啦,说实话 文件上传下载跟用什么框架毛关系都没有,都是靠requestbody与reponsebody 懂HTTP协议就okie
田小瘦 2013-09-23
  • 打赏
  • 举报
回复
下载文件,跟springmvc struts2 没关系, 既然有路径了,路径对应的文件也有, 你为何不直接href=文件的路径, 然后就直接提示下载了
kuailexiaobuding 2013-09-23
  • 打赏
  • 举报
回复
//下载方法 private void downloadFile(File file){ String fileName = file.getName(); try { fileName = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } contextPvd.getResponse().setContentType("application/zip"); contextPvd.getResponse().addHeader("Content-Disposition", "attachment;filename=" + fileName); OutputStream outp = null; FileInputStream in = null; try { outp = contextPvd.getResponse().getOutputStream(); in = new FileInputStream(file); byte[] b = new byte[1024]; int i = 0; while ((i = in.read(b)) > 0) { outp.write(b, 0, i); } outp.flush(); } catch (Exception e) { //log.error("", e); } finally { if (in != null) { try { in.close(); in = null; } catch (IOException e) { e.printStackTrace(); } } if (outp != null) { try { outp.close(); outp = null; } catch (IOException e) { e.printStackTrace(); } } } }
咖啡加糖_ 2013-09-23
  • 打赏
  • 举报
回复
这个SpringMVC没什么关系吧,我在jsp中做过一个图片的下载,你可能要修改下代码 response.setContentType("application/x-download"); //application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径 String filedownload = request.getRealPath("/")+"/organization/img/"+"组织架构图.png"; String filedisplay = "组织架构图.png"; filedisplay = URLEncoder.encode(filedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachment;filename=" + filedisplay); java.io.OutputStream outp = null; java.io.FileInputStream in = null; try { outp = response.getOutputStream(); in = new java.io.FileInputStream(filedownload); byte[] b = new byte[1024]; int i = 0; while((i = in.read(b)) > 0) { outp.write(b, 0, i); } outp.flush(); //要加以下两句话,否则会报错 //java.lang.IllegalStateException: getOutputStream() has already been called for this respons out.clear(); out = pageContext.pushBody(); } catch(Exception e) { System.out.println("Error!"); e.printStackTrace(); } finally { if(in != null) { in.close(); in = null; } if(in != null) { in.close(); in = null; } } 这些在网上找也很好找的
坚持2012 2013-09-23
  • 打赏
  • 举报
回复
谢谢各位,可能是基础太差,以前都是用struts2封装好的东西,自己写的时候就出问题了,问题解决,结贴给分。
坚持2012 2013-09-23
  • 打赏
  • 举报
回复
引用 2 楼 jimmy609 的回复:
这个SpringMVC没什么关系吧,我在jsp中做过一个图片的下载,你可能要修改下代码 response.setContentType("application/x-download"); //application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径 String filedownload = request.getRealPath("/")+"/organization/img/"+"组织架构图.png"; String filedisplay = "组织架构图.png"; filedisplay = URLEncoder.encode(filedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachment;filename=" + filedisplay); java.io.OutputStream outp = null; java.io.FileInputStream in = null; try { outp = response.getOutputStream(); in = new java.io.FileInputStream(filedownload); byte[] b = new byte[1024]; int i = 0; while((i = in.read(b)) > 0) { outp.write(b, 0, i); } outp.flush(); //要加以下两句话,否则会报错 //java.lang.IllegalStateException: getOutputStream() has already been called for this respons out.clear(); out = pageContext.pushBody(); } catch(Exception e) { System.out.println("Error!"); e.printStackTrace(); } finally { if(in != null) { in.close(); in = null; } if(in != null) { in.close(); in = null; } } 这些在网上找也很好找的
下载下来的文件怎么会已损坏,无法打开?亲,知道什么问题吗?给了setContentType("image/jpeg");也不行,知道什么原因吗?
坚持2012 2013-09-22
  • 打赏
  • 举报
回复
亲们,分少了我可以再加的,别让帖子沉了好吗?拜托......

81,121

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧