I am storing images in DB using MEDIUMBLOB. When I try to load image through a servlet, I am able to see those images. However if the image size is big (1 MB or more), I can see half or 3/4th image on the browser.
When I download same image and put it in public webcontent, it works perfectly. Any idea how to overcome this issue? Do I need to set any variable in the servlet or MySQL?
The (JSF-generated) HTML code is as follows:
<img src="DisplayImage?mainID=drawing" />
The image servlet does the following:
String imgLen = rs1.getString(1);
int len = imgLen.length();
byte[] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
InputStream inputStream = readImg;
int index = readImg.read(rb, 0, len);
response.reset();
response.setHeader("Content-Length", String.valueOf(len));
response.setHeader("Content-disposition", "inline;filename=/file.png");
response.setContentType("image/png");
response.getOutputStream().write(rb, 0, len);
response.getOutputStream().flush();
Edit 1
When I use below code and save file to local disk, I see full Image.
String imgLen = rs1.getString(1);
int len = imgLen.length();
rb = new byte[len];
inputStream = rs1.getBinaryStream(1);
while ((read = inputStream.read(rb)) != -1) {
out.write(rb, 0, read);
}
out.flush();
out.close();
Edit 2
When I save the half viewed images I noticed that the size of those images are 100KB. My 1 MB image is showing size as 100KB. This is happening with all images :(
I think this is biggest HINT what wrong is going on. BUT I am not getting what is wrong.
Edit 3
If I remove below stuff from my web.xml
, I am able to view those images.
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
Can I remove those? I don't re-collect why exactly I added those...
Edit 4
My web.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
60
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>restrict</filter-name>
<filter-class>com.sac.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restrict</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>DisplayImage</servlet-name>
<servlet-class>com.sac.databean.DisplayImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayImage</servlet-name>
<url-pattern>/DisplayImage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SaveMyImage</servlet-name>
<servlet-class>com.sac.databean.SaveMyImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SaveMyImage</servlet-name>
<url-pattern>/SaveMyImage</url-pattern>
</servlet-mapping>
</web-app>
See Question&Answers more detail:
os