0

enter link description hereHi i am using the below code to store image from local folder to folder in tomcat. some image is created in tht folder but size is 0.i m unable to find the solution.please help same is working when when runni same code from eclipse luna in tomcat server. running from outside,image size is 0.

File file = new File(filepath);
        BufferedImage image=null; 
        OutputStream stream=null;

        try
        {
            stream = new FileOutputStream(new File(
                    context.getRealPath("/TempJobDescriptionImages/"+file.getName())));
        /*          "http://localhost:8080/Profiles/WebContent/TempJobDescriptionImages/"+file.getName())); // Your output
*/      

            System.out.println(context.getRealPath("/TempJobDescriptionImages/"+file.getName()));
        BufferedImage bimg = ImageIO.read(file);
        int width          = bimg.getWidth();
        int height         = bimg.getHeight();
        byte[] bytes = extractBytes(filepath); // Your image bytes
        image= createRGBImage(bytes, width, height);

        ImageIO.write(image, "jpg", stream);
        stream.close();
        }
        catch(Exception e)
        {
            System.out.println(""+e);
        }

    private static BufferedImage createRGBImage(byte[] bytes, int width, int height) {
        DataBufferByte buffer = new DataBufferByte(bytes, bytes.length);
        ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        return new BufferedImage(cm, Raster.createInterleavedRaster(buffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null), false, null);
    }

    public byte[] extractBytes (String ImageName) throws IOException {
         // open image
         File imgPath = new File(ImageName);
         BufferedImage bufferedImage = ImageIO.read(imgPath);

         // get DataBufferBytes from Raster
         WritableRaster raster = bufferedImage .getRaster();
         DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

         return ( data.getData() );
        }
  • If this is just a file copy why not just call [java.nio.file.Files.copy()](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...))? – Steve C Jun 25 '15 at 13:17
  • no this is not jus file copy...this is image file. – Raju Patil Jun 25 '15 at 13:33
  • also in tht destination folder some default image is created with size 0....so destination path given is correct but writing the image from local folder to folder in tomcat is problem..please help anybody.thanks – Raju Patil Jun 25 '15 at 13:35
  • If you are deliberately doing some image processing then write a unit test and debug it. But you do not need to go to all that trouble if you're just copying it, image file or not. – Steve C Jun 25 '15 at 13:46
  • ok as u suggested I am doin simple...removed processing n just writing...but still same problem – Raju Patil Jun 25 '15 at 14:05

0 Answers0