我正在玩JSF并希望将图片上传到Wildfly服务器上的特定目录。我没有使用PrimeFaces。我希望用普通的JSF 2.2来完成这项工作。这是我的xhtml代码:
<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>
这是控制器类的一个问题:
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;
}
我希望我的代码可以将上传的图像保存在此屏幕截图中标记的封面目录中:
因此,每次上传图像时,目录src / main / webapp / resources / covers都应填充图像。我尝试了很多路径,但我总是得到&#34;系统无法找到给定的路径&#34;。
15:14:18,882 ERROR [stderr] (default task-121) java.io.FileNotFoundException: \covers\drSleep.jpg (Das System kann den angegebenen Pfad nicht finden)
任何人都可以帮我吗?