Strange phenomenon of Java's ability to export or download attachments

has made a function to allow users to download files uploaded in the project system,

downloading is not a problem, but the background will occasionally exception because of files with different suffixes, but it does not affect the download.

in the current test, there is no problem with exporting xlsx,txt, and all other types of files will report exceptions such as actively disconnecting a connection, but it does not affect the download.

I am puzzled.

the core code is as follows

public static void downloadFile(String fileFullName) throws IOException {
        HttpServletResponse response = getResponse();
        File f = new File(fileFullName);
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;

        response.reset(); 
 
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(f.getName(), "utf-8"));
        
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0)
            out.write(buf, 0, len);
        br.close();
        out.close();
    }
Mar.10,2021
Menu