0

I'm playing a little bit with JSF and want to upload a picture to a specific directory on my Wildfly server. I'm not using PrimeFaces. I'd like to get this done with plain JSF 2.2. This is my xhtml code:

<h:body> 
<h:form id="book_input" class="book_input" enctype="multipart/form-data">
    <h:panelGrid columns="3">
        <h:outputLabel for="title" value="Titel:" />
        <h:inputText id="title" value="#{book.title}" />
        <h:message for="title" errorClass="invalid" />

        <h:outputLabel for="author" value="Author:" />
        <h:inputText id="author" value="#{book.author}" />
        <h:message for="author" errorClass="invalid" />

        <h:outputLabel for="isbn" value="ISBN:" />
        <h:inputText id="isbn" value="#{book.isbn}" />
        <h:message for="isbn" errorClass="invalid" />

        <h:outputLabel for="prize" value="Preis:" />
        <h:inputText id="prize" value="#{book.prize}" />
        <h:message for="prize" errorClass="invalid" />          

        <h:outputLabel for="cover" value="Buchcover:" />
        <h:inputFile id="cover" value="#{bookCreatorController.cover}" />
        <h:commandButton action="#{bookCreatorController.upload()}" value="Hochladen" />

    </h:panelGrid>

    <p>
        <h:panelGrid columns="2">
            <h:commandButton action="#{bookCreatorController.createBook()}" value="Speichern" />
            <h:message styleClass="messages" errorClass="invalid" infoClass="valid" warnClass="warning" globalOnly="true"/>
        </h:panelGrid>
    </p>
</h:form>

and this is the part of the controller class which is the problem:

private String getFilename(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            filename = filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
            this.coverPath = "/covers/"+filename;    //Here should be the path to the desired directory!
            logger.info("Saving destination for uploaded file is: "+coverPath);
            return this.coverPath;
        }
    }
    return null;
}

What I want my code to do is to save the uploaded image in the covers directory marked in this screenshot:

enter image description here

So the directory src/main/webapp/resources/covers should be filled with images everytime I upload an image. I have tried a lot of paths but I always get "System cannot find given path".

15:14:18,882 ERROR [stderr] (default task-121) java.io.FileNotFoundException: \covers\drSleep.jpg (Das System kann den angegebenen Pfad nicht finden)

Can anyone help me with this?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Raistlin
  • 997
  • 2
  • 11
  • 33
  • You should account for the context path when setting your file path. Try using an absolute path first to make sure you are reaching the correct file. – Vasco Lameiras Jun 22 '17 at 13:40
  • You should **never** upload to folders in your webapp. https://stackoverflow.com/questions/19139426/how-to-write-a-file-to-resource-images-folder-of-the-app – Kukeltje Jun 22 '17 at 13:55
  • Okay, thank you for your help. So it's better to save the File as bytes in the database? Ich hope I could have avoided that – Raistlin Jun 22 '17 at 14:18
  • 1
    No that is not what is stated. You still can save it in a file somewhere, just outside the webapp (that is what is mentioned in the linked doc) – Kukeltje Jun 22 '17 at 15:54
  • Thank you very much. It seems like I've not thought that through. I definitely gained some Experience Points ;) – Raistlin Jun 22 '17 at 20:14

0 Answers0