zoukankan      html  css  js  c++  java
  • Spring MVC之ResposeEntity下载文件

    Spring Mvc中用ResponseEntity方式下载文件如下:

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("fileName") String fileName) throws IOException {
            String path = request.getServletContext().getRealPath("/uploadImages/");
            File dwFile = new File(path+File.separator+fileName);
            HttpHeaders headers = new HttpHeaders();
            //下载显示的中文名,解决中文名称乱码问题
            String downloadFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
            //application/octet-stream:二进制流数据
            headers.setContentDispositionFormData("attachment",downloadFileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(dwFile),headers, HttpStatus.CREATED);
        }
    }
    

      

  • 相关阅读:
    psi
    firefox修改语言
    automapper
    堆喷图解
    脱壳系列_0_FSG壳_详细版
    脱壳系列_1_UPX壳_详细版
    算法01-最大子数组详解
    逆向MFC程序
    如何执行shell命令
    Unity之流光效果
  • 原文地址:https://www.cnblogs.com/foxting/p/6697933.html
Copyright © 2011-2022 走看看