我刚刚尝试了一个示例代码表,它显示如下警告
SimpleConvertImage.java:7: warning:com.sun.org.apache.xerces.internal.impl.dv.util.Base64 is internal proprietary API and may be removed in a future release
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
^SimpleConvertImage.java:16: warning: com.sun.org.apache.xerces.internal.impl.dv.util. Base64 is internal proprietary API and may be removed in a future release
String base64String=Base64.encode(baos.toByteArray());
^SimpleConvertImage.java:19: warning: com.sun.org.apache.xerces.internal.impl.dv.util .Base64 is internal proprietary API and may be removed in a future release
byte[] bytearray =Base64.decode(base64String);
^代码如下
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class SimpleConvertImage { public static void main(String[] args) throws IOException{ String dirName="/root/Desktop"; ByteArrayOutputStream baos=new ByteArrayOutputStream(1000); BufferedImage img=ImageIO.read(new File(dirName,"Screenshot.png")); ImageIO.write(img,"png",baos); baos.flush(); String base64String=Base64.encode(baos.toByteArray()); baos.close(); byte[] bytearray =Base64.decode(base64String); BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray)); ImageIO.write(imag,new File(dirName,"snap3.png")); } }
解决方法
不要使用内部com.sun *包.如果您使用v6或更高版本,则可以使用
DatatypeConverter.您的代码将如下所示:
String base64String = DatatypeConverter.printBase64Binary(baos.toByteArray()); byte[] bytearray = DatatypeConverter.parseBase64Binary(base64String);