[英]Unable to create temporary file in Java
I have a Java method that takes byte array and String value as arguments and returns a File object. 我有一个Java方法,该方法将字节数组和String值作为参数并返回File对象。 This is the code 这是代码
public File createTempFile(byte[] byteArray, String fileName) throws IOException {
String prefix = FilenameUtils.getBaseName(fileName);
String suffix = getMimeType(byteArray);
File tempFile = File.createTempFile(prefix, suffix, null);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(tempFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fos.write(byteArray);
fos.close();
return tempFile;
}
When I try to run it like this 当我尝试这样运行时
File myFile = tiedostoService.createTempFile(tiedosto.getContent(), attachment.getFileName());
I get an IOException like this 我得到这样的IOException
java.io.IOException: Unable to create temporary file, C:\Users\ROSHAN~1\AppData\Local\Temp\kuva1068864619970584773image\png
at java.io.File$TempDirectory.generateFile(File.java:1921)
at java.io.File.createTempFile(File.java:2010)
From the stacktrace. 从堆栈跟踪。 it can be seen that it's trying to create a file like C:\\Users\\ROSHAN~1\\AppData\\Local\\Temp\\kuva1068864619970584773image\\png
and not C:\\Users\\ROSHAN~1\\AppData\\Local\\Temp\\kuva1068864619970584773image.png
可以看出,它正在尝试创建一个文件,例如C:\\Users\\ROSHAN~1\\AppData\\Local\\Temp\\kuva1068864619970584773image\\png
而不是C:\\Users\\ROSHAN~1\\AppData\\Local\\Temp\\kuva1068864619970584773image.png
How can I fix this? 我怎样才能解决这个问题? I'd really appreciate any sort of help. 我真的很感谢任何帮助。
I think there is an extra \\ in your suffix string, could you try debug and see the actual value of the suffix? 我认为后缀字符串中有一个额外的\\,可以尝试调试并查看后缀的实际值吗?
I tried to run: 我试着跑:
String suffix = "\\png";
and got the same error, but if I do 并得到相同的错误,但如果我这样做
String suffix = ".png";
no error creating the temp file, notice that you need to add a dot in the suffix... 创建临时文件没有错误,请注意,您需要在后缀中添加一个点...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.