问题描述
我在这里讨论了在primefaces上上传图片的大部分帖子。有了这个帮助,我已经能够将图像上传到代码中静态指定的目标路径,如本文所示。 。这工作正常。但是,我想上传一个图像到web.xml中指定的desitnation路径。这是因为我希望即使在部署应用程序之后,路径也是可配置的。当我使用ServletContext#getRealpath()时,返回路径在myProject文件夹中,但是我希望目标路径完全在项目外部,因为我已经发现它是最好的方法。例如E:/ myUploads
这是我的web.xml
$ p $ <滤光器>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< filter-class> org.primefaces.webapp.filter.FileUploadFilter< / filter-class>
< init-param>
< param-name> thresholdSize< / param-name>
<参数值> 51200< /参数值>
< / init-param>
< init-param>
< param-name> uploadDirectory< / param-name>
< param-value> E:/ myUploads< / param-value>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>
这是我的bean。 / b>
public void handleFileUpload(FileUploadEvent event){
//获取事件上传的文件
UploadedFile uploadedFile = (UploadedFile)event.getFile();
//从上传的文件中创建一个InputStream
InputStream inputStr = null;
try
{
inputStr = uploadedFile.getInputstream();
} catch(IOException e){
// log error
}
ServletContext servletContext =(ServletContext)FacesContext.getCurrentInstance()。getExternalContext()。getContext );
String uploadPath = servletContext.getRealPath();
文件destFile =新文件(uploadPath);
//使用org.apache.commons.io.FileUtils复制文件
尝试{
FileUtils.copyInputStreamToFile(inputStr,destFile);
} catch(IOException e){
// log error
}
FacesMessage msg = new FacesMessage(event.getFile()。getFileName()+is uploaded。 ;
FacesContext.getCurrentInstance()。addMessage(null,msg);
}
我的愿望是将上传的图像保存在E:/ myUploads中,而不必说:
字符串destPath =E:\\myUploads\\+ uploadedFile.getFileName();
如果您还向我展示如何使用
显示上传的图片, b$ b
< p:graphicImage
完全清除它,并创建一个独立的上下文参数。
<的context-param>
< param-name> uploadDirectory< / param-name>
< param-value> E:/ myUploads< / param-value>
< / context-param>
然后您可以使用
String directory = externalContext.getInitParameter(uploadDirectory);
String filename = FilenameUtils.getName(uploadedFile.getFileName());
文件文件=新文件(目录,文件名);
// ...
您应该永远不要使用 getRealPath()
。另外一个用户偶然上传一个文件的时候,你也遇到了另一个潜在的问题。您可以使用 File#createTempFile()
自动生成固定前缀/后缀的唯一文件名。另请参阅。
I have consulted most of the posts here on stackoverflow on uploading images in primefaces. With this help, I have been able to upload an image to a destination path statically specified in code as shown in this post. save image file in specific directory jsf primefaces project. This works fine.
However, I wish to upload an Image to a desitnation path specified in web.xml. This is because I want the path to be configurable even after the application is deployed. When I use ServletContext#getRealpath(), the return path is with in the myProject folder, but I want the destination path to be completely external to the project since I have found it as the best way. e.g E:/myUploads
This is my web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>E:/myUploads</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
This is my bean.
public void handleFileUpload(FileUploadEvent event){
//get uploaded file from the event
UploadedFile uploadedFile = (UploadedFile) event.getFile();
//create an InputStream from the uploaded file
InputStream inputStr = null;
try
{
inputStr = uploadedFile.getInputstream();
} catch (IOException e) {
//log error
}
ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
String uploadPath = servletContext.getRealPath("");
File destFile = new File(uploadPath);
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
//log error
}
FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
My desire is to save the Uploaded Image in E:/myUploads without having to say:
String destPath = "E:\\myUploads\\" + uploadedFile.getFileName();
I will be glad if you also show me how to display the uploaded images using
<p:graphicImage
The uploadDirectory
initialization parameter represents the temporary file storage location for the case the uploaded file is larger than the configured threshold size. The configured directory is not intented as a permanent file storage location. It will be auto-cleaned at moments beyond your control.
Get rid of it altogether and create an independent context parameter instead.
<context-param>
<param-name>uploadDirectory</param-name>
<param-value>E:/myUploads</param-value>
</context-param>
Then you can use
String directory = externalContext.getInitParameter("uploadDirectory");
String filename = FilenameUtils.getName(uploadedFile.getFileName());
File file = new File(directory, filename);
// ...
You should indeed never never use getRealPath()
. You've by the way another potential problem when another user happens to upload a file with coincidentally the same filename. You can use File#createTempFile()
to autogenerate unique filenames with a fixed prefix/suffix. See also How to save uploaded file in JSF.
这篇关于使用primefaces将图像上传到web.xml中设置的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!