码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc 文件下载

时间:2016-08-31 18:40:17      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

1、使用servlet的API实现

参考:http://my.oschina.net/u/1394615/blog/311307

@RequestMapping("/download")
    public String download(String fileName, HttpServletRequest request,
            HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
        try {
            String path = Thread.currentThread().getContextClassLoader()
                    .getResource("").getPath()
                    + "download";//这个download目录为啥建立在classes下的
            InputStream inputStream = new FileInputStream(new File(path
                    + File.separator + fileName));

            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }

             // 这里主要关闭。
            os.close();

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
            //  返回值要注意,要不然就出现下面这句错误!
            //java+getOutputStream() has already been called for this response
        return null;
    }

 

2、使用spring的API实现 

参考: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);    
    }    
}  

 

打发大丰

springmvc 文件下载

标签:

原文地址:http://www.cnblogs.com/mabaishui/p/5826834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!