• Spring MVC实现文件下载


    方法一:

        @RequestMapping("/testHttpMessageDown")
        public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
            File file = new File(request.getSession().getServletContext().getClassLoader().getResource("test.xlsx").getPath());
            byte[] body = null;
            InputStream is = new FileInputStream(file);
            body = new byte[is.available()];
            is.read(body);
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition", "attchement;filename=" + file.getName());
            HttpStatus statusCode = HttpStatus.OK;
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
            return entity;
        }

    方法二:

        /*
         * Download a file from 
         *   - inside project, located in resources folder.
         *   - outside project, located in File system somewhere. 
         */
        @RequestMapping(value="/download", method = RequestMethod.GET)
        public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
            File file = new File(request.getSession().getServletContext().getClassLoader().getResource("test.xlsx").getPath());
            
            if(!file.exists()){
                String errorMessage = "Sorry. The file you are looking for does not exist";
                System.out.println(errorMessage);
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
                outputStream.close();
                return;
            }
            
            String mimeType= URLConnection.guessContentTypeFromName(file.getName());
            if(mimeType==null){
                System.out.println("mimetype is not detectable, will take default");
                mimeType = "application/octet-stream";
            }
            
            System.out.println("mimetype : "+mimeType);
            
            response.setContentType(mimeType);
            
            /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser 
                while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/
            response.setHeader("Content-Disposition", String.format("inline; filename="" + file.getName() +"""));
    
            
            /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/
            //response.setHeader("Content-Disposition", String.format("attachment; filename="%s"", file.getName()));
            
            response.setContentLength((int)file.length());
    
            InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    
            //Copy bytes from source to destination(outputstream in this example), closes both streams.
            FileCopyUtils.copy(inputStream, response.getOutputStream());
        }

    Maven示例:

    https://github.com/easonjim/5_java_example/tree/master/springmvc/test1

    参考:

    http://www.yiibai.com/spring_mvc/spring-mvc-4-file-download-example.html

    http://blog.csdn.net/wuzuodingfeng/article/details/53489089

  • 相关阅读:
    Easy File Sharing Web Server 7.2
    我的安全之路——二进制与逆向篇
    下拉滚动,导航条悬停在顶部
    Java-json对象转Map
    Java-验证码生成(数字+字母)
    Java-List分页工具
    HDU-1556-Color the ball (线段树和差分数组两种解法)
    牛客练习赛34-C-little w and Segment Coverage(差分数组)
    牛客练习赛34-C-little w and Segment Coverage(差分数组)
    差分数组原理及应用
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7518217.html
Copyright © 2020-2023  润新知