0

I am running Eclipse Java EE and tomcat for running my webapp. I used the following code to store an image file to the upload/images/profilepics directory:

public String uploadPhoto() {
    try {
        //get path to upload photo
        String filePath = servletRequest.getSession().
                getServletContext().getRealPath("/uploads/profilepics");

        System.out.println("Server path:" + filePath);

        //creating unique picture name
        Map sess = (Map) ActionContext.getContext().get("session");
        Integer uid = (Integer) sess.get("uid");
        String profilePictureName = uid + "-" + 
                MyUtilityFunctions.createVerificationUrl() + this.userImageFileName;

        //update user record
        //tobe done 
        String imgUrl = filePath + profilePictureName;
        ViewProfileModel pofilePictureUpdate = new ViewProfileModel();
        pofilePictureUpdate.updateUserPhotoUrl(imgUrl, uid);

        //create new File with new path and name
        File fileToCreate = new File(filePath, profilePictureName);

        //copy file to given location and with given name
        FileUtils.copyFile(this.userImage, fileToCreate);
    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }
    return SUCCESS;
}

after printing filePath I got the following result:

Server Path: /home/bril/webspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/picvik/uploads/profilepics

Now the problem is, I am not able to get the image or if I give the same url to <img src=""> nothing is getting displayed.

Please correct where I am doing wrong.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Ananda
  • 1,572
  • 7
  • 27
  • 54

1 Answers1

1

There are suggestions:

  1. there are lots of reason, that you shouldn't save user images in this way, just like @DaveNewton mentioned in another question. There are some post to help you make your decision:

    My personal opinion is to save them into DB, because you don't want to let your user lost their images.

  2. If you need access session, you can check out SessionAware. This should be a better way to access session.
  3. You are using tomcat as application container, you can configure the server to use its local installation, which makes you easier to track the problem in this case. check out this picture belowTomcat Server location

Back to your question, There are different ways to do this:

  • if you cannot find the image user just uploaded, you can check it manual, see 3.
  • Otherwise, you could try <img src="/uploads/profilepics/<s:property value='profilePictureName'/>"
  • Or you can get this picture using stream, here is the snippet:

JSP:

    <img src="
        <s:url var="profilePic" action="customer-image-action">
            <s:param name="uid" value="%{uid}"/>
        </s:url>
    " alt="kunden logo" />

Action:

public String execute() throws Exception {
    // filename = somehow(uid);
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
    imgPath = request.getSession().getServletContext().getRealPath("/uploads/profilepics/")+filename;
    log.debug("context-path: " + imgPath);
    try {
        inputStream = FileUtils.openInputStream(new File(imgPath));
    } catch (IOException e) {
        log.error(e.getCause(), e);
    }
    return SUCCESS;
}
Community
  • 1
  • 1
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53