springMVC如何实现文件的下载? springmvc服务器计算机 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个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; } } 这些在网上找也很好找的 //下载方法 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(); } } } } 下载文件,跟springmvc struts2 没关系,既然有路径了,路径对应的文件也有, 你为何不直接href=文件的路径, 然后就直接提示下载了 直接写到respone body里面不就拉倒啦,说实话文件上传下载跟用什么框架毛关系都没有,都是靠requestbody与reponsebody懂HTTP协议就okie 下载下来的文件怎么会已损坏,无法打开?亲,知道什么问题吗?给了setContentType("image/jpeg");也不行,知道什么原因吗? 谢谢各位,可能是基础太差,以前都是用struts2封装好的东西,自己写的时候就出问题了,问题解决,结贴给分。 关于jquery树形控件的问题 菜鸟求教js弹出式菜单代码详解 JSF h:commandLink 提交后只刷新页面不执行绑定的后台方法 问哥哥姐姐们一个问题、通用。过来看看 怎么才能点击这个 button ,新开一个页面呢? WebService --- 急等 google搜索原理 jsp页调用java系统类的问题 一个关于applet存放路径的问题! 请问成批提交数据如何实现?(一定加分) Porlet 开发,求软件安装包 做了一个学校给的CRM项目 里面有iframe request的值如何显示在top.jsp上啊?
//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;
}
}
这些在网上找也很好找的
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();
}
}
}
}
既然有路径了,路径对应的文件也有, 你为何不直接href=文件的路径, 然后就直接提示下载了
文件上传下载跟用什么框架毛关系都没有,都是靠requestbody与reponsebody
懂HTTP协议就okie