0

I'm working with Tomcat servlets - jsp

Users upload images that I save in a directory under my web package (same directory for all my jsp pages) and then persist only the path.

When the image is uploaded, and I try to show it by a img tag, they don't appear. BUT if I update classes and resources or restart the server, without any code change, the image displays.

Is there any correct place to store images and avoid this problem? Or should I persist the image itself?

Thanks!

The code where I save the image:

@MultipartConfig
public class ChangeBrandImage extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws ServletException, IOException {

    DatabaseManager databaseManager = (DatabaseManager) httpServletRequest.getSession().getAttribute("databaseManager");


    Part filePart = httpServletRequest.getPart("image");
    InputStream fileContent = filePart.getInputStream();

    OutputStream outputStream = null;

    Admin admin = databaseManager.getAdminDAO().getAdminByEmail(((SimplePrincipal)
            httpServletRequest.getSession().getAttribute("org.securityfilter.filter.SecurityRequestWrapper.PRINCIPAL")).getName());


    String imageName = Long.toString(System.currentTimeMillis()) + ".jpg";
    String path = admin.getBrand().getFolder_path() + "/" + imageName ;

    try {

        // write the inputStream to a FileOutputStream
        outputStream =
                new FileOutputStream(new File(path));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = fileContent.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileContent != null) {
            try {
                fileContent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                // outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    Brand brand = admin.getBrand();

    BrandDAO brandDAO = databaseManager.getBrandDAO();
    brand.setBrand_photo(imageName);

    brandDAO.beginTransaction();
    brandDAO.update(brand);

    try {
        brandDAO.commitTransaction();
    } catch (DatabaseAccessFailException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } finally {
        httpServletResponse.sendRedirect("/secured/admins/adminIndex.jsp");
    }

}

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {
}

@Override
protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

}

}

How I see it:

HTML view

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3808750
  • 13
  • 2
  • 9
  • Could you post the code that saves the image? Maybe you forget to flush the file that you writing to disk. – nolexa Feb 04 '16 at 00:06
  • Code uploaded! @nolexa – user3808750 Feb 04 '16 at 00:12
  • When you upload the file, do you see it where it should be in your directory immediately after the upload? I mean using the file system tools. Can you verify that it contains a valid image? – nolexa Feb 04 '16 at 00:15
  • Yes.. I check the directory a second later and the file is there, and contains a valid image.. Also, I restart the server, without changing any code, and the image is displayed @nolexa – user3808750 Feb 04 '16 at 00:17
  • You checked that it has a correct size, and it's really an image? You can open it as an image? – nolexa Feb 04 '16 at 00:19
  • Just checked again.. I can open it, and it's in the correct size.. I opened it from finder and it works just as a regular image. @nolexa – user3808750 Feb 04 '16 at 00:21
  • Can you show us the HTTP error code that you get when you try to access one of the images in a browser. Maybe this has to do with the image files' permission. – Titus Feb 04 '16 at 00:26
  • What Tomcat do you use and how do you run it - standalone or from IDE? – nolexa Feb 04 '16 at 00:27
  • I don't get an HTTP error.. The image is just not displayed - I added a picture showing the html where the image should be @Titus – user3808750 Feb 04 '16 at 00:34
  • I'm using Tomcat 7 with Intellij 12 - I little bit outdated, I know. @nolexa – user3808750 Feb 04 '16 at 00:35
  • 1
    It can be a problem similar to this one http://stackoverflow.com/questions/9979981/tomcat-not-seeing-new-files Running the Tomcat from IDE can be tricky because Intellij may internally copy your webapp to some temporary location, which is not updated until you restart the application. Try to run Tomcat standalone and see if you get same problem. – nolexa Feb 04 '16 at 00:40
  • 1
    It is always advisable to have file upload outside the web application as in case of war redeployment you tend to loose the uploaded files. It is better to programmatically save uploaded files in local drive and access from there. The path can be accessed from property file instead of hard coding. – BK Elizabeth Feb 04 '16 at 06:19

0 Answers0