0
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logger.info("Inside Upload Image Preview---");

    String imageName = request.getParameter("imgName");
    String contentType = "image/";

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    logger.info("Inside Upload Image Preview---Before try block.");

    try {
        String imageExt = imageName.substring(imageName.lastIndexOf(".")+1);
        logger.info("conent Type for the response is "+contentType);
        logger.info("Image name  is "+imageName);

        contentType = contentType + imageExt;
        response.setContentType(contentType);

        String dirPath= System.getProperty(AMPortletConstants.JBOSS_HOME_DIR)+"/image/";
        File fileName = new File(dirPath+imageName);

        FileInputStream fileInputStream = new FileInputStream(fileName);
        input = new BufferedInputStream(fileInputStream);
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }

        logger.info("Image added in response.");
    } catch (Exception exception) {
        logger.error("Exception in UploadImagePreview======="+exception);
        exception.printStackTrace();
    }   
}

Web.xml

<servlet>
    <servlet-name>imagePreview</servlet-name>
<servlet-lass>com.xxxxxx.portlet.UploadImagePreview</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>imagePreview</servlet-name>
    <url-pattern>/xxxxxxImages/*</url-pattern>  
</servlet-mapping>

我们点击的 URL 模式

/xxxxxxxx-portlet/xxxxxxImages/img?imgName=Test.png

它在浏览器中显示图像,但问题是图像不完整。缺少较小的底部。请提供解决方案。

4

1 回答 1

0

你不见了

finally
{
    output.flush();
    output.close();
}

实际上close()打电话flush(),但关于你的问题的重要部分是缺少流的冲洗。永远记住close()您的资源,否则您将因资源泄漏而受到伤害:-)

干杯,

于 2013-08-12T10:24:39.883 回答