2012-08-06 48 views
10

我使用Primebe的简单文件上传与Netbeans进行开发。我的测试示例与Primefaces手册类似。
我的问题:哪里的文件让我的本地计算机上载?我如何改变它的路径?谢谢!p:fileUpload上传文件保存在哪里?我该如何改变它?

JSF的文件:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<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>Page test</title> 
    </h:head> 
    <h:body> 
     Hello! My first JSF generated page! 
     <h:form enctype="multipart/form-data"> 
      <p:fileUpload value="#{fileBean.file}" mode="simple" /> 
      <p:commandButton value="Submit" ajax="false"/> 
     </h:form> 

    </h:body> 
</html> 

和管理的Bean:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import org.primefaces.event.FileUploadEvent; 
import org.primefaces.model.UploadedFile; 


    @ManagedBean 

@RequestScoped 
public class FileBean { 

    private UploadedFile file; 

    public FileBean() { 
    } 

    public UploadedFile getFile() { 
     return file; 
    } 

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

    } 
} 

回答

18

它的默认保存在任何servlet容器的内存或临时文件夹,这取决于文件大小和Apache Commons FileUpload配置(另请参见PrimeFaces User's Guide中的<p:fileUpload>章节的“Filter Configuration”一节)。

你不应该担心这一点的。 servlet容器和PrimeFaces完全知道它们的功能。你应该在命令按钮的操作方法其实是可以保存上传文件的内容到需要它的位置。您可以通过UploadedFile#getInputStream()或作为byte[]UploadedFile#getContents()(得到一个byte[]是潜在的大文件的情况下,内存价格昂贵,要知道,每一个byte吃JVM的内存的一个字节,所以不这样做,让上传的文件内容作为InputStream在大文件的情况下)。

E.g.

<p:commandButton value="Submit" action="#{fileBean.save}" ajax="false"/> 

private UploadedFile uploadedFile; 

public void save() throws IOException { 
    String filename = FilenameUtils.getName(uploadedFile.getFileName()); 
    InputStream input = uploadedFile.getInputStream(); 
    OutputStream output = new FileOutputStream(new File("/path/to/uploads", filename)); 

    try { 
     IOUtils.copy(input, output); 
    } finally { 
     IOUtils.closeQuietly(input); 
     IOUtils.closeQuietly(output); 
    } 
} 

FilenameUtilsIOUtils是从你无论如何都应该已经安装,以获得<p:fileUpload>工作下议院IO)

生成唯一的文件名,您可能会发现File#createTempFile()设施很有帮助。

String filename = FilenameUtils.getName(uploadedFile.getFileName()); 
String basename = FilenameUtils.getBaseName(filename) + "_"; 
String extension = "." + FilenameUtils.getExtension(filename); 
File file = File.createTempFile(basename, extension, "/path/to/uploads"); 
FileOutputStream output = new FileOutputStream(file); 
// ... 
+0

谢谢,我感谢您的帮助!我得到了它的逻辑,它才起作用。 – seinecle 2012-08-06 14:26:33

+1

不客气。 – BalusC 2012-08-06 14:27:48

-1

我用简单的模式,GlassFish的4.0和PrimeFaces 4.0

辅助Bean

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

public void upload() { 

    System.out.println("uploading"); 
    if(file != null) { 
     System.out.println("the file is" +file); 
     FacesMessage msg = new FacesMessage("Succesful" + file.getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 

     try { 
      copyFile(file.getFileName(), file.getInputstream()); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    System.out.println("uploaf finished"); 
} 

public void copyFile(String fileName, InputStream in) { 
    try { 
     // write the inputStream to a FileOutputStream 
     OutputStream out = new FileOutputStream(new File(destination + fileName)); 

     int read = 0; 
     byte[] bytes = new byte[1024]; 

     while ((read = in.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 

     in.close(); 
     out.flush(); 
     out.close(); 

     System.out.println("New file created!"); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

JSF页面

<p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/> 
相关问题