无法将图像从本地文件夹写入tomcat servrer中的文件夹

时间:2015-06-25 12:39:34

标签: java-ee

在这里输入链接描述我正在使用以下代码将图像从本地文件夹存储到tomcat中的文件夹。 一些图像是在该文件夹中创建的,但大小为0.i m无法找到解决方案。请帮助 当在tomcat服务器上运行来自eclipse luna的相同代码时,同样工作。 从外面跑,图像大小为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() );
        }

0 个答案:

没有答案