-1

I am able to upload a picture in my webapp with the struts2 framework, but i am not able to understand the path. how to get the path of the image as a URL, so that i can use it for further processing in <img src="url"/>.

This is my action class source code and i have mentioned the URL returned in comments, but the URL does not make any sense to me. How can i decrypt it to actual URL ?

public class AddItemAction extends ActionSupport implements
        ServletContextAware {

    @Override
    public void setServletContext(ServletContext arg0) {
        // TODO Auto-generated method stub

    }

    File pic;
    String picContentType;
    String picFileName;

    public File getPic() {
        return pic;
    }

    public void setPic(File pic) {
        this.pic = pic;
    }

    public String getPicContentType() {
        return picContentType;
    }

    void setPicContentType(String picContentType) {
        System.out.println("Setting conteent tuype" + picContentType);
        this.picContentType = picContentType;
    }

    public void setPicFileName(String picFileName) {
        this.picFileName = picFileName;
    }

    public String getPicFileName() {
        return picFileName;
    }

    public String execute() {
        File file = getPic();
        String strFinalFullPathFileName = file.getAbsolutePath() + File.separator + picFileName;
        System.out.println(strFinalFullPathFileName);

        // This is the path returned
        /*
         * /Users/..../Catalina/localhost/.../upload_584d2719_13d5fdf593d__8000_00000000.tmp/IMG_20120526_083438.jpg
         * 
         * 
         */

        return SUCCESS;
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Arun Abraham
  • 4,011
  • 14
  • 54
  • 75
  • 1
    Create an action which fetches the picture and use that as the url. – Quaternion Mar 12 '13 at 18:46
  • @Quaternion Did you mean getPic(); but when i try to do getPic().getPath(), it gives me the os file path i.e. /Users/... – Arun Abraham Mar 12 '13 at 18:48
  • Don't mention the actual URL in the comments, update the question. – Dave Newton Mar 12 '13 at 18:52
  • 1
    Create a new action, which reads from a text file and prints the content from some location in your file system. Now you can read files. Next create an action which uses the stream result to send back the picture. I store my images in a database because I already need to set it up and it reduces application configuration between systems. If your application is under very heavy load database could be performance tuned. You generally need to put meta data into the db anyways (content type, who owns the data,...) – Quaternion Mar 12 '13 at 18:53

2 Answers2

0

Uploaded artifacts should be stored outside the web app structure.

In addition, by default, the file upload interceptor deletes the temporary files created during the upload process. That should either be turned off, or the file should be copied to a known location so they can be either (a) streamed back via an action, or (b) served directly if you set up your container to serve static assets outside of the normal web structure.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

seems like you are uploading your file to a temp folder, what you should do is move this file to a folder inside your web app

you case use request.getServletContext().getRealPath(YOUR_PATH) to get a path where to move the file

YOUR_PATH being something like "/uploadimage/img.png" => uploadimage being a folder directly in you webapp

Jerome Diaz
  • 1,746
  • 8
  • 15
  • the url to use is then YOUR_CONTEXT_PATH_IF_ANY + "/uploadimage/img.png" – Jerome Diaz Mar 12 '13 at 18:55
  • It's almost never a good idea to put uploaded artifacts *inside* the web app structure. Indeed, if you're deploying a war file (the most common scenario), you *cannot* move things into the web app. – Dave Newton Mar 12 '13 at 18:56