6 Replies Latest reply on Jul 23, 2003 9:05 AM by Kelly MacInnis

    Image Upload blues

    Jon Ferguson Newbie

      I've got an app that allows users to upload images which then can be viewed. Deploying this is easy when I front JBoss with Apache because then I have a directory that is seen by the server and into which I can upload the images...

      However, deploying this on JBoss alone leaves me guessing, since it doesn't unjar the .war file. Is there somewhere I can put an image dynamically and get JBoss to see and associate it with a particular web context?

      Thanks in advance,

      Jon

        • 1. Re: Image Upload blues
          Kelly MacInnis Newbie

          Greetings,

          I have a similar requirement. Did you find a way to do this? It doesn’t look like you ever got a response.

          Kelly

          • 2. Re: Image Upload blues
            Jon Barnett Master

            We cheat and use jspSmartUpload that handles all that file upload stuff. You can get it from http://www.jspsmart.com. It takes care of everything, and we don't have to worry about where the file actually manifests itself. We've been using for a couple of years now in a variety of environments - standalone, embedded, Jetty/Tomcat.

            • 3. Re: Image Upload blues
              Jon Barnett Master

              With the smartupload facilities, you can save it to a physical context easily. You would need to create this physical context, but I'm not sure how you would do it otherwise. For example, create an upload context (unless you have security issues). Your other option is to deploy an unpacked WAR.

              We tend to store these things in a database (not hsqldb) and smartupload helps in this case. You can then build a display servlet fairly quickly. However, it depends on your access/retrieval requirements.

              • 4. Re: Image Upload blues
                Kelly MacInnis Newbie

                Thanks for your reply,

                I have the upload part figured out. I just drop the file in a temp directory on the server from my request controller and pass the location and original name on to the handler for the form. The handler moves the file wherever it needs it and adds data to the database about it. Since people have to find these files to work with them, and there are a lot of them, keeping them organized is a big requirement.

                The part that has got me stumped is displaying them so users can see what they have. Since I can’t just point to the file (packed WAR), and security is an issue, maybe I can read the file from the jsp page and write the data out using the writer. Does this sound reasonable? How do I tell the browser what format the data is in (gif, jpg, etc.)?

                Kelly

                • 5. Re: Image Upload blues
                  Jon Barnett Master

                  You can do that, although we do it with a servlet and our source is the DB. File display may be a little slower, and depending on load may not be suitable. You probably need to trial it and see.

                  You tell the browser via the content type setting. You'd probably need to detect the file extension. We do it this way in our servlet (modified for bits you are going to do as we don't actually deal with files):

                  private HashMap hash = null;

                  /**
                  * Init the servlet
                  */
                  final public void init(ServletConfig config) throws ServletException
                  {
                  hash = new HashMap();
                  hash.put("gif", "image/gif");
                  hash.put("jpg", "image/jpeg");
                  // Add other appropriate mapping for bin types
                  }

                  public final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
                  {
                  String filename = request.getParameter("filename");
                  ServletOutputStream out = response.getOutputStream();

                  // Implement get file and extension

                  if (file exists) <-- appropriate test
                  response.setContentType((String)hash.get(extension));
                  else
                  {
                  response.setContentType("image/gif");
                  // Do something appropriate to do generate
                  // browser friendly (maybe error gif)
                  try
                  {
                  out.write(gifImage);
                  }catch(Exception e)
                  {}
                  return;
                  }
                  try
                  {
                  if (data.length > 0)
                  out.write(file stream);
                  }
                  catch(Exception e)
                  {
                  System.out.println(e.toString());
                  e.printStackTrace();
                  out.println("" + e.toString());
                  }
                  }

                  So if this servlet is mapped as /Image

                  You would have in a calling page:


                  You'd need to have some way of knowing what the name of the picture is the user uploaded.

                  Another of the reasons we use JDBC storage is that we can map user to image and not have other users access the picture and users don't overwrite the picture of another user. Smartupload also makes sure there are no clashes when the files are uploaded and temporarily materialised.

                  Hmm, giving away all our commercial secrets.

                  • 6. Re: Image Upload blues
                    Kelly MacInnis Newbie

                    This is the part that I couldn't quite put together. Thank you. I will let you know how it goes!

                    Kelly