spring文件的下载

@RequestMapping("/downLoad")
    public void downLoad(HttpServletResponse response){
        File file=new File("E:\book\JavaScript权威指南(第6版)(中文版).pdf");
        if(file==null||!file.exists()){
            return;
        }
        OutputStream out=null;
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        try {
            out=response.getOutputStream();
            out.write(FileUtils.readFileToByteArray(file));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
原文地址:https://www.cnblogs.com/javaweb2/p/6591181.html