2013-11-01 105 views
0

我的服务器上有一个PDF文件,需要用户从客户端下载。打开/另存为...对话框不显示

使用Spring框架,我用javax.servlet.http.HttpServletResponse创建正确的反应和相应的头:

response.setHeader("Expires", "-1"); 
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 
response.setHeader("Pragma", "public"); 
response.setContentType("application/pdf"); 
response.setHeader("Content-Disposition", "attachment;filename="content.pdf"); 
response.setContentLength(content.size()); 

然后我用ServletOutputStream的写的内容:

ServletOutputStream os; 
try { 
    os = response.getOutputStream(); 
    os.write(((ByteArrayOutputStream)baos).toByteArray()); 
    baos.close(); 
    os.flush(); 
    os.close(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

在客户端,我收到HTTP代码200,并收到正确的响应正文,PDF文件,但“另存为...”弹出窗口未出现。

在Header配置中是否有任何原因可能导致此问题或者它可能在其他地方?

谢谢。

+0

您正在使用哪种浏览器? –

+0

@MicheleMariotti IE 9 – xptoo

+0

你是从servlet还是从jsp页面调用这段代码?你确定没有设置其他头文件吗? –

回答

0

可能:

attachment;空间filename=content.pdf

更新

public static void download(HttpServletResponse response, File file, String downloadName) throws IOException 
{ 
    if(file == null) throw new IllegalArgumentException("file is null"); 

    response.reset(); 
    response.setHeader("Content-Length", String.valueOf(file.length())); 
    response.setContentType(new MimetypesFileTypeMap().getContentType(file)); 
    response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\""); 

    InputStream input = new FileInputStream(file); 

    try 
    { 
     OutputStream output = response.getOutputStream(); 

     try 
     { 
      IOUtils.copy(input, output); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 

      throw e; 
     } 
     finally 
     { 
      output.close(); 
     } 
    } 
    catch(IOException e) 
    { 
     throw e; 
    } 
    finally 
    { 
     input.close(); 
    } 
} 

,我看到的唯一区别是在报头部分。 你尝试过没有缓存控制,杂注和过期?

更新

没有差异可言使用文件或流:

public static void download(HttpServletResponse response, InputStream input, String downloadName, String contenType) throws IOException 
{ 
    response.reset(); 
    response.setHeader("Content-Length", String.valueOf(input.available())); 
    response.setContentType(contenType); 
    response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\""); 

    OutputStream output = response.getOutputStream(); 
    IOUtils.copy(input, output); 
    input.close(); 
} 
+0

不,它不起作用 – xptoo

+0

对不起,我的坏。我想要导出的文件是使用HTTP响应创建的,并不存储在服务器中。 – xptoo

+0

我试过IOUtils复制解决方案,但没有奏效。 除此之外,如果我们使用网络监视器,我们可以从HTTP响应中获取PDF文件。是否有可能我没有正确关闭流? – xptoo

0

尝试的内容类型设置为application/octet-stream代替:

response.setContentType("application/octet-stream"); 

这应该强制浏览显示“另存为...”弹出窗口。如果您将其设置为application/pdf,则浏览器会识别文件类型并将其显示出来。

0

当我运行这段代码的问题排在

response.setHeader("Content-Disposition", "attachment;filename="content.pdf"); 

定义文件名

尝试:

response.setHeader("Content-Disposition", "attachment;filename="+"content.pdf"); 

这将打开对话框,并给你保存为选项时点击保存按钮

相关问题