Convert an UploadedFile into a File

℡╲_俬逩灬. 提交于 2019-12-05 02:09:03

I met the similar problem before, it is very simple, You just need to create a new File for it. But firstly, you should confirm the path you want to write is available. Here is the sample code

//This is used for new file creation.
File f = new File(ParentPath, FileName);
FileOutputStream fos = new FileOutputStream(f);

Or you can just use java.nio.Files for it.

Files.newOutputStream(path, options);

Please check javadoc for it.

try {
            moveFile(file.getInputstream(), file.getFileName());
        } catch (IOException e1) {
            System.out.println(e1);
        }


private Boolean moveFile(InputStream inputStream, String name) {

    OutputStream outputStream = null;
    String path = "/Users/barry/Desktop/";
    Boolean flag = false;
    try {

        outputStream
                = new FileOutputStream(new File(path + name));

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

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

        System.out.println("Done!");
        flag = true;
    } catch (IOException e) {
       flag = false;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                // outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    return flag;
}

From the primefaces example. https://www.primefaces.org/showcase/ui/file/upload/basic.xhtml

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
public class FileUploadView {

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

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

public void upload() throws IOException {
    if (file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);

        System.out.println("Uploaded file now: " + file.getFileName());

        String name = file.getFileName();

        InputStream inputStream = file.getInputstream();
        OutputStream outputStream = null;
        String path = "/Users/daryl/Desktop/";

        File file = new File(path + name);

        outputStream = new FileOutputStream(file);

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

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

        System.out.println("Done!");

    }
}
}

You can also use this code for converting an UploadedFile into a java File.

File f = new File(uploadedFile.getFileName());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!