[英]base64 image back to png using java too long constant
I am trying to learn java base64 conversion to image 我正在尝试学习java base64转换为图像
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import redis.clients.jedis.Jedis;
public class HelloWorld {
public static void main(String[] args) throws IOException {
byte[] btDataFile = new sun.misc.BASE64Decoder().decodeBuffer("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAn4AAAMUCAYAAAAiyCroAAAMGWlDQ1BJQ0MgUHJvZmlsZQAASImVVwdUU0kXnldSCEkogQhICb0J0qv0XgSkg42QBAglhkBQsSOLCq4FFVEUFV0Rsa0FkLUgo...AAABJRU5ErkJggg==");
File of = new File("/Users/myfolder/Desktop/yourFile.png");
FileOutputStream osf = new FileOutputStream(of);
osf.write(btDataFile);
osf.flush();
}
}
Problem is that it too long (which it is :) ) of a constant. 问题是它的长度太长(它是:))。 Now what can I do to achieve this goal? 现在我该怎么做才能实现这个目标? Thanks 谢谢
The short answer is that you shouldn't have such a long String
as a constant in your source code. 简短的回答是,您的源代码中不应该有这么长的String
作为常量。 How to change it depends on your actual scenario: 如何更改它取决于您的实际情况:
String
constant limit, and not a RAM memory limit), put the text into a file and load it at run time. 如果这只是编译器实现限制问题(即问题是String
常量限制,而不是RAM内存限制),请将文本放入文件并在运行时加载它。 Alternatively, if you really insist on the text being embedded in the source code, split the String
into several constant-sized chunks and join them at run time. 或者,如果您确实坚持将源代码嵌入到源代码中,请将String
拆分为几个常量大小的块,并在运行时将它们连接起来。 sun.misc
package, but a quick glance at http://www.docjar.com/docs/api/sun/misc/BASE64Decoder.html shows decodeAtom
and decodeBufferToByteBuffer
methods which seem quite promising. 如果图像的base64编码对于你的进程可用的RAM内存量实在太大,请使用流式方法(我没有使用sun.misc
包,但快速浏览一下http:// www .docjar.com / docs / api / sun / misc / BASE64Decoder.html显示了decodeAtom
和decodeBufferToByteBuffer
方法,看起来非常有前景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.