springMVC 文件下载
2015-06-02 09:06
393 查看
springmvcdownload文件下载
[java]
view plaincopyprint?
package com.clj.test.down.util; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.context.annotation.Scope; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; /** * <一句话功能简述> * <功能详细描述> * * @author Administrator * @version [版本号, 2014年3月7日] * @see [相关类/方法] * @since [产品/模块版本] */ @Component @Scope("prototype") @RequestMapping("/downloadFile") public class DownloadAction { @RequestMapping("download") public ResponseEntity<byte[]> download() throws IOException { String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx"; File file=new File(path); HttpHeaders headers = new HttpHeaders(); String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
在浏览器里面写一句,便可以下载文件
[html]
view plaincopyprint?
<a href="./downloadFile/download" >下载</a>
springMVC具体怎么配置,请参看:
http://blog.csdn.net/clj198606061111/article/details/20492887
springmvcdownload文件下载
[java]
view plaincopyprint?
package com.clj.test.down.util; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.context.annotation.Scope; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; /** * <一句话功能简述> * <功能详细描述> * * @author Administrator * @version [版本号, 2014年3月7日] * @see [相关类/方法] * @since [产品/模块版本] */ @Component @Scope("prototype") @RequestMapping("/downloadFile") public class DownloadAction { @RequestMapping("download") public ResponseEntity<byte[]> download() throws IOException { String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx"; File file=new File(path); HttpHeaders headers = new HttpHeaders(); String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
package com.clj.test.down.util; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.context.annotation.Scope; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; /** * <一句话功能简述> * <功能详细描述> * * @author Administrator * @version [版本号, 2014年3月7日] * @see [相关类/方法] * @since [产品/模块版本] */ @Component @Scope("prototype") @RequestMapping("/downloadFile") public class DownloadAction { @RequestMapping("download") public ResponseEntity<byte[]> download() throws IOException { String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx"; File file=new File(path); HttpHeaders headers = new HttpHeaders(); String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
在浏览器里面写一句,便可以下载文件
[html]
view plaincopyprint?
<a href="./downloadFile/download" >下载</a>
<a href="./downloadFile/download" >下载</a>
springMVC具体怎么配置,请参看:
http://blog.csdn.net/clj198606061111/article/details/20492887
相关文章推荐
- Simulated Test of SCJP for JAVA2 PlatFORM (only for training)【修改版】
- Java中Runnable和Thread的区别
- Android Studio+jdk 环境安装
- java使用Thumbnailator操作图片
- Unable to create an instance of the Java Virtual Machine
- 利用IKVM在C#中调Java程序(总结+案例)
- Java下利用Jackson进行JSON解析和序列化
- java的线程安全与不安全
- Java知多少(80)图形界面设计基础
- Java基础之反射
- Java基础之Java1.5重要新特性
- Java使用soap调用qq在线状态
- java-String中的 intern()(
- spring--两个数据源模板
- spring--两个数据源模板
- struts2简介
- 使用myeclipse的反向工程来生成相应的hibernate映射文件和POJO类
- 初学java java与C的区别
- Java 数据类型相关
- 第一个struts2实例