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:
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?