springMVC 文件下载【文件已经自服务器上准备,可以按照此下载文件】

文章来源:http://blog.csdn.net/clj198606061111/article/details/20743769

  1. package com.clj.test.down.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7. import org.springframework.context.annotation.Scope;  
  8. import org.springframework.http.HttpHeaders;  
  9. import org.springframework.http.HttpStatus;  
  10. import org.springframework.http.MediaType;  
  11. import org.springframework.http.ResponseEntity;  
  12. import org.springframework.stereotype.Component;  
  13. import org.springframework.web.bind.annotation.RequestMapping;  
  14.   
  15. /** 
  16.  * <一句话功能简述> 
  17.  * <功能详细描述> 
  18.  *  
  19.  * @author  Administrator 
  20.  * @version  [版本号, 2014年3月7日] 
  21.  * @see  [相关类/方法] 
  22.  * @since  [产品/模块版本] 
  23.  */  
  24. @Component  
  25. @Scope("prototype")   
  26. @RequestMapping("/downloadFile")  
  27. public class DownloadAction  
  28. {  
  29.   
  30.     @RequestMapping("download")    
  31.     public ResponseEntity<byte[]> download() throws IOException {    
  32.         String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx";  
  33.         File file=new File(path);  
  34.         HttpHeaders headers = new HttpHeaders();    
  35.         String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
  36.         headers.setContentDispositionFormData("attachment", fileName);   
  37.         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
  38.         return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
  39.                                           headers, HttpStatus.CREATED);    
  40.     }    
  41. }  

在浏览器里面写一句,便可以下载文件

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <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"/>  


  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  2.     <property name="messageConverters">  
  3.         <list>  
  4.             //把ByteArray加在Json前面  
  5.             <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>  
  6.             <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >  
  7.                 <property name = "supportedMediaTypes">  
  8.                     <list>  
  9.                         <value>text/plain;charset=UTF-8</value>  
  10.                     </list>  
  11.                 </property>  
  12.             </bean>  
  13.         </list>  
  14.     </property>  
  15. </bean>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值