13

我在 Netbeans 开发中使用 Primefaces 的简单文件上传。我的测试示例类似于 Primefaces 手册。
我的问题:文件在我的本地计算机上上传到哪里?我怎样才能改变它的路径?谢谢!

.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;

    }
}
4

2 回答 2

21

默认情况下,它保存在 servlet 容器的内存或临时文件夹中,具体取决于文件大小和 Apache Commons FileUpload 配置(另请参阅PrimeFaces 用户指南<p:fileUpload>中章节的“过滤器配置”部分)。

你根本不应该担心这个。servlet 容器和 PrimeFaces 确切地知道它们在做什么。您应该在命令按钮的操作方法中实际将上传的文件内容保存到需要的位置。您可以将上传的文件内容以InputStreambyUploadedFile#getInputStream()byte[]by 的形式UploadedFile#getContents()获取(在大文件的情况下获取 abyte[]可能会占用大量内存,您知道,每个byte占用 JVM 内存的一个字节,因此在大文件的情况下不要这样做) .

例如

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

FilenameUtils并且IOUtils来自Commons IO,无论如何您都应该已经安装它才能开始<p:fileUpload>工作)

要生成唯一的文件名,您可能会发现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);
// ...
于 2012-08-06T14:20:03.210 回答
-1

我使用了简单模式,GlassFish 4.0 和 PrimeFaces 4.0

支持豆

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}"/> 
于 2014-03-21T12:50:42.683 回答