2013年9月10日星期二

Page export file Times ClientAbortException: java.io.IOException, how to deal with this ?

RT: I use firefox and ie all the normal tests , with 360 and Sogou browser This problem occurs because the export file , but the file can be exported is the background would normally throw this exception. My online search, I found a lot of have this problem, basically no good solution , some say is tomcat problems, some say because of the abolition of the download operation cause. But seemingly did not give solutions.
wood have a look at this great God how to solve it ?
The following my code :
js:

$("#btnExport").click(function(event) {
                $("#op").val("export");
                $("#pageNo").val(1);
                $("#frmQuery").submit();
                $("#op").val("");
            });

action:

 protected void exportFile(File file) {
        logger.debug("file=" + file);
        ServletOutputStream out = null;
        try {
            byte[] fileData = FileUtils.readFileToByteArray(file);
            String filename = file.getName();

            logger.debug("fileData.length=" + fileData.length);

            HttpServletResponse response = ServletActionContext.getResponse();
            response.addHeader("Content-Disposition", "attachment; filename="
                    + filename);
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/octet-stream");
            out = response.getOutputStream();
            out.write(fileData);
        } catch (Exception e) {
            logger.error("输出文件时出错! " + file.getAbsolutePath(), e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

------ Solution ------------------------------------- -------
protected void exportFile(File file) {
        logger.debug("file=" + file);
        HttpServletResponse response = ServletActionContext.getResponse();
        ServletOutputStream out = null;
        try {
            byte[] fileData = FileUtils.readFileToByteArray(file);
            String filename = response.encodeURL(file.getName());

            logger.debug("export.file.length=" + fileData.length);

            response.reset();
            response.setContentType("application/octet-stream");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-transform, max-age=0");
            response.setHeader("Accept-Ranges", "none");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + filename);
            out = response.getOutputStream();
            out.write(fileData);
            out.flush();
        } catch (Exception e) {
            logger.error("输出文件时出错! " + file.getAbsolutePath(), e);
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

------ For reference only ------------------------- --------------
is not refresh the page repeatedly repeated requests .
I have encountered a similar problem that has been refreshed a page address , resulting in the background this error .
------ For reference only -------------------------------------- -

没有评论:

发表评论