使用primefaces将图像上载到web.xml中设置的Path

时间:2013-12-06 08:46:37

标签: file-upload jsf-2 primefaces

我在stackoverflow上咨询了大部分关于在primefaces上传图片的帖子。有了这个帮助,我已经能够将图像上传到代码中静态指定的目标路径,如本文所示。 save image file in specific directory jsf primefaces project。这很好。

但是,我希望将Image上传到web.xml中指定的desitnation路径。这是因为我希望即使在部署应用程序之后也可以配置路径。当我使用ServletContext#getRealpath()时,返回路径在myProject文件夹中,但我希望目标路径完全在项目外部,因为我发现它是最好的方法。例如E:/ myUploads

这是我的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>

这是我的豆。

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);
}

我希望将上载的图像保存在E:/ myUploads中,而不必说:

String destPath = "E:\\myUploads\\" + uploadedFile.getFileName(); 

如果您还告诉我如何使用

显示上传的图像,我将很高兴
<p:graphicImage

2 个答案:

答案 0 :(得分:1)

uploadDirectory初始化参数表示上传文件大于配置的阈值大小的情况下的临时文件存储位置。配置的目录不是永久文件存储位置。它会在您无法控制的时刻自动清洁。

完全摆脱它并创建一个独立的上下文参数。

<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());
File file = new File(directory, filename);
// ...

你确实应该从不使用getRealPath()。当另一个用户碰巧上传一个恰好具有相同文件名的文件时,你会遇到另一个潜在的问题。您可以使用File#createTempFile()自动生成具有固定前缀/后缀的唯一文件名。另请参阅How to save uploaded file in JSF

答案 1 :(得分:1)

BalusC答案给了我很多帮助......再次感谢BalusC。如果任何人对最终代码感兴趣,那么它就是......

使用工具Netbeans 7.4,Primefaces 4.0,GlassFish 3.1.2

这是我的上传表格。

<h:form  enctype="multipart/form-data">  
        <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" mode="advanced" dragDropSupport="false"  
                      update="messages" sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />  
        <p:messages id="messages" showDetail="true"/>  

    </h:form>  

我的lib

中有Apache Commons库
commons-io-2.4.jar  (http://commons.apache.org/io)
commons-fileupload-1.3.jar  (http://commons.apache.org/fileupload)

我在我的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>
</filter>

<context-param>
    <param-name>uploadDirectory</param-name>
    <param-value>E:/myUploads</param-value>
</context-param>

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

这是我的FileUploadController

@ManagedBean(name = "fileUploadController")
@ViewScoped
public class FileUploadController {

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
    }

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String directory = externalContext.getInitParameter("uploadDirectory");
    String filename = FilenameUtils.getName(uploadedFile.getFileName());
    File destFile = new File(directory, filename);

    //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);
}

}

这很有效。一切顺利。