如何使用JSF 2.2< h:inputFile>上传文件?保存的文件在哪里? [英] How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File?
问题描述
我希望能够在我的JSF2.2 web应用程序中上传文件,所以我开始使用新的< h:inputFile>
组件。
我唯一的问题是,如何指定位置,文件将保存在服务器中?我想把它们作为 java.io.File
实例。这必须在backing bean中实现,但是我不清楚如何实现。
JSF不会保存文件在任何预定义的位置。它基本上只是为您提供上传的文件风格的 javax.servlet.http.Part
实例,这些实例暂时存储在服务器内存和/或临时磁盘存储位置,您不必担心。
为了将其保存到所需位置,您需要通过 Part#getInputStream()
然后将其复制到 Path
表示位置。你可以用通常的方法在(ajax)动作(监听器)方法中做到这一点。下面是一个在HTML DOM change
事件中使用ajax监听器的例子:
< h:form enctype =multipart / form-data>
< h:inputFile value =#{bean.file}>
< / h:inputFile>
< / h:表格>
private part file; //输入文件,输入文件(上传文件),输入新文件(上传文件) ,filename).toPath());
}
catch(IOException e){
//显示面孔信息?
$ b $ / code> uploads
文件夹和文件名
完全由您控制。例如。 / path / to / uploads
和 Part#getSubmittedFileName()
。请记住,任何现有的文件将被覆盖,您可能需要使用 File#createTempFile()
自动生成一个文件名。
不要使用 Part#write()
。它将基本上重命名由 @MultipartConfig(位置)
标识的临时存储位置中的文件。为了将上传的文件保存在deploy文件夹中,也可以使用 not 使用 ExternalContext#getRealPath()
WAR被重新部署时,该文件将会丢失,原因很简单,因为该文件不包含在原始WAR中。始终将其保存在部署文件夹外部的绝对路径中。
对于现场演示,请检查 < o:graphicImage>
在OmniFaces showcase上的演示。
另见:
a>
将上传的文件保存在servlet应用程序中的推荐方法
I would like to be able to upload files in my JSF2.2 web application, so I started using the new <h:inputFile>
component.
My only question is, how can I specify the location, where the files will be saved in the server? I would like to get hold of them as java.io.File
instances. This has to be implemented in the backing bean, but I don't clearly understand how.
解决方案 JSF won't save the file in any predefined location. It will basically just offer you the uploaded file in flavor of a javax.servlet.http.Part
instance which is behind the scenes temporarily stored in server's memory and/or temporary disk storage location which you shouldn't worry about.
In order to save it to the desired location, you need to get the content by Part#getInputStream()
and then copy it to the Path
representing the location. You can do this in an (ajax) action (listener) method the usual way. Here's an example which does it with an ajax listener during the HTML DOM change
event:
<h:form enctype="multipart/form-data">
<h:inputFile value="#{bean.file}">
<f:ajax listener="#{bean.save}" />
</h:inputFile>
</h:form>
private Part file; // +getter+setter
public void save() {
try (InputStream input = file.getInputStream()) {
Files.copy(input, new File(uploads, filename).toPath());
}
catch (IOException e) {
// Show faces message?
}
}
The uploads
folder and the filename
is fully under your control. E.g. "/path/to/uploads"
and Part#getSubmittedFileName()
respectively. Keep in mind that any existing file would be overwritten, you might want to use File#createTempFile()
to autogenerate a filename.
Do not use Part#write()
as some prople may suggest. It will basically rename the file in the temporary storage location as identified by @MultipartConfig(location)
. Also do not use ExternalContext#getRealPath()
in order to save the uploaded file in deploy folder. The file will get lost when the WAR is redeployed for the simple reason that the file is not contained in the original WAR. Always save it on an absolute path outside the deploy folder.
For a live demo, check the last item of <o:graphicImage>
demo on OmniFaces showcase.
See also:
- Write file into disk using JSF 2.2 inputFile
- How to save uploaded file in JSF
- Recommended way to save uploaded files in a servlet application
这篇关于如何使用JSF 2.2< h:inputFile>上传文件?保存的文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!