• SpringMVC文件下载


    package com.sniper.springmvc.action;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
     
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
     
    @Controller
    public class FileDownloadAction extends RootAction {
     
        private String getFileName(String filePath)
                throws UnsupportedEncodingException {
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            String Agent = request.getHeader("User-Agent");
            if (null != Agent) {
                Agent = Agent.toLowerCase();
                if (Agent.indexOf("firefox") != -1) {
                    fileName = new String(fileName.getBytes(), "iso8859-1");
                } else if (Agent.indexOf("msie") != -1) {
                    fileName = URLEncoder.encode(fileName, "UTF-8");
                } else {
                    fileName = URLEncoder.encode(fileName, "UTF-8");
                }
            }
            return fileName;
        }
     
        /**
         * 需要通过 redirect:download 传递文件路径或者文件类型
         *
         * @param path
         * @param type
         * @return
         * @throws IOException
         */
        @ResponseBody
        @RequestMapping(value = "download")
        public ResponseEntity<byte[]> download(
                @RequestParam(value = "path") String path,
                @RequestParam(value = "type", required = false, defaultValue = "application/vnd.ms-excel") String type)
                throws IOException {
            // 确定各个成员变量的值
            String fileName = getFileName(path);
     
            HttpHeaders headers = new HttpHeaders();
            byte[] body = null;
            HttpStatus httpState = HttpStatus.NOT_FOUND;
            File file = new File(path);
            if (file.exists() && file.isFile()) {
     
                InputStream is = new FileInputStream(file);
                body = new byte[is.available()];
                is.read(body);
                is.close();
                headers.add("Content-Type", type);
                headers.add("Content-Length", "" + body.length);
                headers.add("Content-Disposition", "attachment;filename="
                        + fileName);
                httpState = HttpStatus.OK;
     
            }
     
            ResponseEntity<byte[]> entity = new ResponseEntity<>(body, headers,
                    httpState);
     
            return entity;
        }
    }
  • 相关阅读:
    .vscode\settings.json .gitignore 项目文件配置
    mainWindow = new BrowserWindow 打开慢的原因 electron 已解决 Windows Defender 拦截导致
    iVCam 可以当电脑的摄像头 同一个wifi
    vuerouter tomcat 下报404 WEBINF 放入 web.xml 即可
    Editor Goto Location: Multiple Definitions vscode 多个定义,直接跳转到主定义 不进行选择
    VS Code Snippet Generator 插件 生成 vscode代码片段
    devsidecar 让github 可以正常访问
    react start 后 url 后面不带/ 解决思路
    vscode 尾逗号不自动删除 'commadangle': 'off' eslint vue
    coast 海岸 单词记忆方法
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5844742.html
Copyright © 2020-2023  润新知