apache VSF 操作类

package com.biya.dao.jdbc;


public class VirtualFileSystemDAOImpl {
private String uri;
private String userName;
private String password;
private boolean needAuth;// 是否需要用户认证
private DefaultFileSystemManager mgr;
private FileObject fo;

private DefaultFileSystemManager getFileSystemManager() throws FileSystemException {
if (mgr == null) {
mgr = (DefaultFileSystemManager) VFS.getManager();
//mgr.setCacheStrategy(CacheStrategy.ON_CALL);
}
return mgr;
}

public FileObject getFileObject(String path) throws FileSystemException {
return getFileObject(path,true);
}

public FileObject getFileObject(String path,boolean includeUri) throws FileSystemException {
if (this.needAuth) {
StaticUserAuthenticator auth = new StaticUserAuthenticator(null, userName, password);
FileSystemOptions opts = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
if (includeUri){
fo = VFS.getManager().resolveFile(uri + path, opts);
}else{
fo = VFS.getManager().resolveFile("file://" + path, opts);
}
} else {
if (includeUri){
fo = this.getFileSystemManager().resolveFile(uri + path);
}else{
fo = VFS.getManager().resolveFile("file://" + path);
}
}
return fo;
}

public void setNeedAuth(boolean needAuth) {
this.needAuth = needAuth;
}

public void setUri(String uri) {
this.uri = uri;
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setPassword(String password) {
this.password = password;
}

public void copyFile(String sourcePath, String targetPath) throws FileSystemException {
FileObject src = getFileObject(sourcePath);
FileObject dest = getFileObject(targetPath);
if (dest.exists() && dest.getType() == FileType.FOLDER) {
dest = dest.resolveFile(src.getName().getBaseName());
}
dest.copyFrom(src, Selectors.SELECT_ALL);
}

public void createFile(FileItem fileItem, String targetPath) throws IOException {
String sourcePath=System.getProperty("user.home")+ File.separator + "objecttype.gif";
File file = new File( sourcePath);
try {
fileItem.write(file);
} catch (Exception ex) {
ex.printStackTrace();
}
FileObject src = getFileObject(sourcePath,false);
FileObject dest = getFileObject(targetPath);
dest.createFile();
if (dest.isWriteable()){
dest.copyFrom(src, Selectors.SELECT_ALL);
}
}

public void moveFile(String sourcePath, String targetPath) throws FileSystemException {
copyFile(sourcePath, targetPath);
removeFile(sourcePath);
}

public void removeFile(String sourcePath) throws FileSystemException {
FileObject file = getFileObject(sourcePath);
file.delete(Selectors.SELECT_SELF);

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值