我正在尝试转换Base64
编码的字符串(来自图片) [link to convertor]。我是这样做的:首先我转换字符串,即:
String str =
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQHBhMTEhQWFBUUGBUZGRYXFxgXGBQWFRQYGBQVFxgaISogGBomGxUXITEiJSkrLi4uFx8zODMsNygtLisBCgoKDgwMFw8PFywcFB4sLCwsNyssLDcrLCwsKzcsNzc3NzcsKywsKysrLCsrKywsNysrLCsrKyssKysrKysrK//AABEIALEBHQMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EADcQAAIBAwEFBQcEAgIDAQAAAAABAgMEERIFITFBURMiMnGhFBU0UmGRsXKBweHR8EJDM2KCBv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwD7iAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4ARb29jaR372+C/3gVU9rVJPdhft/ki3NV167k+b9OSLe22VGNPv736L6AQPelTqvsh70qdV9kWvuyn8vqx7sp/L6sDLZ1y7m3y+KePMlGNOmqUMRWEjIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHk/Az08ksoDlIf8AkXn/ACdYc5Cyn7So6Xx443ceOTowAAAAEDat27amlHxP0QE8HNe31PnY9vqfOwOlBza2hUT8T9DoaMtdJN7m0mBmARr68VpT6t8F/PkBJBQWt9Uldre3l8OW8vwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKC92jKtUai2o8sc/qyJ28vml92B1LeEcze1/aLhy5cvJcDB15NeJ/dkjZlv7Rcrot7/hAWVjs+MbZaopt79/LPIkew0/kX2JAA0RsqcXnQvsbwc9tCvNXck21h7lnG7kBb314rSn1b4L+X9CglKVzW6yZhKTk8t58xGbg8p4f0Av9n2KtY5e+T59PoiaVGyr9zq6JvOeD5+TLcAAAAAAAAAAAAAAAAAAAA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epNNYy/uTQBE2dZewU5RTzHU3FPLcU97WW9+/L/cxtdnRttoVaqe+pjdjw4Xex5vDfkTQBW3Wx4XtzOVRt6oqKSlKKit+c4fey3z6IlU7drZ6pylqejS5Y47samupIAEGrs/tNjdhq/wCtQ1Y6RxnGf5NtpCrBvtJwl00wccdc5k8kkAVFbYUaznJyl2kpalNOSUXHGjuasPGFxJm0bR3dvFKSjKMoSTaysweeGVu3dSWAIdjZyoVpznPXOelNqOmKUM6Ull/M+L5mm0sqtrWeKkNEpzm49m9Xfk5NatX144LIAQb2znVvIVKc4wcYyj3oOSak4vlJY8JKt4yjRSm1KW/LUdK47t2Xy+psAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH//2Q==
像这样:
byte[] imageString = str.getBytes("UTF-8");
然后我想将文件保存在这样的机器上:
public boolean uploadImage(byte[] imageString, String name, String extension)
{
BufferedImage image = null;
String path = EVENT_FOLDER + name + "." + extension;
try
{
image = ImageIO.read(new ByteArrayInputStream(imageString)); //null returned here
ImageIO.write(image, extension, new File(path));
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
但是我在这一行上得到了空:image = ImageIO.read(new ByteArrayInputStream(imageString));
我做错了什么?
答案 0 :(得分:4)
我做错了什么?
您忽略了数据是Base64编码的事实,不是 UTF-8。
要将字符串转换为字节以获取原始数据,您需要反转该Base64编码,例如使用this library。
byte[] imageData = Base64.decode(str);