以前一直使用FCK编辑器,没有发现什么问题,最近在做FCKEditor编辑器上传图片的时候自动生成图片的缩略图功能的时候发现上传后图片不能删除,系统提示图片正在使用中不能删除的提示框。
因为我是在struts2中使用的FCKEditor,感觉是struts2的问题,但是在查看了FCKEditor编辑器源码后发现时FCKEditor编辑器上传图片后文件流没有关闭产生的。虽然不是什么大的问题,但是如果释放到程序就做的完美了哈。
1. FCKEditor源码
net\fckeditor\connector\impl\AbstractLocalFileSystemConnector.java
public String fileUpload(final ResourceType type,
final String currentFolder, final String fileName,
final InputStream inputStream)
throws InvalidCurrentFolderException, WriteException {
String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler
.getUserFilesAbsolutePath(ThreadLocalData.getRequest()));
File typeDir = getOrCreateResourceTypeDir(absolutePath, type);
File currentDir = new File(typeDir, currentFolder);
if (!currentDir.exists() || !currentDir.isDirectory())
throw new InvalidCurrentFolderException();
File newFile = new File(currentDir, fileName);
File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile());
try {
IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave));
} catch (IOException e) {
throw new WriteException();
}
return fileToSave.getName();
}
注意在82行的这行代码
IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave));
inputStream是通过形参传递过来的,第二个参数直接生成了一个输出流FileOutputStream,程序结束。
这么一来就有问题,当函数返回的时候inputStream输入流并没有显示进行关闭,那么此时如果在服务器上面进行删除刚刚上传的文件的时候是不允许的。
解决方式如下:
IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave));
inputStream.close();
即可。