1

I'm writing a Java EE application, and I try to get an image from an URL then save it in my resource folder (thru an AJAX request). My problem is if I don't reboot my server I'm not able to display this image because it isn't loaded on my server.

I'm using Tomcat 7, Spring, Hibernate, Primeface.

Here is my class to save myimage

public class ImageSaver {
    final static int SIZE=1024;

    public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
        OutputStream outStream = null;
        URLConnection  uCon = null;

        InputStream is = null;
        try {
            URL url = new URL(fAddress);
            byte[] buf;
            int byteRead;
            int byteWritten=0;
            outStream = new BufferedOutputStream(new FileOutputStream(destinationDir+"\\"+localFileName));
            uCon = url.openConnection();
            is = uCon.getInputStream();
            buf = new byte[SIZE];
            while ((byteRead = is.read(buf)) != -1) {
                outStream.write(buf, 0, byteRead);
                byteWritten += byteRead;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                is.close();
                outStream.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void fileDownload(String fAddress,String fileName, String destinationDir){
        int slashIndex =fAddress.lastIndexOf('/');
        int periodIndex =fAddress.lastIndexOf('.');
        //String fileName=fAddress.substring(slashIndex + 1);
        if (periodIndex >=1 &&  slashIndex >= 0 && slashIndex < fAddress.length()-1){
        }
        else{
            System.err.println("path or file name.");
        }
    }

}

and the way I call the function :

ImageSaver.fileDownload("http://www.mywebsite.com/myImage.jpg","myImage.jpg", "C:\\Users\\MyProject\\src\\main\\webapp\\resources\\images");

How can I automatically loaded on my serve my uploaded Image without any reboot ? Which file allows the configuration of an upload folder ? And how ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tako
  • 661
  • 12
  • 34

1 Answers1

3

You should not write images retrieved dynamically into your webapp's folder - this opens you up for a whole category of problems. Instead, save them to a folder outside of the appserver's root directory and create a download servlet that will serve these resources.

The danger otherwise is that you'll retrieve some jsp file from external sources, save them to your appserver and on download the appserver will happily execute it server side.

Assume your webapp's directory to be non-writeable to your webapp. This will also ease backup and updates: Imagine you'll need to install an update or migrate to a different server: The application you have on your server will only partially be contained in its *.war file. If there's an explicit resource directory, you can back this up independently (or put it on a network share drive)

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Well, I've already tried this method but I think not in a good way. How can I "connect" my folder to my webapp ? I mean if I store my image in _C:/image_ then if I use the path _C:/image/myimage.jpg_ to display it in my JSP (in fact I use XHTML but it's not the point), it will not work. I've read something about filter in _web.xml_ where we'ra able to specify an upload folder. Is it a good solution ? If yes how does it work ? Thanks for your answer – Tako Mar 21 '13 at 11:06
  • I think the response of my question is [here](http://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java/1812356#1812356) – Tako Mar 21 '13 at 15:27
  • yes, looks like an exact duplicate. Thanks for linking – Olaf Kock Mar 21 '13 at 16:37