简体   繁体   English

自Internet Explorer以来通过servlet下载文件

[英]download file through servlet since internet explorer

I have this method for download files through servlet, and works fine on chrome and mozilla but not on Internet Explorer (I try on version 11) 我有这种方法可以通过servlet下载文件,并且在chrome和mozilla上工作正常,但在Internet Explorer上却不能(我尝试使用版本11)

public void downloadFile(HttpServletResponse response, String filePath) throws FileNotFoundException, IOException
{
    System.out.println(response.toString());
    File downloadFile = new File(filePath);
    OutputStream outStream;
    // gets MIME type of the file
    try (FileInputStream inStream = new FileInputStream(downloadFile)) {
        // gets MIME type of the file
        String mimeType = "application/octet-stream";
        System.out.println("MIME type: " + mimeType);
        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        // forces download
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
        //response.setHeader("Cache-Control", "max-age=60");
        // obtains response's output stream
        outStream = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
    }
    outStream.close();     
}

I verify the client side and find that the hiperlink with href wasnt working on explorer, I made the redirect from javascript; 我验证了客户端,发现带有href的hiperlink在浏览器上不起作用,我从javascript进行了重定向; the method is that I post is working fine. 方法是我发的很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM