1

我正在使用 Spring MVC 创建一个 webapp,它有一个按钮,当你按下它时,它会在我的 web 应用程序之外点击一个 servlet 并返回一个 excel 电子表格。

我正在使用 servlet 来在应用程序之间建立 servlet 样式链接,而不是直接链接。不过,我对 http 标头有点问题。发生的事情是,在我从另一个应用程序中获取文件到它在我的应用程序中返回给用户的时间之间,文件被损坏了。我认为这是因为我没有正确编写 http 标头。我在想这是因为当我在我的应用程序之外点击其他应用程序时文件很好。

我想要实现的是一些代码,使我的应用程序中的 http 标头看起来与我的 servlet 从其他应用程序接收的 http 标头完全相同,但文件名除外。

这是我的代码。

private void getReportContent(HttpServletRequest request, HttpServletResponse response) throws BusinessException, ParseException {
    HttpSession session = request.getSession(true);
    int statusCode = 0;
    HttpState state = null;

synchronized (session) {
    state = (HttpState) session.getAttribute(HTTP_STATE_KEY);
    if (state == null) {
        if (logger.isDebugEnabled())
            logger.debug("Creating new http state.");
        state = createHttpState(request);
        session.setAttribute(HTTP_STATE_KEY, state);
    }
}
String reportURL = null;
GetMethod getMethod = null;

try {

    // Generate the Report URL
    reportURL = createReportURL(request, response);                 
    getMethod = new GetMethod(reportURL);   
    getMethod.setFollowRedirects(false);

    // Retrieve the Report          
    statusCode = client.executeMethod(client.getHostConfiguration(), getMethod, state);
    if (logger.isDebugEnabled())
        logger.debug("Status code from Reports servlet: " + statusCode);
    if (statusCode != HttpStatus.SC_OK) {
        throw new BusinessException(new Exception("No Report content found. Received HTTP status code " + getMethod.getStatusCode()));
    }

    response.setHeader("Date", getMethod.getResponseHeader("Date").getValue());
    response.setHeader("Server", getMethod.getResponseHeader("Server").getValue());
    response.setHeader("Content-Length", getMethod.getResponseHeader("Content-Length").getValue());
    response.setHeader("Content-Length", getMethod.getResponseHeader("Content-Length").getValue());
    response.setHeader("Content-Type", getMethod.getResponseHeader("Content-Type").getValue()); 
    response.setHeader("Content-disposition", "attachment; filename=\"" + REP_REPORT_NAME + "\"");
    //Set the response into HttpServletResponse
    ServletOutputStream outputStream = response.getOutputStream();
    outputStream.write(getMethod.getResponseBody());            

} catch (IOException e){
    logger.error("Unable to contact report URL", e);
    throw new BusinessException("Unable to contact report URL",e);
} finally {
    // Close the HTTPClient connection
    if (getMethod!= null){
        getMethod.releaseConnection();
    }
}

}

有谁知道我做错了什么,或者任何人都可以提出任何提示来实现我想做的事情。在调试会话中,当我查看从其他应用程序返回的标头时,它看起来像......

[Date: Mon, 26 Aug 2013 10:47:42 GMT 
, Server: Oracle-Application-Server-10g/9.0.4.1.0 Oracle-HTTP-Server 
, Content-Length: 802570 
, Content-Disposition: attachement; name="TestEmployeeReport.xls"
, Content-Type: application/vnd.ms-excel
]
4

0 回答 0