1

下载时如何在不保存或另存为选项的情况下以视图格式获取 pdf 文件。任何人都可以帮助我解决这种情况并提供示例代码。这是我的下载和保存格式的代码。

File f= new File(file);
if(f.exists()){
    ServletOutputStream op= response.getOutputStream();
    response.reset();
    if(check==1){
        response.setContentType("application/pdf");
    }else{
        response.setContentType(content);
    }
    response.setHeader("Content-disposition","attachment; filename=" +fileName);
    byte[] buf = new byte[4096];
    int length;
    DataInputStream in = new DataInputStream(new FileInputStream(f));
    while ((in != null) && ((length = in.read(buf)) != -1)){
        op.write(buf,0,length);
    }
    in.close();
    op.flush();
    op.close();
}
4

3 回答 3

1

To indicate to the browser that the file should be viewed in the browser:

Content-Type: application/pdf
Content-Disposition: inline; filename.pdf
于 2013-10-10T09:13:11.850 回答
1

You need to change your code to

response.setHeader("Content-disposition","inline; filename=" +fileName);
于 2013-10-10T09:13:16.113 回答
0

无需做任何事情直接在浏览器上打开 pdf,只需使用response.setHeader("Content-disposition","inline;)而不是 attachment 。

于 2018-05-31T05:06:48.120 回答