zoukankan      html  css  js  c++  java
  • springmvc文件下载

    下面代码为模拟springmvc文件下载 如果需要实现具体功能 一般一个按钮提交到这个/downLoad这个控制器方法就行了,这是主要的action处的代码

    /**
         * 文件下载
         * @param request
         * @param filename
         * @param model
         * @return
         * @throws Exception
         */
        @RequestMapping("/downLoad")
        public ResponseEntity<byte[]> downLoad(HttpServletRequest request,
                @RequestParam(value="filename",required=false)String filename,
                Model model) throws Exception{
            //下载文件路径
            String filepath = request.getServletContext().getRealPath("/image");
            File file = new File(filepath, "图片.jpg");
            HttpHeaders headers = new HttpHeaders();
            //下载显示的文件名,解决中文乱码问题
            String downLoadFileName = new String("图片.jpg".getBytes("utf-8"),"iso-8859-1");
            //通知浏览器以attachment(下载方式)打开
            headers.setContentDispositionFormData("attachment", downLoadFileName);
            //application_octet_stream 二进制流数据(最常见的文件下载)
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
        }
    }
    勿忘初心 得过且过
  • 相关阅读:
    Gym 101194L / UVALive 7908
    POJ 2259
    POJ 2559
    Gym 101194E / UVALive 7901
    Gym 101194D / UVALive 7900
    一种整数集上二分的正确写法
    日常训练记录
    Gym 101194C / UVALive 7899
    Gym 101194A / UVALive 7897
    HDU 5542
  • 原文地址:https://www.cnblogs.com/xpf1009/p/9227317.html
Copyright © 2011-2022 走看看