jeudi 25 juin 2015

Not able to write image from local folder to folder in tomcat servrer

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

Aucun commentaire:

Enregistrer un commentaire