Java downloads files through response

Need to pay attention to two places:

          1: There will be garbled codes under IE, so you need to use the userAgent part to deal with

          2: After entering the method, it will not download, and there is no response. My situation is because I used AJAX to submit, so I have to solve this problem

                    1: Set AJAX parameters

                    2: Change a request method instead of AJAX

public ActionForward creatCollectFileReport(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String filePath = getMyParamPlus(request, "filePath", "");
        String userAgent = request.getHeader("User-Agent");
        String fileName = "";
        int start = filePath.lastIndexOf("/");
        if (start != -1) {
            fileName = filePath.substring(start + 1);
        }
        StringBuffer localStringBuffer = new StringBuffer(50);
        FileInputStream localFileInputStream = null;
        BufferedOutputStream localBufferedOutputStream = null;
        File localFile1 = new File(filePath);
        if ((localFile1 != null) && (localFile1.exists())
                && (localFile1.isFile())) {
            long l = localFile1.length();
            try {
                localFileInputStream = new FileInputStream(localFile1);
                response.reset();
                response.setContentType("application/octet-stream");
                response.setContentLength((int) l);
                // 针对IE或者以IE为内核的浏览器:
                if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                    fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
                } else {
                    // 非IE浏览器的处理:
                    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
                }
                response.setHeader("Content-Disposition","attachment;filename="+ new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
                
                
                localBufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
                try {
                    byte[] arrayOfByte1 = new byte[2048];
                    int i;
                    while ((i = localFileInputStream.read(arrayOfByte1)) > 0) {
                        localBufferedOutputStream.write(arrayOfByte1, 0, i);
                    }
                    localBufferedOutputStream.flush();
                } catch (Exception localException2) {
                }
                localBufferedOutputStream.close();
                localFileInputStream.close();
            } catch (Exception localException3) {

            }
        }
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localFileInputStream != null) {
                localFileInputStream.close();
            }
        } catch (Exception localException1) {
        }
        return null;
    }

 

Guess you like

Origin blog.csdn.net/qq_20594019/article/details/102796775