IllegalArgumentException:非法参数异常,
试下这个,应该可以。
给你讲述下过程:
去了stackoverflow,debug。最后发现data为null,,加油吧,我们需要学的还很多
下次遇到问题debug下,看是哪条代码出现问题了,通过回答你,我也学到了很多
关键点在这里: throw new IOException();
try (OutputStream out = new FileOutputStream(newPath.toString())) {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("异常是这么抛出的");
//throw new RuntimeException(e);
}
public static String base64ToImg(String src) throws IOException {
String uuid = UUID.randomUUID().toString();
StringBuilder newPath = new StringBuilder("xx");
newPath.append("xx").
append(uuid).
append("xx");
if (src == null) {
return null;
}
byte[] data = Base64.getDecoder().decode(src);
try (OutputStream out = new FileOutputStream(newPath.toString())) {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
return newPath.toString();
}
补充另外一种常用关闭资源:
public static String base64ToImg(String src) throws IOException {
String uuid = UUID.randomUUID().toString();
StringBuilder newPath = new StringBuilder("xx");
newPath.append("xx").
append(uuid).
append("xx");
if (src == null) {
return null;
}
byte[] data = null;
OutputStream out = null;
Base64.Decoder decoder = Base64.getDecoder();
try {
out = new FileOutputStream(newPath.toString());
data = decoder.decode(src);
out.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
return newPath.toString();
}