0

Hi i am trying to save the uploaded file to a folder in my project but i am not not sure how to give the path. I also want the same name of the uploaded file to be the name of the file stored in the folder.

  public void handleFileUpload(FileUploadEvent event) throws IOException {
        LOG.info("Entered into save action event");
        FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        String filename = event.getFile().getFileName();
        UploadedFile file = event.getFile();
        InputStream input = file.getInputstream();
        OutputStream output = new FileOutputStream(new File("src/main/resources/uploads/schema.xsd", filename));
        try {
            IOUtils.copy(input, output);
        } catch (IOException e){
            LOG.error("Error in copying file.", e);
        }
        finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    }
kish
  • 85
  • 1
  • 13

1 Answers1

0

Your best bet for doing that is making a folder with an absolute path from the Root of the server. This means C: if you are using Windows or / if you are using Linux. For example in a Linux Server you can do this:

OutputStream output = new FileOutputStream(new File("/var/my_app/uploads/" + filename + "/" + filename + ".xsd"));
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
  • The thing is i can save it on desktop but i want to save it to a floder on eclps so that i can access it at run time i have an xsd file which will be importing another xsd file from the same location . If i am trying to save it on the desktop or C: it is not importing . SO i am trying to write the xsd to a folder. – kish Nov 11 '15 at 14:58
  • How are you doing that? Loading files from classpath at runtime will require a custom classloader. Take a look at this: http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – Aurasphere Nov 11 '15 at 22:32