admin管理员组

文章数量:1130349

@GetMapping("file/download")
    public void downloadDevtool(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String property = System.getProperty("user.dir");
        String path = property + "/src/main/resources/file/text.txt";
        
        File file = new File(path);
        String filename = path.substring(path.lastIndexOf("/")+1);
        response.setContentType("application/x-download");
        response.setHeader("content-Disposition", "attachment;filename=" + filename);
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            int len = 0;
            byte buffer[] = new byte[1024];
            OutputStream out = response.getOutputStream();
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
@GetMapping("file/download")
    public void downloadDevtool(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String property = System.getProperty("user.dir");
        String path = property + "/src/main/resources/file/text.txt";
        
        File file = new File(path);
        String filename = path.substring(path.lastIndexOf("/")+1);
        response.setContentType("application/x-download");
        response.setHeader("content-Disposition", "attachment;filename=" + filename);
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            int len = 0;
            byte buffer[] = new byte[1024];
            OutputStream out = response.getOutputStream();
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

本文标签: 浏览器文件SpringBoot