2013-06-05 38 views
1

美好的一天!如何上傳文件而不將其轉爲臨時文件? (NetBeans JSF Primefaces)

我一直在使用可以上傳.csv,.jpeg/.jpg和.pdf文件的Netbeans,JSF和Primefaces製作一個簡單的Web應用程序。我製作了2個存儲在驅動器C中的文件夾:(上傳的文件夾和tmp文件夾)。

我分配了「上傳」文件夾上傳的文件的存儲位置和「TMP」爲上傳的文件的.TMP。我已經通過了很多問題線索和視頻教程,我正確地遵循了。

我還下載了commons fileupload和commons io並將其添加到庫中。它工作正常,它顯示它正在上傳,甚至看到我分配給它的文件夾上的.tmp文件。

但我看不到上傳的文件在我上傳的文件夾中。 所以,我的問題是, 如何將這些文件上傳到我的「上傳」文件夾中。

這裏是我的代碼:

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head> 
    <title>Facelet Title</title> 
</h:head> 
<h:body> 
    <h:form enctype="multipart/form-data" > 

     <p:fileUpload fileUploadListener="#{FileUploadControl.fileUploadControl}" 
         mode="advanced" 
         update="messages" 
         auto="true" 
         sizeLimit="10000000" 
         allowTypes="/(\.|\/)(gif|jpe?g|csv|pdf)$/" 
         /> 

     <!-- --> 

     <p:growl id="messages" showDetail="true"/> 
    </h:form> 
</h:body> 
</html>  

FileUploadControl.java

package controller; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.Serializable; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext; 
import org.primefaces.model.UploadedFile; 


@ManagedBean 
@SessionScoped 
public class FileUploadControl implements Serializable { 

private String destination = "C:\\uploaded\\"; 
private UploadedFile file; 

public UploadedFile getFile() { 
    return file; 
} 

public void setFile(UploadedFile file) { 
    this.file = file; 
} 

public FileUploadControl() { 
} 

public void TransferFile(String fileName, InputStream in) { 
    try { 
     OutputStream out = new FileOutputStream(new File(destination + fileName)); 
     int reader = 0; 
     byte[] bytes = new byte[(int) getFile().getSize()]; 
     while ((reader = in.read(bytes)) != -1) { 
      out.write(bytes, 0, reader); 
     } 
     in.close(); 
     out.flush(); 
     out.close(); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

public void upload() { 
    String extValidate; 
    if (getFile() != null) { 
     String ext = getFile().getFileName(); 
     if (ext != null) { 
      extValidate = ext.substring(ext.indexOf(".")+1); 
     } else { 
      extValidate = "null"; 

      if (extValidate.equals("pdf")) { 
       try { 
        TransferFile(getFile().getFileName(), getFile().getInputstream()); 
       } catch (IOException ex) { 
         Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE, null, ex); 
        FacesContext context = FacesContext.getCurrentInstance(); 
        context.addMessage(null, new FacesMessage("Wrong", "Error Uploading  file...")); 
       } 
       FacesContext context = FacesContext.getCurrentInstance(); 
       context.addMessage(null, new FacesMessage("Succesful",  getFile().getFileName() + "is uploaded.")); 
      } else { 
       FacesContext context = FacesContext.getCurrentInstance(); 
       context.addMessage(null, new FacesMessage("Wrong_ext", "only extension .pdf")); 
      } 
     } 
    } else { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     context.addMessage(null, new FacesMessage("Wrong", "Select File!")); 
    } 
} 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
</context-param> 

<!--File upload commons --> 
<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>C:\tmp</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
</filter-mapping> 
<!--File upload commons --> 

<servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 



<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>faces/index.xhtml</welcome-file> 
</welcome-file-list> 

謝謝您的答覆和幫助。期待它!

回答

1

的主要原因它的失敗截至現在,你有沒有約束value屬性來支持bean的變量,因此getFile()總是會返回null,upload不會做任何事情。

你仍然可能不會得到任何結果,因爲它看來,你試圖結合<p:fileUpload/>成分的兩種不同的操作模式。

  1. 簡單模式

    • 你不定義fileUploadListener
    • 您在您的支持bean定義組件並綁定到UploadedFile類型屬性上value屬性(你)
  2. 高級模式

    • 你不定義value屬性
    • 你定義它綁定到一個方法,在支撐bean一個fileUploadListener(你也有)
+0

謝謝主席先生!將在此工作。 – user2431653

+0

我上個月做過了,忘了在這裏發佈,我的錯誤是我忘了將commonsio文件夾和其他庫上載到項目文件中。它的工作順便說一句。謝謝! – user2431653

相關問題