I have posted how to upload image in path or in database Blob column Upload Image in server.
Now next is how to show image on Page.
For same we have to use Servlet.
- Create Servlet.
- Now use following below code to in Servlet to show image
import java.io.BufferedInputStream;
import java.io.File;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageShowServlt extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = (request.getParameter("path"));
OutputStream os = response.getOutputStream();
if (path.equalsIgnoreCase("No")) {
path = "D:\\Document\\UserImage\\User.jpg";
}
if (request.getParameter("path") == "") {
path = "D:\\Document\\UserImage\\User.jpg";
}
InputStream inputStream = null;
try {
File outputFile = new File(path);
inputStream = new FileInputStream(outputFile);
BufferedInputStream in = new BufferedInputStream(inputStream);
int b;
byte[] buffer = new byte[10240];
while ((b = in.read(buffer, 0, 10240)) != -1) {
os.write(buffer, 0, b);
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (os != null) {
os.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}
}
Servlet process image file into bytes and then show image using af:image component.
For this create a managed bean with variable name path.
Generate variable Accessor.
Now run application
No comments:
Post a Comment