文件下载代码

  •  文件下载可以直接用下面的俩个方法。
package com.zscat.web;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


import com.zsCat.common.common.utils.PageUtils;
import com.zsCat.common.common.utils.Query;
import com.zsCat.common.common.utils.R;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import com.zscat.shop.domain.FileDO;
import com.zscat.shop.service.FileService;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




@Controller
@RequestMapping("/shop/file")
public class FileController {


 
@RequestMapping(value = "/download", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> downloadFile(Long id)
            throws IOException {
        String filePath = "upload/2018/05/19/09/商品类目.xlsx";
        //2.获取绝对路径
        String tmpPath = ClassLoader.getSystemClassLoader().getResource("./static").toString();
        String realUploadPath = tmpPath.substring(tmpPath.indexOf("/") + 1, tmpPath.length()) + "/";
        filePath=realUploadPath+filePath;
        FileSystemResource file = new FileSystemResource(filePath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"",new String(file.getFilename().getBytes("utf-8"),"ISO-8859-1") ));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");


        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }



https://blog.csdn.net/xiangwangxiangwang/article/details/78092396      

此处链接有获取部署的servletContext
import org.springframework.stereotype.Controller;    
    import org.springframework.web.bind.annotation.RequestMapping;    
    import org.springframework.web.context.ServletContextAware;    
        
    import javax.servlet.ServletContext;    
    import javax.servlet.ServletOutputStream;    
    import javax.servlet.http.HttpServletResponse;    
    import java.io.*;    
        
    @Controller    
    public class FileController implements ServletContextAware{    
        //Spring这里是通过实现ServletContextAware接口来注入ServletContext对象    
        private ServletContext servletContext;    
        
        
        @RequestMapping("file/download")    
        public void fileDownload(HttpServletResponse response){    
            //获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载    
            String path = servletContext.getRealPath("/");    
        
            //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型    
            response.setContentType("multipart/form-data");    
            //2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)    
            response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");    
            ServletOutputStream out;    
            //通过文件路径获得File对象(假如此路径中有一个download.pdf文件)    
            File file = new File(path + "download/" + "download.pdf");    
        
            try {    
                FileInputStream inputStream = new FileInputStream(file);    
        
                //3.通过response获取ServletOutputStream对象(out)    
                out = response.getOutputStream();    
        
                int b = 0;    
                byte[] buffer = new byte[512];    
                while (b != -1){    
                    b = inputStream.read(buffer);    
                    //4.写到输出流(out)中    
                    out.write(buffer,0,b);    
                }    
                inputStream.close();    
                out.close();    
                out.flush();    
        
            } catch (IOException e) {    
                e.printStackTrace();    
            }    
        }    
        
        @Override    
        public void setServletContext(ServletContext servletContext) {    
            this.servletContext = servletContext;    
        }    
    }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值