系统找不到jsf2.0中指定的文件

发布于 2024-12-14 13:15:41 字数 3964 浏览 3 评论 0原文

我在 eclipse ide 中使用 jsf2.0。我在本地文件夹中存储图像时遇到问题。图像存储在本地文件夹中,大小为 0 字节。图像被读取和写入 0 字节。

Backing Bean:

public class uploadImageAction {

public void handleFileUpload(FileUploadEvent event) throws IOException {


    FacesContext context = FacesContext.getCurrentInstance();
    PhotoBean photoBean = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean}", PhotoBean.class);
    PhotoBean photoBean2 = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean2}", PhotoBean.class);

    ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();

    File file = new  File(extContext.getRealPath(event.getFile().getFileName()));


      System.out.println("Hibernate save image into database");
        Session session = HibernateUtil1.getSessionFactory().openSession();

        session.beginTransaction();

        //save image into database
        //File file = new File("D:\\1.gif");
        //byte[] bFile = new byte[(int) file.length()];
        //System.out.println("DDDDDDDDDDDD"+bFile);

        byte[] bfile = new byte[(int)file.length()];
        System.out.println("DDDDDDDDDDDD"+bfile);
        try {
             FileInputStream fileInputStream = new FileInputStream(file);
             //convert file into array of bytes
             fileInputStream.read(bfile);
             fileInputStream.close();
            } catch (Exception e) {
                System.out.println("Cannnoy");
             e.printStackTrace();
            }

        String urlImage = file.toString();

        String imageName = event.getFile().getFileName();


        photoBean.setPathName(file);

        //file = photoBean.getPathName();

        photoBean.setImageName(imageName);

        imageName = photoBean.getImageName();

        photoBean.setUrl(urlImage);

        urlImage = photoBean.getUrl();


    //  PhotoDaoService photoDaoService = new PhotoDaoService();

        System.out.println("+++++++++++++++++++++++++"+file);


 System.out.println("CHECK BYTES"+bfile);
     //   uploadImageBean upldImageBean = new uploadImageBean();
        //upldImageBean.setImageByte(bfile);
        photoBean.setByteImage(bfile);
        //avatar.setImage(bFile);
       // System.out.println("dfdffdf"+upldImageBean.setImageByte(bFile));


        session.save(photoBean);


        //Get image from database
         photoBean2 = (PhotoBean)session.get(PhotoBean.class, photoBean.getId());
        byte[] bImage = photoBean2.getByteImage();
        System.out.println("QQQQQQQQQQQQQQQ"+bImage);
        int i=0;
        try{
            FileOutputStream fos = new FileOutputStream("e:/out/out"+0+".jpg"); 
            fos.write(bImage);
            fos.close();
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()
                    + " is uploaded.");

                    FacesContext.getCurrentInstance().addMessage(null, msg);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("UPLOAD DISPLAY EXCEPTION");
        }

        session.getTransaction().commit();
    }

}

发生错误:

Hibernate save image into database
bytes[B@12fb7ef
java.io.FileNotFoundException: E:\Mecherie_project\.metadata\.plugins \org.eclipse.wst.server.core\tmp0\wtpwebapps\image_web\baby.jpg (The system cannot find  the file specified)

at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at image.uploadImageAction.handleFileUpload(uploadImageAction.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)

I have using jsf2.0 in eclipse ide.I have a problem to store a image in local folder.The images are stored in local folder with 0 bytes.The images are read and write into 0 bytes.

Backing Bean:

public class uploadImageAction {

public void handleFileUpload(FileUploadEvent event) throws IOException {


    FacesContext context = FacesContext.getCurrentInstance();
    PhotoBean photoBean = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean}", PhotoBean.class);
    PhotoBean photoBean2 = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean2}", PhotoBean.class);

    ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();

    File file = new  File(extContext.getRealPath(event.getFile().getFileName()));


      System.out.println("Hibernate save image into database");
        Session session = HibernateUtil1.getSessionFactory().openSession();

        session.beginTransaction();

        //save image into database
        //File file = new File("D:\\1.gif");
        //byte[] bFile = new byte[(int) file.length()];
        //System.out.println("DDDDDDDDDDDD"+bFile);

        byte[] bfile = new byte[(int)file.length()];
        System.out.println("DDDDDDDDDDDD"+bfile);
        try {
             FileInputStream fileInputStream = new FileInputStream(file);
             //convert file into array of bytes
             fileInputStream.read(bfile);
             fileInputStream.close();
            } catch (Exception e) {
                System.out.println("Cannnoy");
             e.printStackTrace();
            }

        String urlImage = file.toString();

        String imageName = event.getFile().getFileName();


        photoBean.setPathName(file);

        //file = photoBean.getPathName();

        photoBean.setImageName(imageName);

        imageName = photoBean.getImageName();

        photoBean.setUrl(urlImage);

        urlImage = photoBean.getUrl();


    //  PhotoDaoService photoDaoService = new PhotoDaoService();

        System.out.println("+++++++++++++++++++++++++"+file);


 System.out.println("CHECK BYTES"+bfile);
     //   uploadImageBean upldImageBean = new uploadImageBean();
        //upldImageBean.setImageByte(bfile);
        photoBean.setByteImage(bfile);
        //avatar.setImage(bFile);
       // System.out.println("dfdffdf"+upldImageBean.setImageByte(bFile));


        session.save(photoBean);


        //Get image from database
         photoBean2 = (PhotoBean)session.get(PhotoBean.class, photoBean.getId());
        byte[] bImage = photoBean2.getByteImage();
        System.out.println("QQQQQQQQQQQQQQQ"+bImage);
        int i=0;
        try{
            FileOutputStream fos = new FileOutputStream("e:/out/out"+0+".jpg"); 
            fos.write(bImage);
            fos.close();
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()
                    + " is uploaded.");

                    FacesContext.getCurrentInstance().addMessage(null, msg);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("UPLOAD DISPLAY EXCEPTION");
        }

        session.getTransaction().commit();
    }

}

The Error Occurs:

Hibernate save image into database
bytes[B@12fb7ef
java.io.FileNotFoundException: E:\Mecherie_project\.metadata\.plugins \org.eclipse.wst.server.core\tmp0\wtpwebapps\image_web\baby.jpg (The system cannot find  the file specified)

at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at image.uploadImageAction.handleFileUpload(uploadImageAction.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2024-12-21 13:15:42

正如回答的那样很多 之前,您不应该存储/定位上传的内容公共网络内容中的文件。您应该改用绝对路径。例如,

File file = new File("/path/to/images", filename);
// ...

上传的文件不会神奇地出现在扩展的 WAR 中,因此使用 FileInputStream 读取它根本不起作用。您必须从 event.getFile().getInputStream() 读取它,然后将其写入任意 OutputStream,例如 FileOutputStream

但现在您的真正问题比之前的问题更加清晰。您似乎想将上传的文件保存在数据库中。那么不需要首先将文件存储在本地磁盘文件系统上。只需将接收到的字节直接流式传输到 byte[] 并将其保存在数据库中。

InputStream input = event.getFile().getInputstream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];

for (int length; (length = input.read(buffer)) > -1;) {
    output.write(buffer, 0, length);
}

photoBean.setByteImage(output.toByteArray());

As answered so many times before, you should not store/locate uploaded files in public web content. You should be using an absolute path instead. E.g.

File file = new File("/path/to/images", filename);
// ...

The uploaded file is not magically present in the expanded WAR, so reading it with FileInputStream won't work at all. You'd have to read it from event.getFile().getInputStream() and then write it to an arbitrary OutputStream such as FileOutputStream.

But now your real problem is more clear than in your previous questions. You seem to want to save the uploaded file in the database. There's then no need to store the file on local disk file system first. Just stream the received bytes to a byte[] directly and the save it in the DB.

InputStream input = event.getFile().getInputstream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];

for (int length; (length = input.read(buffer)) > -1;) {
    output.write(buffer, 0, length);
}

photoBean.setByteImage(output.toByteArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文