|
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);
}
} |
|
|