package com.vci.client.uif.actions.client;
|
|
import java.awt.Component;
|
import java.io.File;
|
import javax.swing.JFileChooser;
|
import javax.swing.filechooser.FileFilter;
|
|
import com.vci.client.fm.ClientFileObjectOperation;
|
import com.vci.client.fm.FileObject;
|
|
/**
|
* 文件上传下载工具
|
* @author VCI-STGK006
|
*
|
*/
|
public class FileOperation {
|
|
/**
|
* 文件对象操作方法
|
*/
|
private ClientFileObjectOperation operation = new ClientFileObjectOperation();
|
|
/**
|
* 构造器
|
*/
|
public FileOperation(){}
|
|
/**
|
* 选择并上传文件
|
* @param parent 父组件
|
* @param currentDirectory 默认目录
|
* @param filters 文件过滤器
|
* @param multiSelection 是否可以多选 true允许
|
* @param revisionLimit 版本上限?
|
* @param boOid 业务对象ID
|
* @return FileObject[] 业务对象集合 如果没有上传任何文件时为null
|
* @throws Exception
|
*/
|
public FileObject[] selectUploadFile(Component parent, String currentDirectory,FileFilter[] filters, boolean multiSelection, int revisionLimit, String boOid) throws Exception {
|
//创建文件选择窗口
|
JFileChooser jfc = null;
|
if(currentDirectory == null || currentDirectory.equals("")){
|
jfc = new JFileChooser();
|
} else {
|
jfc = new JFileChooser(currentDirectory);
|
}
|
jfc.setDialogTitle("请选择文件");
|
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
jfc.setMultiSelectionEnabled(multiSelection);
|
//设置文件过滤器
|
if(filters != null){
|
for(FileFilter filter : filters){
|
jfc.addChoosableFileFilter(filter);
|
}
|
}
|
//FileFilter DefaultFilter = new FileNameExtensionFilter("所有文件", "*");
|
//jfc.addChoosableFileFilter(DefaultFilter);
|
int result = jfc.showOpenDialog(parent);
|
if(result == JFileChooser.APPROVE_OPTION){
|
//获得选择文件
|
File[] files = null;
|
if(multiSelection){
|
files = jfc.getSelectedFiles();
|
} else {
|
files = new File[1];
|
files[0] = jfc.getSelectedFile();;
|
}
|
return uploadFile(files, revisionLimit, boOid);
|
}
|
return null;
|
}
|
|
/**
|
* 上传文件
|
* @param filePath 文件路径
|
* @param revisionLimit 版本上限?
|
* @param boOid 业务对象ID
|
* @return 文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject uploadFile(String filePath, int revisionLimit, String boOid) throws Exception{
|
if(filePath == null || filePath.trim().equals("")){
|
throw new Exception("参数错误:文件路径为空!");
|
}
|
String[] filePaths = {filePath};
|
FileObject[] fileObjects = uploadFile(filePaths, revisionLimit, boOid);
|
if(fileObjects != null && fileObjects.length == 1){
|
return fileObjects[0];
|
}
|
return null;
|
}
|
|
/**
|
* 上传文件
|
* @param file 文件
|
* @param revisionLimit 版本上限?
|
* @param boOid 业务对象ID
|
* @return 文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject uploadFile(File file, int revisionLimit, String boOid) throws Exception{
|
if(file == null){
|
throw new Exception("参数错误:文件对象为空!");
|
}
|
File[] files = new File[1];
|
files[0] = file;
|
FileObject[] fileObjects = uploadFile(files, revisionLimit, boOid);
|
if(fileObjects != null && fileObjects.length == 1){
|
return fileObjects[0];
|
}
|
return null;
|
}
|
|
/**
|
* 上传文件
|
* @param filePaths 文件路径集合
|
* @param revisionLimit 版本上限?
|
* @param boOid 业务对象ID
|
* @return 文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject[] uploadFile(String[] filePaths, int revisionLimit, String boOid) throws Exception{
|
FileObject[] fileObjects = null;
|
if(filePaths == null || filePaths.length<1){
|
throw new Exception("参数错误:文件路径为空!");
|
}
|
fileObjects = new FileObject[filePaths.length];
|
for(int i = 0; i<filePaths.length; i++){
|
//验证文件是否存在或是否是标准文件
|
String filePath = filePaths[i];
|
File newFile = new File(filePath);
|
if(!newFile.exists()){
|
throw new Exception("文件不存在!");
|
}
|
if(!newFile.isFile()){
|
throw new Exception("不是标准文件!");
|
}
|
FileObject fileObject = operation.createNewFile();
|
//得到文件名称和文件类型
|
String fileName = newFile.getName();
|
String fileRealName = "";
|
String fileType = "";
|
int index = fileName.lastIndexOf(".");
|
if(index<1){
|
fileRealName = fileName;
|
fileType = "";
|
} else {
|
fileRealName = fileName.substring(0, index - 1);
|
fileType = fileName.substring(index);
|
}
|
//设置文件对象基本信息
|
fileObject.setName(fileName);
|
fileObject.setFileType(fileType);
|
fileObject.setRevisionLimit(revisionLimit);
|
fileObject.setDocumentoid(boOid);
|
String localfilePath = filePath;
|
//保存文件及文件对象
|
fileObjects[i] = operation.createNewFile(localfilePath, fileObject);
|
}
|
return fileObjects;
|
}
|
|
/**
|
* 上传文件
|
* @param files 文件集合
|
* @param revisionLimit 版本上限?
|
* @param boOid 业务对象ID
|
* @return 文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject[] uploadFile(File[] files, int revisionLimit, String boOid) throws Exception{
|
if(files == null || files.length < 1){
|
throw new Exception("参数错误:文件对象为空!");
|
}
|
FileObject[] fileObjects = new FileObject[files.length];
|
for(int i = 0; i<files.length; i++){
|
//验证文件是否存在或是否是标准文件
|
File newFile = files[i];
|
if(!newFile.exists()){
|
throw new Exception("文件不存在!");
|
}
|
if(!newFile.isFile()){
|
throw new Exception("不是标准文件!");
|
}
|
FileObject fileObject = createFileObject(newFile, revisionLimit, boOid);
|
String localfilePath = newFile.getPath();
|
//保存文件及文件对象
|
fileObjects[i] = operation.createNewFile(localfilePath, fileObject);
|
}
|
return fileObjects;
|
}
|
|
/**
|
* 创建FileObject对象
|
* @param newFile 文件对象
|
* @param revisionLimit 版本上线
|
* @param boOid fromBO对象
|
* @return
|
*/
|
public FileObject createFileObject(File newFile, int revisionLimit, String boOid) throws Exception{
|
FileObject fileObject = operation.createNewFile();;
|
//得到文件名称和文件类型
|
String fileName = newFile.getName();
|
String fileRealName = "";
|
String fileType = "";
|
int index = fileName.lastIndexOf(".");
|
if(index<1){
|
fileRealName = fileName;
|
fileType = "";
|
} else {
|
fileRealName = fileName.substring(0, index - 1);
|
fileType = fileName.substring(index);
|
}
|
//设置文件对象基本信息
|
fileObject.setName(fileName);
|
fileObject.setFileType(fileType);
|
fileObject.setRevisionLimit(revisionLimit);
|
fileObject.setDocumentoid(boOid);
|
return fileObject;
|
}
|
|
/**
|
* 选择保存路径并下载文件到本地
|
* @param parent 父组件
|
* @param foOids 文件对象OID
|
* @param currentDirectory 默认打开路径
|
* @return FileObject[]下载的文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject[] downloadFile(Component parent, String[] foOids, String currentDirectory) throws Exception{
|
//创建文件选择窗口
|
JFileChooser jfc = null;
|
if(currentDirectory == null || currentDirectory.equals("")){
|
jfc = new JFileChooser();
|
} else {
|
jfc = new JFileChooser(currentDirectory);
|
}
|
jfc.setDialogTitle("请选择保存路径");
|
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
jfc.setDialogType(JFileChooser.SAVE_DIALOG);
|
jfc.setMultiSelectionEnabled(false);
|
int result = jfc.showSaveDialog(parent);
|
if(result == JFileChooser.APPROVE_OPTION){
|
File file = jfc.getSelectedFile();
|
//下载保存文件
|
return downloadFile(foOids, file.getPath());
|
}
|
return null;
|
}
|
|
/**
|
* 打开文件
|
* 文件加载到系统临时目录下
|
* @param foOids 文件对象OID
|
* @throws Exception
|
*/
|
public boolean openFile(String[] foOids)throws Exception{
|
String tempFile = System.getProperty("java.io.tmpdir")
|
+ File.separator + "innovation";
|
//每次下载之前都清空之前的文件,无论是否删除成功都执行下一步操作
|
File newFile = new File(tempFile);
|
try{
|
if(newFile.exists()){
|
newFile.delete();
|
}
|
} catch(Exception e){
|
}
|
return openFile(foOids, tempFile);
|
}
|
|
/**
|
* 打开文件
|
* @param foOids 文件对象OID
|
* @param tempPath 文件保存路径
|
* @throws Exception
|
*/
|
public boolean openFile(String[] foOids, String tempPath) throws Exception{
|
//校验路径是否存在
|
File tempFile = new File(tempPath);
|
if(!tempFile.exists()){
|
tempFile.mkdirs();
|
}
|
//下载文件
|
FileObject[] fos = downloadFile(foOids, tempPath);
|
//打开文件
|
if(fos != null && fos.length > 0){
|
for(int i = 0; i < fos.length; i++){
|
String[] cmdArray = {"cmd.exe","/c",tempPath
|
+ File.separator + fos[i].getBusinessObject().name};
|
Runtime.getRuntime().exec(cmdArray);
|
}
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 下载文件到本地
|
* @param foOids 文件对象OID
|
* @param savePath 文件保存路径
|
* @return FileObject[]下载的文件对象
|
* @throws Exception 异常类型 Exception VCIError
|
*/
|
public FileObject[] downloadFile(String[] foOids, String savePath) throws Exception{
|
if(foOids != null && foOids.length >0){
|
FileObject[] fileObjects = new FileObject[foOids.length];
|
for(int i = 0; i<foOids.length; i++){
|
//下载文件对象
|
String foOid = foOids[i];
|
FileObject fileObject = operation.getFileObject(foOid);
|
if(fileObject == null || fileObject.getBusinessObject() == null
|
|| fileObject.getBusinessObject().oid == null
|
|| fileObject.getBusinessObject().oid.equals("")) {
|
fileObjects[i] = null;
|
continue;
|
}
|
//下载文件到本地文件夹
|
String fileName = fileObject.getName(); //+ "." + fileObject.getFileType();
|
String fileSavePath = savePath + File.separator + fileName;
|
try {
|
if(!operation.downLoadFileByOid(fileSavePath, fileObject.getOid())){
|
throw new Exception("文件“"+ fileName +"”加载失败!");
|
}
|
|
// if(!operation.downLoadFile(fileSavePath, fileObject.getFilePath())){
|
// throw new Exception("文件“"+ fileName +"”加载失败!");
|
// }
|
} catch (Exception e) {
|
throw new Exception("文件“"+ fileName +"”加载失败!请检查文件服务是否开启或文件柜是否设置!");
|
}
|
fileObjects[i] = fileObject;
|
}
|
return fileObjects;
|
}
|
return null;
|
}
|
}
|