从Internet Explorer开始,通过servlet下载文件

时间:2014-11-26 01:38:40

标签: java internet-explorer servlets

我有通过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();     
}

1 个答案:

答案 0 :(得分:0)

我验证了客户端并发现带有href的超链接无法在资源管理器上运行,我从javascript进行了重定向;方法是我发布工作正常。