1

In my current spring, some forms have a field where the user can upload a file, which should be saves inside the folder resources from project (src/main/resources, following the structure created by maven).

the method responsible for save the file in this folder is working ok until this line:

URL path = this.getClass().getClassLoader().getResource(file);

the variable path is staying with the value null. the variable file is defined with this value:

String file = "/"+this.getName()+"/"+String.valueOf(id)+"/picture.jpg";

Anyone can tell me the right way to do this?

PS.: the complete code for this method is:

public boolean upload_picture(E e, MultipartFile f) {
    Integer id = null;

    if(f == null) {
        return false;
    }

    Class<?> clazz = e.getClass();
    Method methods[] = clazz.getDeclaredMethods();
    for(int i=0; i<methods.length; i++) {
        if(methods[i].getName().equals("getId")) {
            try {
                id = (Integer) methods[i].invoke(e);
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
                return false;
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
                return false;
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
                return false;
            }
        }
    }

    BufferedImage src;
    try {
        src = ImageIO.read(new ByteArrayInputStream(f.getBytes()));
    } catch (IOException e3) {
        e3.printStackTrace();
        return false;
    }

    String file = "/"+this.getName()+"/"+String.valueOf(id)+"/picture.jpg";
    URL path = this.getClass().getClassLoader().getResource(file);
    File destination;
    try {
        destination = new File(path.toURI());
    } catch (URISyntaxException e2) {
        destination = new File(path.getPath());
        e2.printStackTrace();
    }

    try {
        ImageIO.write(src, "jpeg", destination);
        return true;
    } catch (IOException e1) {
        e1.printStackTrace();
        return false;
    }
}
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • i don't see structure – jmj Jul 16 '14 at 20:48
  • You shouldn't really be accessing the source directories of your project at runtime. Anything you add to resources will be included in your next build and copied onto the WEB-INF/classes directory, which isn't available to your html. This seems like a poor design. – Software Engineer Jul 16 '14 at 21:05
  • @EngineerDollery what the best place to save this files then? – Kleber Mota Jul 16 '14 at 21:06
  • You could read: http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet – Software Engineer Jul 16 '14 at 21:08
  • @EngineerDollery Is there any way to avoid the operational system syntax for the file path? I want enable the user to install the web application in any machine he wants. – Kleber Mota Jul 16 '14 at 21:20
  • That's another reason not to store images in your source directory -- the user won't have the source directory, only the web-app. And no, there has to be a filesystem path in your case. Or, I suppose you could store the images in a database. – Software Engineer Jul 16 '14 at 21:37

0 Answers0