-2

我想用 servlet 打开一个文件

我有一个返回这种类型 byte[] 的函数

我有一个包含按钮的 jps 文件,当我单击此按钮时,将调用此方法(咨询)我尝试此代码

public class Test{

    private HttpServletRequest request;
      private HttpServletResponse response;

public void consult() {

        P8Connector connector = new P8Connector(TypeEnum.CE.value(),
                "test");


        GetDocumentsListInFolder getDocumentsListInFolder = new GetDocumentsListInFolder();
        List<Document> list = new ArrayList<Document>();

        list = getDocumentsListInFolder.execute(connector,
                "/test", "1");


        for (int i = 0; i < list.size(); i++) {
            Document doc = (Document)list.get(i);
            GetDocumentContent c = new GetDocumentContent();
           byte[] is =  c.execute(connector , doc); 


   response.setHeader("Expires", "0");
      response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
         response.setHeader("Pragma", "public");
          response.setContentType("application/pdf");
           InputStream in = new FileInputStream("20130114.008.01.pdf");
           OutputStream out = response.getOutputStream();

           // Copy the bits from instream to outstream
           byte[] buf = new byte[1024];
           int len;
           while ((len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
           }
           in.close();

}
 public void setServletRequest(HttpServletRequest request){
          this.request = request;
          }

          public HttpServletRequest getServletRequest(){
          return request;
          }

          public void setServletResponse(HttpServletResponse response){
          this.response = response;
          }

          public HttpServletResponse getServletResponse(){
          return response;
          }

}

但是当我运行这段代码时,我有

这个错误:空点异常

在这一行

   response.setHeader("Expires", "0");
4

1 回答 1

0

谁打电话setServletResponse?似乎没有人,所以响应为空。

于 2013-05-19T19:10:02.377 回答