springMVC 文件下载【文件已经自服务器上准备,可以按照此下载文件】
2016-02-24 16:45
597 查看
文章来源:http://blog.csdn.net/clj198606061111/article/details/20743769
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
plain copy
<a href="./downloadFile/download" >下载</a>
springMVC具体怎么配置,请参看:
http://blog.csdn.net/clj198606061111/article/details/20492887
==================================================================
参考文章:http://blog.csdn.net/buster2014/article/details/50731952
按照上面下载文件时文本文件不是excel文件,springmvc配置文件需要加入如下配置【仅供参考,和自己的配置结合】:
此配置必须有<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
//把ByteArray加在Json前面
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
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
plain copy
<a href="./downloadFile/download" >下载</a>
springMVC具体怎么配置,请参看:
http://blog.csdn.net/clj198606061111/article/details/20492887
==================================================================
参考文章:http://blog.csdn.net/buster2014/article/details/50731952
按照上面下载文件时文本文件不是excel文件,springmvc配置文件需要加入如下配置【仅供参考,和自己的配置结合】:
此配置必须有<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
//把ByteArray加在Json前面
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
相关文章推荐
- Spring MVC 文件下载最佳实践
- java web 开发中验证码的应用
- Eclipse中使用javap
- spring security的标签库
- Java并发学习博客
- springmvc 项目完整示例08 前台页面以及知识点总结
- JRE与JVM、JDK的区别
- springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置
- spring-security原理学习
- 基于Struts2的供求信息网设计(一)
- springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置
- ES学习(一)Windows搭建
- springmvc 项目完整示例05 日志 --log4j整合 配置 log4j属性设置 log4j 配置文件 log4j应用
- Java输入输出流
- springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
- Java命名规则
- Struts Spring Hibernate
- springmvc 项目完整示例03 小结
- JAVA多线程
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试