Java 运行解码程序时出现异常
我有一个解码字符串的代码,如下所示:Java 运行解码程序时出现异常,java,Java,我有一个解码字符串的代码,如下所示: public static void main(String[] args) throws IOException { String base64="anybase64value" byte[] bytes = Base64.decodeBase64(base64); String tempDir = System.getProperty("java.io.tmpdir"); System.out.print
public static void main(String[] args) throws IOException {
String base64="anybase64value"
byte[] bytes = Base64.decodeBase64(base64);
String tempDir = System.getProperty("java.io.tmpdir");
System.out.println(System.getProperty("java.io.tmpdir"));
String testFileName = "/tmp/" + "base64.xlsx";
FileOutputStream fos = new FileOutputStream(new File(testFileName));
IOUtils.write(bytes, fos);
System.out.println("Wrote " + bytes.length + " bytes to: " + testFileName);
}
但当我运行时,它会给我一个错误:
The method write(byte[], OutputStream) in the type IOUtils is not applicable for the arguments (byte[], File)
The method close() is undefined for the type File
发生这种情况是因为您试图在rt.jar中的
sun.misc.IOUtils
中调用方法write
。您需要使用importorg.apache.commons.io.IOUtils
。可能导入错误。什么是IOUtils
类?它来自Apache Commons吗?您是否遇到异常或编译错误?发生在哪一行?您的程序似乎与您提供的错误消息不匹配。你甚至没有在任何地方呼叫close
。奇怪。看看这段代码,您得到的错误似乎不是由以下几行引起的:我确认您的fos不是一个文件;)