如何在Java中提供本地PDF文件的下载? [英] How to offer download of local PDF file in Java?

查看:136
本文介绍了如何在Java中提供本地PDF文件的下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JBoss作为应用程序服务器运行,而在我的高清上的某个地方有一个PDF文件,当用户点击一个特定的动作时,该文件被创建。假设文件在这里: C:/PDF/doonot/10.07.2012/doonot.pdf 。如何提供这个文件作为下载?我已经做了一个CSV文件,但我不知道如何用PDF。



非常感谢任何帮助。

解决方案

是否有在jsp中下载所有类型的文件的常见方法?



可以使用这样的东西:

  public HttpServletResponse getFile(HttpServletRequest request,HttpServletResponse httpServletResponse,.......){
HttpServletResponse response = httpServletResponse;
InputStream in = / *在这里您阅读您的文件作为BinaryStream * /

String filename =;
String agent = request.getHeader(USER-AGENT);
if(agent!= null&&&  agent.indexOf(MSIE)!= -1)
{
filename = URLEncoder.encode(/ *这是FILENAME SHOW USER * /,UTF8);
response.setContentType(application / x-download);
response.setHeader(Content-Disposition,attachment; filename =+ filename);
}
else if(agent!= null&&&agent.indexOf(Mozilla)!= -1)
{
response.setCharacterEncoding(UTF-8 );
filename = MimeUtility.encodeText(/ *这是向用户显示的文件名* /,UTF8,B);
response.setContentType(application / force-download);
response.addHeader(Content-Disposition,attachment; filename = \+ filename +\);
}


BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
byte by [] = new byte [32768];
int index = in.read(by,0,32768);
while(index!= -1){
out.write(by,0,index);
index = in.read(by,0,32768);
}
out.flush();

返回响应;
}

更新:



不要忘了你可以使用InputStream:

  //将本地文件读入InputStream 
InputStream inputStream = new FileInputStream(c:\\\SOMEFILE.xml);

或者你可以使用它,就像这样

  //从数据库中读取
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();


I have JBoss running as application server and somewhere on my HD there is a PDF file, that gets created when the user clicks on a specific action. Let's say the file is here: C:/PDF/doonot/10.07.2012/doonot.pdf. How can I offer this file as download? I already did it for a CSV file, but I don't know how to do it with PDF.

Any help is much appreciated.

解决方案

as i wrote on Is there a common way to download all types of files in jsp?

you can use something like this:

public HttpServletResponse getFile (HttpServletRequest request ,HttpServletResponse httpServletResponse, .......){
          HttpServletResponse response = httpServletResponse;
          InputStream in =/*HERE YOU READ YOUR FILE AS BinaryStream*/

          String filename = "";
          String agent = request.getHeader("USER-AGENT");
          if (agent != null && agent.indexOf("MSIE") != -1)
          {
            filename = URLEncoder.encode(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8");
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment;filename=" + filename);
          }
          else if ( agent != null && agent.indexOf("Mozilla") != -1)
          {
            response.setCharacterEncoding("UTF-8");
            filename = MimeUtility.encodeText(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8", "B");
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
          }


          BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
          byte by[] = new byte[32768];
          int index = in.read(by, 0, 32768);
          while (index != -1) {
              out.write(by, 0, index);
              index = in.read(by, 0, 32768);
          }
          out.flush();

          return response;
}

UPDATE:

Dont forget that you can use the InputStream as this:

// read local file into InputStream
InputStream inputStream = new FileInputStream("c:\\SOMEFILE.xml");

or you can use it even like this

//read from database
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();

这篇关于如何在Java中提供本地PDF文件的下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆