Java Code Examples for org.apache.commons.vfs2.FileSystemManager#closeFileSystem()

The following examples show how to use org.apache.commons.vfs2.FileSystemManager#closeFileSystem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageObject.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/** 将内容流出到output */
public Long readContent(StorageMapping mapping, OutputStream output) throws Exception {
	long length = -1L;
	FileSystemManager manager = this.getFileSystemManager();
	String prefix = this.getPrefix(mapping);
	String path = this.path();
	FileSystemOptions options = this.getOptions(mapping);
	try (FileObject fo = manager.resolveFile(prefix + PATHSEPARATOR + path, options)) {
		if (fo.exists() && fo.isFile()) {
			try (InputStream input = fo.getContent().getInputStream()) {
				length = IOUtils.copyLarge(input, output);
			}
		} else {
			throw new Exception(fo.getPublicURIString() + " not existed, object:" + this.toString() + ".");
		}
		manager.closeFileSystem(fo.getFileSystem());
	}
	return length;
}
 
Example 2
Source File: StorageObject.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/** 删除内容,同时判断上一级目录(只判断一级)是否为空,为空则删除上一级目录 */
public void deleteContent(StorageMapping mapping) throws Exception {
	FileSystemManager manager = this.getFileSystemManager();
	String prefix = this.getPrefix(mapping);
	String path = this.path();
	FileSystemOptions options = this.getOptions(mapping);
	try (FileObject fo = manager.resolveFile(prefix + PATHSEPARATOR + path, options)) {
		if (fo.exists() && fo.isFile()) {
			fo.delete();
			if ((!StringUtils.startsWith(path, PATHSEPARATOR)) && (StringUtils.contains(path, PATHSEPARATOR))) {
				FileObject parent = fo.getParent();
				if ((null != parent) && parent.exists() && parent.isFolder()) {
					if (parent.getChildren().length == 0) {
						parent.delete();
					}
				}
			}
		}
		manager.closeFileSystem(fo.getFileSystem());
	}
}
 
Example 3
Source File: StorageObject.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/** 更新Content内容 */
public Long updateContent(StorageMapping mapping, InputStream input) throws Exception {
	long length = -1L;
	FileSystemManager manager = this.getFileSystemManager();
	String prefix = this.getPrefix(mapping);
	String path = this.path();
	if (StringUtils.isEmpty(path)) {
		throw new Exception("path can not be empty.");
	}
	FileSystemOptions options = this.getOptions(mapping);
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		/* 由于可以在传输过程中取消传输,先拷贝到内存 */
		IOUtils.copyLarge(input, baos);
		try (FileObject fo = manager.resolveFile(prefix + PATHSEPARATOR + path, options);
				OutputStream output = fo.getContent().getOutputStream()) {
			length = IOUtils.copyLarge(new ByteArrayInputStream(baos.toByteArray()), output);
			this.setLength(length);
			if (!Objects.equals(StorageProtocol.webdav, mapping.getProtocol())) {
				/* webdav关闭会试图去关闭commons.httpClient */
				manager.closeFileSystem(fo.getFileSystem());
			}
		}
	}
	this.setStorage(mapping.getName());
	this.setLastUpdateTime(new Date());
	return length;
}