@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
它在浏览器中显示图像,但问题是图像不完整。缺少较小的底部。请提供解决方案。