p:fileUpload上传的文件保存在哪里?如何更改? [英] Where is the p:fileUpload uploaded file saved and how do I change it?

查看:168
本文介绍了p:fileUpload上传的文件保存在哪里?如何更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Primebe的简单文件上传与Netbeans进行开发。我的测试示例与Primefaces手册相似。

我的问题:文件在本地计算机上传到哪里?我怎样才能改变它的路径? Thx!



jsf档案:

 < ;?xml version ='1.0'encoding ='UTF-8'?> 
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
xmlns:h =http://java.sun.com/jsf/html
xmlns:p =http://primefaces.org/ui>
< h:head>
< title>页面测试< / title>
< / h:头>
< h:body>
您好!我的第一个JSF生成的页面!
< h:form enctype =multipart / form-data>
< p:fileUpload value =#{fileBean.file}mode =simple/>
< p:commandButton value =Submitajax =false/>
< / h:表格>

< / 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
公共类FileBean {

私有的UploadedFile文件;
$ b public FileBean(){
}

public UploadedFile getFile(){
return file;
}

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


$ b $ / code $ / $ p

解决方案

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

你根本不用担心这个。 servlet容器和PrimeFaces完全知道他们在做什么。您应该在命令按钮的操作方法中将上传的文件内容保存到 you 需要的位置。你可以通过 UploadedFile#getInputStream()或者作为 InputStream > byte [] by UploadedFile#getContents()(获取 byte [] 潜在的内存在大文件的情况下是很昂贵的,你知道,每个 byte 吃一个字节的JVM内存,所以在大文件的情况下不要这样做)。 >



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



 私人UploadedFile上传文件; 
$ b $ 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(输出);




$ FilenameUtils 和 IOUtils 来自Commons IO,为了得到< p:fileUpload> 工作)



要生成唯一的文件名,您可以找到 $ File $ createTempFile() 设施很有帮助。

$ p $ FilenameUtils.getName(uploadedFile.getFileName());
String basename = FilenameUtils.getBaseName(filename)+_;
字符串扩展=。 + FilenameUtils.getExtension(filename);
File file = File.createTempFile(basename,extension,/ path / to / uploads);
FileOutputStream output = new FileOutputStream(file);
// ...


I use the simple File upload of Primefaces in development with Netbeans. My test example is similar to to the Primefaces manual.
My question: where does the file get uploaded on my local computer? How can I change the path for it? Thx!

The jsf file:

<?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>

and the managed 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;

    }
}

解决方案

It's by default saved in either the servlet container's memory or the temp folder, depending on the file size and the Apache Commons FileUpload configuration (see also "Filter Configuration" section of the <p:fileUpload> chapter in PrimeFaces User's Guide).

You shouldn't worry about this at all. The servlet container and PrimeFaces know exactly what they do. You should in the command button's action method actually be saving the uploaded file contents to the location where you need it to be. You can get the uploaded file contents as an InputStream by UploadedFile#getInputStream() or as a byte[] by UploadedFile#getContents() (getting a byte[] is potentially memory expensive in case of large files, you know, each byte eats one byte of JVM's memory, so don't do that in case of large files).

E.g.

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

with

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 and IOUtils are from Commons IO which you should anyway already have installed in order to get <p:fileUpload> to work)

To generate unique file names, you may find File#createTempFile() facility helpful.

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

这篇关于p:fileUpload上传的文件保存在哪里?如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆