将图像文件保存在特定的目录jsf中 [英] save image file in specific directory jsf primefaces project

查看:177
本文介绍了将图像文件保存在特定的目录jsf中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

,我访问的字节[]与:

  uploadedPicture.getContents()

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


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



谢谢



** * ** * *** / em> * ** * ** * ** *
这里是代码whic的作品,但有时我有错误:

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

//创建目标文件
String destPath =C:\\+ uploadedPicture.getFileName();
文件destFile = new File(destPath);

//使用org.apache.commons.io.FileUtils复制文件
try {
FileUtils.copyInputStreamToFile(inputStr,destFile);
} catch(IOException e){
e.printStackTrace();
}

}


解决方案

  public void handleFileUpload(FileUploadEvent event){

//从事件获取上传的文件
UploadFile uploadedFile =(UploadedFile)event.getFile );

//从上传的文件创建一个InputStream
InputStream inputStr = null;
try {
inputStr = uploadedFile.getInputstream();
} catch(IOException e){
// log error
}

//创建目标文件
String destPath =你的路径在这里;
文件destFile = new File(destPath);

//使用org.apache.commons.io.FileUtils复制文件
try {
FileUtils.copyInputStreamToFile(inputStr,destFile);
} catch(IOException e){
//日志错误
}
}


I want to save byte[] file into a specific directory : I get it from this method :

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

and I access the byte[] with :

uploadedPicture.getContents()

I tested this link but no result

how to save it into a specific directory either inside my project or outside

thank you

*********EDIT********** here is the code whic works but sometimes I have the error :

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

}

解决方案

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

这篇关于将图像文件保存在特定的目录jsf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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