2 Replies Latest reply: Jan 8, 2010 11:04 AM by 807580 RSS

    why do my file is stored and has the correct dimension but I cannot open it

    807580
      hi,

      I want to store an image from an inputStream to my harddisk. The created file (image) has the same dimensions of the original image (8kb), but I cannot open it with photoshop or other image editors. Something is wrong in the data... can you give me some tips ?

      thanks

      File outputfile = new File("/Library/Tomcat/webapps/ROOT/WEB-INF/classes/saved2.png");


                try {
                     //File f=new File("outFile.java");
                     //InputStream inputStream= new FileInputStream("InputStreamToFile.java");
                     OutputStream out=new FileOutputStream(outputfile);
                     byte buf[]=new byte[1024];
                     int len;
                     while((len=inputStream.read(buf))>0)
                          out.write(buf,0,len);
                     out.close();
                     inputStream.close();
                     System.out.println("\nFile is created");
                }
                catch (IOException e){
                     
                }
        • 1. Re: why do my file is stored and has the correct dimension but I cannot open it
          dcminter
          What makes you think the contents of the input stream are correct?
          • 2. Re: why do my file is stored and has the correct dimension but I cannot open it
            807580
            I've solved it with this code:

            InputStream inputStream = req.getInputStream();
                           BufferedImage img = ImageIO.read(inputStream);
                           
                           File outputfile = new File("/Library/Tomcat/webapps/ROOT/WEB-INF/classes/saved2.png");

                           //storing the image to file system.
                           if (outputfile.isDirectory()) {
                                ImageIO.write(img, "png", outputfile);
                           } else {
                                outputfile.mkdirs();
                                ImageIO.write(img, "png", outputfile);
                           }

                      } catch (Exception e) {
                           System.out.println(e.getMessage());
                      }