Image doesn't display in browser after file upload

Hey all,

My Question:
I use the commons fileupload to upload images onto my web server (tomcat 6.0), the diectorys to store the file are created using file.mkdirs(). I was having a problem with folder permissions (access denied error) so i changed them using the security properties window (windows vista) allowing full control of that folder to all users of the system. The file now uploads without a problem.

The thing is that now the image is not displaying on the webpage. I have double checked the address and it is correct and also checked that the file resides in the proper folder. What would be the reason for the file not displaying?

Thanks in advance for the help.
mygreenislandAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
hehehe, it must have seen us looking ;-)
0
 
rrzCommented:
>checked that the file resides in the proper folder.    
Do you mean to say that the file was created by your upload code ?  Is it the correct size ?   Please show us your code. Maybe someone can help you.    
0
 
KuldeepchaturvediCommented:
what kind of errors are you getting? how the image files are getting served to the browser?
if you go the server & try to open these images directly from there do they open?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
mygreenislandAuthor Commented:
Thanks for the quick response,

My code is as follows, maybe it'll give some insight:

package action.admin.events;

import javax.servlet.http.*;
import org.apache.struts.action.*;

import util.constants.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import java.io.*;
import java.util.*;
import database.*;
import forms.*;

public class Upload extends Action
{
      
      public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws java.lang.Exception
      {
            ServletFileUpload servletFileUpload;
            List fileItemsList;
            DiskFileItemFactory diskFileItemFactory;
            File repositoryPath;
            FileItem fileItem;
            File saveTo;
            EventRegistry sqlrequest = EventRegistry.getInstance(servlet.getServletContext());
            
            String type = request.getParameter(Constants.SV_SITE_VALUE);
            String id = request.getParameter(Constants.SV_SITE_ID);
            String year = request.getParameter(Constants.SV_SITE_YEAR);
      
            String path = "/assets/images/events";
            String filename = "";
            String return_page = "";
            String errors_page = "";
            String number = "";
            
            int last_file = 0;
            
            try
            {                  
                  if("thumb".equals(type))
                  {
                        filename = "photo.jpg";
                        path = path + "/" + year + "/" + id;
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_DETAILS_UPLOAD_THUMB;
                  }
                  
                  if("invite".equals(type))
                  {
                        filename = "invite.jpg";
                        path = path + "/" + year + "/" + id;
                        request.getSession().setAttribute(Constants.SV_SITE_ID, id);
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS_DETAILS;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_DETAILS_UPLOAD;
                  }
                  
                  if("archive_thumb".equals(type))
                  {
                        filename = "photo.jpg";
                        path = path + "/" + year + "/" + id;
                        request.getSession().setAttribute(Constants.SV_SITE_YEAR, year);
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS_ARCHIVE;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_ARCHIVE_DETAILS_UPLOAD_THUMB;
                  }
                  
                  if("archive_invite".equals(type))
                  {
                        filename = "invite.jpg";
                        path = path + "/" + year + "/" + id;
                        request.getSession().setAttribute(Constants.SV_SITE_ID, id);
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS_ARCHIVE_DETAILS;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_ARCHIVE_DETAILS_UPLOAD;
                  }
                  
                  if("archive_recap_thumb".equals(type))
                  {      
                        last_file = sqlrequest.getLastArchivedPicFilename(id);
                        last_file += 1;
                        
                        sqlrequest.setArchivedPicFilename(id, last_file);
                        filename = String.valueOf(last_file) + ".jpg";
                        
                        path = path + "/" + year + "/" + id + "/small";
                        request.getSession().setAttribute(Constants.SV_SITE_ID, id);
                        request.getSession().setAttribute(Constants.SV_SITE_VALUE, filename);
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS_ARCHIVE_DETAILS;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_ARCHIVE_DETAILS_UPLOAD_RECAP;
                  }
                  
                  if("archive_recap_large".equals(type))
                  {      
                        number = (String)request.getParameter(Constants.SV_SITE_NUMBER);
                        filename =  number + ".jpg";
                        
                        path = path + "/" + year + "/" + id + "/big";
                        request.getSession().setAttribute(Constants.SV_SITE_ID, id);
                        request.getSession().setAttribute(Constants.SV_SITE_VALUE, filename);
                        return_page = Pages.PAGE_ADMIN_ACTION_EVENTS_ARCHIVE_DETAILS;
                        errors_page = Pages.PAGE_ADMIN_EVENTS_ARCHIVE_DETAILS_UPLOAD_RECAP_LARGE;
                  }
                  
                  // Parse the HTTP request...
                  
                  diskFileItemFactory = new DiskFileItemFactory();
                  diskFileItemFactory.setSizeThreshold(81920); /* the unit is bytes */

                  repositoryPath = new File("/temp");
                  diskFileItemFactory.setRepository(repositoryPath);

                  servletFileUpload = new ServletFileUpload(diskFileItemFactory);
                  servletFileUpload.setSizeMax(81920); /* the unit is bytes */
                  
                  fileItemsList = servletFileUpload.parseRequest(request);
                  
                  Iterator it = fileItemsList.iterator();
                  
                  while (it.hasNext())
                  {
                    fileItem = (FileItem)it.next();
                    if (fileItem.isFormField())
                    {
                          if("archive_recap_large".equals(type))
                          {
                                if("caption".equals(fileItem.getFieldName()))
                                {
                                      if(fileItem.getString().isEmpty())
                                      {
                                            request.setAttribute(Constants.SV_SITE_VALUE, "Caption must be filled in.");
                                            return mapping.findForward(errors_page);
                                      }
                                      else
                                      {
                                            sqlrequest.setArchivedPicCaption(id, number, fileItem.getString());
                                      }
                                }
                          }
                    }
                    else
                    {
                          if(fileItem.getName().isEmpty())
                          {
                                request.setAttribute(Constants.IMG_PATH, "");
                                request.setAttribute(Constants.IMG_ERR, "Select an image to upload.");
                                return mapping.findForward(errors_page);
                          }
                          if(!"image/jpeg".equals(fileItem.getContentType()) && !"image/gif".equals(fileItem.getContentType()))
                          {
                                request.setAttribute(Constants.IMG_PATH, fileItem.getName());
                                request.setAttribute(Constants.IMG_ERR, "Image must be .gif or .jpg format only");
                                return mapping.findForward(errors_page);
                          }
                         
                          String fullpath = servlet.getServletContext().getRealPath( path + File.separator + filename );
                              
                          File dir = new File(path);
                      if (!dir.exists() )
                      {
                            dir.mkdirs();
                      }
                     
                          saveTo = new File(fullpath);
                          fileItem.write(saveTo);
                    }
                  }
                        return mapping.findForward(return_page);
            }
            
            catch (SizeLimitExceededException ex)
            {
                  return mapping.findForward(Pages.PAGE_SITE_ERROR);
                  /* The size of the HTTP request body exceeds the limit */
            }      
                  
      }
}

As for your other questions:

1. I am not getting any errors of any kind
2. I can open the image file directly if i put the folder path in the address bar (C:/Program Files etc..)
3. The file uploaded is the correct size
0
 
TimYatesCommented:
>> 2. I can open the image file directly if i put the folder path in the address bar (C:/Program Files etc..)

What about if you put the URL to this image itself in the address bar?

Can you print out:

                          String fullpath = servlet.getServletContext().getRealPath( path + File.separator + filename );

Does it look right?

This sounds more like a view issue than a model/controller issue
0
 
mygreenislandAuthor Commented:
fullpath =
C:\Program Files\Apache Software Foundation\Tomcat
6.0\webapps\iusc\assets\images\events\2005\1\photo.jpg

url =
http://localhost:8080/iusc/assets/images/events/2005/1/photo.jpg

The weird thing is, that in the last five minutes it has started working without a problem, i haven't cleared any cache nor made any changes to the code..

Strange one eh..
0
 
KuldeepchaturvediCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.