user9081995
user9081995

Reputation: 23

How fix java download button continuous click ClientAbortException: java.io.IOException error?

My download page has two download (pdf and installer) buttons. When a download button is clicked only once, it works OK. But when clicked continuously it throws a ClientAbortException:java.io.IOException error. I have found many answers for ClientAbortException, but they were not easy to understand for me. Can someone help me how fix this error?

This is my code for the download buttons in the Controller:

@Repository    
public interface UserRepository extends JpaRepository<User, Long> {
    User findByToken(String token);
}

@Controller
public class UserController {
    @RequestMapping(value = "download", method = RequestMethod.POST, params = "action=ins")
    public String downloadInstaller(HttpServletRequest request, HttpServletResponse response, @ModelAttribute User user, Model model)
            {

        User user2 = userRepository.findByToken(user.getToken());
        if(user2.getDownload() == 0) {
            user2.setDownload(1);
            userRepository.save(user2);

            File file = new File("File");
            download(file, response);
            log.info(Log.LOG_DOWNLOAD_INSTALLER);
            return "";
        }
        return "downloaded";
    }

    @RequestMapping(value = "download", method = RequestMethod.POST, params = "action=pdf")
    public String downloadPDF(HttpServletResponse response, @ModelAttribute User user, Model model) {       
        User user2 = userRepository.findByToken(user.getToken());
        if(user2.getDownload() == 0) {
            user2.setDownload(1);
            userRepository.save(user2);
            File file = new File("Pdf");
            download(file, response);
            log.info(Log.LOG_DOWNLOAD_PDF);
            return "";
        }
        return "downloaded";
    }

    public void download(File file, HttpServletResponse response) {

        try {
            InputStream is = new FileInputStream(file);
            response.setContentType("application/octet-stream");
            // Response header
            response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
            // Read from the file and write into the response
            OutputStream os = response.getOutputStream();

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
            os.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {           
            e.printStackTrace();
        }   
    }
}

Upvotes: 0

Views: 135

Answers (1)

Kari Sarsila
Kari Sarsila

Reputation: 252

Are you getting the exception from the "os.flush();" line of the download() method? If so, that basically means that the client who sent the request, is no longer waiting for the response. If you have made two requests, probably the first one is failing.

If there's nothing particular you want to achieve by clicking the button multiple times, I guess you can just catch and ignore the exception within download()?

Upvotes: 1

Related Questions