下载文件

    @GetMapping(value = "downloadFile",produces = "text/plain;charset=UTF-8")
    public void downloadFile(HttpServletResponse response, String path) throws IOException {
        String fileName = path.substring(path.lastIndexOf("."),path.length());
        InputStream fileInputStream = new FileInputStream(path);
        OutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition","attachment; filename= " + FileUtils.getNowTime()+fileName + "");
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while ((len = fileInputStream.read(b)) > 0){
                outputStream.write(b,0,len);
            }
            fileInputStream.close();
            outputStream.close();
        }catch (Exception e){
            System.out.println("下载失败");
            e.printStackTrace();
        }
    }

 

posted @ 2023-11-16 15:48  _Lawrence  阅读(46)  评论(0)    收藏  举报