我使用jQuery插件叫做wPaint允许用户绘制自己的形象。我发送所得到的图像作为一个字符串到服务器作为与我如何转换图像作为字符串PNG文件
数据开始的字符串:图像/ PNG; BASE64,
我试图以下,但与两个i”中的方法这两种方法m无法存储图像。
方法1
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
FileOutputStream fileOutputStream = new FileOutputStream("/home/arvind/Desktop/test.png");
fileOutputStream.write(imgByteArray);
fileOutputStream.close();
在这种情况下,文件写入,但不显示图像。但是,当我删除文件扩展名时,我得到了发送到服务器的字符串(即无论在imageData中)。
方法2
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
InputStream in = new ByteArrayInputStream(imgByteArray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "png", new File("/home/arvind/Desktop/test.png"));
的BufferedImage bImageFromConvert为空,所以我得到,当文件被创建的异常(抛出:IllegalArgumentException)。
的Base64编码类是从Apache公地编解码器库,并为1.2版本。
有什么我做错了吗?
图像如何被转换为:
现在,我使用以下代码
在服务器侧,这是最后的代码发送数据到服务器一个字符串? – 2013-03-18 15:27:37
在插件wPaint(实际上是一个jQuery插件)中,有一个将图像转换为字符串的函数。字符串被正确转换,如果你在客户端分配一个带有相同字符串的img src,就会显示图像。 – 2013-03-18 15:48:09
然后你需要用相同的插件将它转换回字节。 – 2013-03-18 16:33:11