Spring文件下载在IE浏览器中无效

时间:2016-09-08 05:57:06

标签: java spring internet-explorer

public void downloadCreateUserTemplate(HttpServletResponse response) throws IOException, URISyntaxException {

    String fn = "/templates/Create users.xls";
    URL url = getClass().getResource(fn);
    File file = new File(url.toURI());

    String mimeType = URLConnection.guessContentTypeFromName(file.getName());
    if (mimeType == null) 
    {
        mimeType = "application/octet-stream";
    }

    response.setContentType(mimeType);
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"" + file.getName() + "\""));
    response.setContentLength((int) file.length());
    InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    FileCopyUtils.copy(inputStream, response.getOutputStream());
    inputStream.close();
    response.getOutputStream().close();
    response.getOutputStream().flush();

0 个答案:

没有答案