springmvc下载文件

Controller内代码:

@RequestMapping(value = "/upload")
    public ResponseEntity<byte[]> upload() throws IOException{
        File file=new File("G:\\e盘\\恒之工作室\\书\\zxc.pdf");
        HttpHeaders headers = new HttpHeaders();
        String fileName=new String("zxc.pdf".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }
View Code

需要用到commons-io-XX.jar包

posted @ 2017-12-29 21:12  王森123  阅读(101)  评论(0编辑  收藏  举报