Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PDFBox和Java Filter Servlet在下载PDF文件之前向其添加头_Java_Servlet Filters_Pdfbox - Fatal编程技术网

使用PDFBox和Java Filter Servlet在下载PDF文件之前向其添加头

使用PDFBox和Java Filter Servlet在下载PDF文件之前向其添加头,java,servlet-filters,pdfbox,Java,Servlet Filters,Pdfbox,我试图在向客户端显示之前向PDF文件添加标题。我有一个过滤器servlet,它将在应用程序请求保存之前拦截请求/响应。我已经在一个独立的PDF上测试了我的PDFBox类,这很有效。我在使用过滤器Servlet时遇到困难。我尝试了HttpServletRequestWrapper,但无法获得eh inputstream。它似乎是空的。这是我的过滤代码: public class AddFOUOFilterServlet implements Filter { /** The log. */ pri

我试图在向客户端显示之前向PDF文件添加标题。我有一个过滤器servlet,它将在应用程序请求保存之前拦截请求/响应。我已经在一个独立的PDF上测试了我的PDFBox类,这很有效。我在使用过滤器Servlet时遇到困难。我尝试了HttpServletRequestWrapper,但无法获得eh inputstream。它似乎是空的。这是我的过滤代码:

public class AddFOUOFilterServlet implements Filter {
/** The log. */
private static Log log = LogFactory.getLog(AddFOUOFilterServlet.class);

private String loginUrl = null;

/**
 * @desc - This function initializes the global variables.
 * @param filterConfig - FilterConfig settings that come from the web.xml
 */
public void init(FilterConfig filterConfig) throws ServletException {
    log.debug("Inside AddFOUOFilterServlet init ");
    ServletContext context = filterConfig.getServletContext();
} 

/**
 * @desc - the method called by the server before each server request
 * @param req - the servlet request
 * @param res - the servlet response
 * @param chain - a FilterChain object that lists all filters to be called by the server
 * @throws IOException, ServletException
 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    //log.debug("Inside doFilter");

    // cast the HTTP request and response objects and get the url path info
    HttpServletRequest request = (HttpServletRequest)req; 
    HttpServletResponse response = (HttpServletResponse)res;  
    HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request);
    InputStream is = request.getInputStream();
        log.debug("is.available() "+is.available());
        byte[] buf = new byte[1024];
        while((is.read(buf))>0) {
            log.debug("Reading the InputStream from the wrapper ");
    }
    if (request.getParameter("DocumentName") != null && request.getParameter("BlobID") != null)
    {
        log.debug("Processing the PDF URIs... ");
    try 
    {   
            AddFOUOToReport addfouo = new AddFOUOToReport();
            OutputStream os = null;
            os = addfouo.doIt(is, "For Official Use Only");
            chain.doFilter(requestWrapper, res);
    }
    catch (IOException ex)
    {
        log.error("AddFOUOToReport Filter error: " + ex.getMessage());
        response.sendRedirect(loginUrl); 
    } catch (COSVisitorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    } else {
        chain.doFilter(req, res);
    }

}

public void destroy() {log.info("Inside destroy");  }
}

Web.xml条目:

    <!-- BEGIN AddFOUOFilter Changes  -->
<filter>
    <filter-name>AddFOUO Filter</filter-name>
    <filter-class>com.jmar.bo.controller.AddFOUOFilterServlet</filter-class>
</filter>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/cdz/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/cdzServlet?getBlob*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- END AddFOUOFilter Changes -->

AddFOUO过滤器
com.jmar.bo.controller.AddFOUOFilterServlet
AddFOUO过滤器
/cdz/*
AddFOUO过滤器
/cdzServlet?getBlob*
AddFOUO过滤器
/*

没有唯一的模式来捕获此事件,这就是我使用as/*as模式的原因。

如果
InputStream
为空,则表示浏览器没有发送PDF


我猜PDF不是来自浏览器,而是来自服务器…

感谢您的快速回复,这是正确的PDF来自服务器。这看起来像是在文件准备好之前过滤器触发。不,您正在从
请求
读取
输入流
,这是浏览器发送的。这不是正确的方法。我尝试了响应对象包装器,但没有getInputstream。如果我使用的是过滤器servlet,那么对该过滤器的请求应该具有文件权限?你对正确的方法有什么建议吗?感谢您的时间。请参阅“为自定义请求和响应编程”一节。