0

I have uploaded my war file and database on amazon ec2-instance. and trying to display images from tomcat7 webapps on jsp page. I set path /webapps/images/ in image src to display images dynamically.

When I click on upload the images. The images uploaded in path saved in the database. I fetched image path from database. The image is not showing up.

Please help me to solve this.....

Edit

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            if(!ServletFileUpload.isMultipartContent(request)){
        throw new ServletException("Content type is not multipart/form-data");
    }
    List<UploadBean> list = new ArrayList<UploadBean>();

    int  count=0;
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try {
        @SuppressWarnings("unchecked")
        List<FileItem> fileItemsList = uploader.parseRequest(request);
        Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
        while(fileItemsIterator.hasNext()){
            FileItem fileItem = fileItemsIterator.next();
            System.out.println("FieldName="+fileItem.getFieldName());
            System.out.println("FileName="+fileItem.getName());
            System.out.println("ContentType="+fileItem.getContentType());
            System.out.println("Size in bytes="+fileItem.getSize());
            File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
            System.out.println("Absolute Path at server="+file.getAbsolutePath());
           fileItem.write(file);
           SelectImageClass select = new SelectImageClass();
           insertTempExp(fileItem.getName());
           list= select.getUploadedImage();
           request.setAttribute("serv", list);
           request.getRequestDispatcher("/test.jsp").forward(
           request, response);
        }
    } catch (FileUploadException e) {
        out.write("Exception in uploading file.");
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Irshad Qureshi
  • 65
  • 1
  • 2
  • 6

4 Answers4

2

Show your jsp and java file to find what path you are using to access images from server.

Simply use this for uploading path

    String rootPath=request.getSession().getServletContext().getRealPath("/");
    String relativePath="images";  //folder name to save images
    File file = new File(rootPath + File.separator + relativePath);
    if(!file.exists()){ file.mkdirs();}

    ctx.setAttribute("FILES_DIR_FILE", file);
    ctx.setAttribute("FILES_DIR", rootPath + File.separator + relativePath);

and show the image in jsp using <image src="images/image_name">

sohal
  • 189
  • 1
  • 6
  • Hi sohal, can you please explain little more where i need to put this code because String rootPath=request.getSession().getServletContext().getRealPath("/"); i dont have Request methode in servletListener class – Irshad Qureshi Dec 02 '15 at 08:17
  • So use ServletContext , like this ServletContext ctx = servletContextEvent.getServletContext(); String rootPath=ctx.getRealPath("/"); – sohal Dec 02 '15 at 09:04
  • Hi sohal, i have used this code directly the image folder is created inside the project but i dont have image uploaded and null pointer exception on using ServletContext ctx = servletContextEvent.getServletContext(); – Irshad Qureshi Dec 02 '15 at 09:12
  • I did use same code in servlet without any listener and it is working fine for me. – sohal Dec 02 '15 at 09:31
  • thank u so much sohal its working but the previous uploaded images by default get deleted when i run code again now i working on local host – Irshad Qureshi Dec 02 '15 at 10:23
  • hi sohal, its working on local server when i uploaded the war file on amazon ec2-instance getting no result – Irshad Qureshi Dec 02 '15 at 11:02
  • and i need to use global folder to store uploaded image that can be shown in another project on same server – Irshad Qureshi Dec 02 '15 at 11:25
0
public class FileLocationContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String rootPath = System.getProperty("catalina.home");
        ServletContext ctx = servletContextEvent.getServletContext();
        String relativePath = ctx.getInitParameter("/webapps/CovetloFiles.dir");

        File file = new File(rootPath + File.separator + relativePath);
        if(!file.exists()){ file.mkdirs();}
        if(!file1.exists()){ file1.mkdirs();}

        System.out.println("File Directory created to be used for storing files");
        ctx.setAttribute("FILES_DIR_FILE", file);
        ctx.setAttribute("FILES_DIR", rootPath + File.separator + relativePath);
    }

<%
ArrayList<UploadBean> list = (ArrayList<UploadBean>) request.getAttribute("serv"); 
if(list!=null) {
  for(UploadBean upload : list) {
    strGet = upload.getUploadImage();   
%>
<img class="img-responsive" src="/CovetloFiles/<%=upload.getUploadImage()%>" name="imgUrl" alt="prod1">
Stefan Ferstl
  • 5,135
  • 3
  • 33
  • 41
Irshad Qureshi
  • 65
  • 1
  • 2
  • 6
0

Above using servlet context i set path where the image will get uploaded

Below i have created java class where i get image name from database only images uploaded on above path those name stored in database

i have used : request.setAttribute("serv",list); list contain all the name from database table

Irshad Qureshi
  • 65
  • 1
  • 2
  • 6
0
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            if(!ServletFileUpload.isMultipartContent(request)){
        throw new ServletException("Content type is not multipart/form-data");
    }
             List<UploadBean> list = new ArrayList<UploadBean>();
            //HttpSession session = request.getSession(true);
     int  count=0;
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try {
        @SuppressWarnings("unchecked")
        List<FileItem> fileItemsList = uploader.parseRequest(request);
        Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
        while(fileItemsIterator.hasNext()){
            FileItem fileItem = fileItemsIterator.next();
            System.out.println("FieldName="+fileItem.getFieldName());
            System.out.println("FileName="+fileItem.getName());
            System.out.println("ContentType="+fileItem.getContentType());
            System.out.println("Size in bytes="+fileItem.getSize());


            File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
            System.out.println("Absolute Path at server="+file.getAbsolutePath());
           // out.write("<a href=\"UploadServlet?fileName="+fileItem.getName()+"\">Download <img src=/CovetloFiles/"+fileItem.getName()+"  width=100px height=100px></a>");

            fileItem.write(file);
            SelectImageClass select = new SelectImageClass();
           insertTempExp(fileItem.getName());
           list= select.getUploadedImage();
           request.setAttribute("serv", list);
       request.getRequestDispatcher("/test.jsp").forward(
            request, response); 

        }
    } catch (FileUploadException e) {
        out.write("Exception in uploading file.");
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Irshad Qureshi
  • 65
  • 1
  • 2
  • 6
  • if image is being uploaded in file directory and right path in db. try to print the path and then try to access the same path from browser in url path. if your path is OK, get path on Jsp page and then do reference like this – Khalid Habib Dec 04 '15 at 05:45