如何保存上传的文件在JSF中 [英] How to save uploaded file in JSF
问题描述
< t:inputFileUpload>
,但同样的问题也适用于例如。 PrimeFaces < p:fileUpload>
和JSF 2.2 < h:inputFile>
。 b 我有下面的支持bean代码:
pre $ private $ UploadedFile uploadedFile; // + getter + setter
$ b $ public String save()throws IOException {
String name = uploadedFile.getName();
System.out.println(File name:+ name);
String type = uploadedFile.getContentType();
System.out.println(File type:+ type);
long size = uploadedFile.getSize();
System.out.println(File size:+ size);
InputStream stream = uploadedFile.getInputStream();
byte [] buffer = new byte [(int)size];
stream.read(buffer,0,(int)size);
stream.close();
$ b我可以得到文件名,类型和大小,但是我无法保存此文件在特定的路径。我无法弄清楚保存上传文件的正确方法。如何才能达到这个目的?
InputStream input = uploadedFile.getInputStream();
您需要将其复制到一个文件中。您应该先在本地磁盘文件系统上准备一个文件夹,上传的文件应该存储在该文件夹中。例如, / path / to / uploads
(在Windows上,与服务器运行在同一个磁盘上)。请注意,由于在此提及的原因,绝对不 通过使用相对路径或 getRealPath()
将文件存储在展开的WAR文件夹中上传的图片只有刷新页面后才可用。
然后,您需要自动生成文件名。否则,当其他人稍后上传一个同名的文件时,它将被覆盖。您可以使用 Files#createTempFile()
设施获得一个自动生成的文件名。
Path folder = Paths.get(/ path / to / uploads);
String filename = FilenameUtils.getBaseName(uploadedFile.getName());
字符串扩展= FilenameUtils.getExtension(uploadedFile.getName());
路径文件= Files.createTempFile(文件夹,文件名+ - ,。+扩展名);
如果需要,上传路径可以根据本Q& A :将上传的文件保存在servlet应用程序中的推荐方法。 FilenameUtils
是Apache Commons IO的一部分,因为它是Tomahawk文件上传组件的依赖关系,所以您应该已经在您的类路径中拥有了它们。
最后,将上传的文件流式传输到该文件中(假设Java 7):
$ b $ $ p $ try(InputStream input = uploadedFile.getInputStream()){
Files.copy(输入,文件,StandardCopyOption.REPLACE_EXISTING);
System.out.println(上传的文件成功保存在+文件中);
然后下载它,最简单的方法是注册 / path /将/ uploads
作为新的Web应用程序上下文或虚拟主机,以便所有这些文件都可以通过URL访问。另请参阅使用webapps / webcontext / deploy文件夹外部的图像< H:graphicImage的>或< img>标记。
I am uploading a file in JSF. I am using Tomahawk's <t:inputFileUpload>
for this, but the same question is applicable on e.g. PrimeFaces <p:fileUpload>
and JSF 2.2 <h:inputFile>
.
I have the below backing bean code:
private UploadedFile uploadedFile; // +getter+setter
public String save() throws IOException {
String name = uploadedFile.getName();
System.out.println("File name: " + name);
String type = uploadedFile.getContentType();
System.out.println("File type: " + type);
long size = uploadedFile.getSize();
System.out.println("File size: " + size);
InputStream stream = uploadedFile.getInputStream();
byte[] buffer = new byte[(int) size];
stream.read(buffer, 0, (int) size);
stream.close();
}
I am able to get the file name, type and size, but I am unable to save this file at a specific path. I can't figure out the proper way to save the uploaded file. How can I achieve this?
The getInputStream()
method of the uploaded file represents the file content.
InputStream input = uploadedFile.getInputStream();
You need to copy it to a file. You should first prepare a folder on the local disk file system where the uploaded files should be stored. For example, /path/to/uploads
(on Windows, that would be on the same disk as where the server runs). Note that you should absolutely not store the files in expanded WAR folder by using a relative path or getRealPath()
for the reasons mentioned here Uploaded image only available after refreshing the page.
Then, you need to autogenerate the filename. Otherwise, when someone else uploads a file with coincidentally the same name later, it would be overwritten. You could use Files#createTempFile()
facility to get an autogenerated filename.
Path folder = Paths.get("/path/to/uploads");
String filename = FilenameUtils.getBaseName(uploadedFile.getName());
String extension = FilenameUtils.getExtension(uploadedFile.getName());
Path file = Files.createTempFile(folder, filename + "-", "." + extension);
The path to uploads could if necessary be parameterized based on one of several ways shown in this Q&A: Recommended way to save uploaded files in a servlet application. The FilenameUtils
is part of Apache Commons IO which you should already have in your classpath as it's a dependency of the Tomahawk file upload component.
Finally, just stream the uploaded file to that file (assuming Java 7):
try (InputStream input = uploadedFile.getInputStream()) {
Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
}
System.out.println("Uploaded file successfully saved in " + file);
Then, to download it back, easiest would be to register /path/to/uploads
as a new webapp context or a virtual host so that all those files are available by an URL. See also Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag.
这篇关于如何保存上传的文件在JSF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!