我刚试过一个示例代码表格网,它显示如下警告
SimpleConvertImage.java:7:警告:com.sun.org.apache.xerces.internal.impl.dv.util.Base64是内部专有API,可能会在将来的版本中删除 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; ^
SimpleConvertImage.java:16:警告:com.sun.org.apache.xerces.internal.impl.dv.util。 Base64是内部专有API,可能会在将来的版本中删除 String base64String = Base64.encode(baos.toByteArray()); ^
SimpleConvertImage.java:19:警告:com.sun.org.apache.xerces.internal.impl.dv.util .Base64是内部专有API,可能会在将来的版本中删除 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, "png", new File(dirName,"snap3.png"));
}
}
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, "png", new File(dirName,"snap3.png"));
}
}
答案 0 :(得分:24)
不要使用内部com.sun。*包。如果您使用的是v6或更高版本,则可以使用DatatypeConverter。您的代码如下:
String base64String = DatatypeConverter.printBase64Binary(baos.toByteArray());
byte[] bytearray = DatatypeConverter.parseBase64Binary(base64String);
答案 1 :(得分:15)
您还可以使用:java.util.Base64 添加到Java 1.8
String encryptedValue = new String(Base64.getEncoder().encode(bytesToEncode));
byte[] decodedValue = Base64.getDecoder().decode(encryptedDataString);