0

我想将 byte[] 文件保存到特定目录中:我从这个方法中得到它:

public void setUploadedPicture(UploadedFile uploadedPicture)
{
    System.out.println("set : "+uploadedPicture.getFileName()+" size : "+uploadedPicture.getSize());        
    this.uploadedPicture = uploadedPicture;
}

我访问 byte[] :

uploadedPicture.getContents()

我测试了这个链接,但没有结果

如何将其保存到我的项目内部或外部的特定目录中

谢谢你

** * ** * ***编辑* ** * ** * ** * 这是有效的代码,但有时我有错误:

public void setUploadedPicture(UploadedFile uploadedPicture)
{
    System.out.println("set : "+uploadedPicture.getFileName()+" size : "+uploadedPicture.getSize());        
    this.uploadedPicture = uploadedPicture;

    InputStream inputStr = null;
    try {
        inputStr = uploadedPicture.getInputstream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //create destination File
    String destPath = "C:\\"+uploadedPicture.getFileName();
    File destFile = new File(destPath);

    //use org.apache.commons.io.FileUtils to copy the File
    try {                    
        FileUtils.copyInputStreamToFile(inputStr, destFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
4

1 回答 1

2
public void handleFileUpload(FileUploadEvent event) {  

    //get uploaded file from the event
    UploadedFile uploadedFile = (UploadedFile)event.getFile();

    //create an InputStream from the uploaded file
    InputStream inputStr = null;
    try {
        inputStr = uploadedFile.getInputstream();
    } catch (IOException e) {
        //log error
    }

    //create destination File
    String destPath = "your path here";
    File destFile = new File(destPath);

    //use org.apache.commons.io.FileUtils to copy the File
    try {                    
        FileUtils.copyInputStreamToFile(inputStr, destFile);
    } catch (IOException e) {
        //log error
    }
}
于 2013-03-26T13:45:42.583 回答