java 远程共享文件_Java 远程文件对接

packagecom.remote;

importjava.io.BufferedOutputStream;

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.OutputStream;

importjava.net.MalformedURLException;

importjava.net.UnknownHostException;

importjava.util.ArrayList;

importjava.util.List;

importjcifs.smb.SmbException;

importjcifs.smb.SmbFile;

importjcifs.smb.SmbFileInputStream;

importjcifs.smb.SmbFileOutputStream;

/***********************************************

*  File Name: RemoteFileUtil.java

*  Created by: JunjieQin

*  Checked in by:

*  Date: 2011-9-6

*  Revision: 1.7

*  Description:操作远程共享文件夹类

*  Amendment History

*  Modified Date:2011-9-16

*  Modified By:JunjieQin

*  Change Description:From local copy files to remote directory

*

***********************************************/

publicclassRemoteFileUtil {

privateArrayList filelist =newArrayList();

RemoteConfigUtil rc =newRemoteConfigUtil();

privateString remoteHostIp;//远程主机IP

privateString account;//登陆账户

privateString password;//登陆密码

privateString shareDocName;//共享文件夹名称

/**

* 默认构造函数

*/

publicRemoteFileUtil(){

this.remoteHostIp = rc.getREMOTE_HOST_IP();

this.account = rc.getLOGIN_ACCOUNT();

this.password = rc.getLOGIN_PASSWORD();

this.shareDocName = rc.getSHARE_DOC_NAME();

}

/**

* 构造函数

* @param remoteHostIp  远程主机Ip

* @param account       登陆账户

* @param password      登陆密码

* @param sharePath     共享文件夹路径

*/

publicRemoteFileUtil(String remoteHostIp, String account, String password,String shareDocName) {

this.remoteHostIp = remoteHostIp;

this.account = account;

this.password = password;

this.shareDocName = shareDocName;

}

/**

* 对远程共享文件进行读取所有行

* @param remoteFileName  文件名  说明:参数为共享目录下的相对路径

*  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);

*  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;

* @return  文件的所有行

*/

publicList readFile(String remoteFileName){

SmbFile smbFile =null;

BufferedReader reader =null;

List resultLines =null;

//构建连接字符串,并取得文件连接

String conStr =null;

conStr ="smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;

try{

smbFile =newSmbFile(conStr);

}catch(MalformedURLException e) {

e.printStackTrace();

}

//创建reader

try{

reader =newBufferedReader(newInputStreamReader(newSmbFileInputStream(smbFile)));

}catch(SmbException e) {

e.printStackTrace();

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(UnknownHostException e) {

e.printStackTrace();

}

//循环对文件进行读取

String line;

try{

line = reader.readLine();

if(line !=null&& line.length()>0){

resultLines =newArrayList();

}

while(line !=null) {

resultLines.add(line);

line = reader.readLine();

}

}catch(IOException e) {

e.printStackTrace();

}

//返回

returnresultLines;

}

/**

* 对远程共享文件进行写入

* @param is                本地文件的输入流

* @param remoteFileName    远程文件名    说明:参数为共享目录下的相对路径

*  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);

*  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;

* @return

*/

publicbooleanwriteFile(InputStream is,String remoteFileName){

SmbFile smbFile =null;

OutputStream os =null;

byte[] buffer =newbyte[1024*8];

//构建连接字符串,并取得文件连接

String conStr =null;

conStr ="smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;

try{

smbFile =newSmbFile(conStr);

}catch(MalformedURLException e) {

e.printStackTrace();

returnfalse;

}

//获取远程文件输出流并写文件到远程共享文件夹

try{

os =newBufferedOutputStream(newSmbFileOutputStream(smbFile));

while((is.read(buffer))!=-1){

os.write(buffer);

}

}catch(Exception e) {

e.printStackTrace();

returnfalse;

}

returntrue;

}

/**

* 对远程共享文件进行写入重载

* @param localFileName   要写入的本地文件全名

* @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径

*  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);

*  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;

* @return

*/

publicbooleanwriteFile(String localFileFullName ,String remoteFileName){

try{

returnwriteFile(newFileInputStream(newFile(localFileFullName)),remoteFileName);

}catch(FileNotFoundException e) {

e.printStackTrace();

returnfalse;

}

}

/**

* 对远程共享文件进行写入重载

* @param localFileName   要写入的本地文件

* @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径

*  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);

*  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;

* @return

*/

publicbooleanwriteFile(File localFile ,String remoteFileName){

try{

returnwriteFile(newFileInputStream(localFile),remoteFileName);

}catch(FileNotFoundException e) {

e.printStackTrace();

returnfalse;

}

}

/**

* 对远程共享文件所有文件

* @return  所有文件

*/

publicList getFiles(){

SmbFile smbFile =null;

BufferedReader reader =null;

List resultLines =newArrayList();

//构建连接字符串,并取得文件连接

String conStr =null;

conStr ="smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/";

try{

smbFile =newSmbFile(conStr);

}catch(MalformedURLException e) {

e.printStackTrace();

}

//创建reader

try{

String[] a = smbFile.list();

for(inti=0;i

resultLines.add(a[i]);

System.out.println(a[i]);

}

}catch(SmbException e) {

e.printStackTrace();

}catch(Exception e) {

e.printStackTrace();

}

//返回

returnresultLines;

}

/** 在本地为共享计算机创建文件夹

* @param remoteUrl 远程计算机路径

*/

publicvoidsmbMkDir(String name) {

// 注意使用jcifs-1.3.15.jar的时候 操作远程计算机的时候所有类前面须要增加Smb

// 创建一个远程文件对象

String conStr ="smb://"+ account +":"+ password +"@"+ remoteHostIp +"/"+ shareDocName;

SmbFile remoteFile;

try{

remoteFile =newSmbFile(conStr +"/"+ name);

if(!remoteFile.exists()) {

remoteFile.mkdir();// 创建远程文件夹

}

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(SmbException e) {

e.printStackTrace();

}

}

/**

* 删除文件夹

* @param folderPath 共享文件夹下一个文件夹名

* @return

*/

publicvoiddelFolder(String folderPath) {

//String conStr = "smb://"+LOGIN_ACCOUNT+":"+LOGIN_PASSWORD+"@"+remoteHostIp+"/"+shareDocName;

try{

delAllFile(folderPath);//删除完里面所有内容

String filePath = folderPath;

filePath = filePath.toString();

SmbFile myFilePath =newSmbFile(filePath);

myFilePath.delete();//删除空文件夹

}

catch(Exception e) {

String message = ("删除文件夹操作出错");

System.out.println(message);

}

}

/**

* 删除共享文件夹下一个文件夹名

* @param path 共享文件夹下一个文件夹名

* @return

* @return

*/

publicbooleandelAllFile(String path) {

booleanbea =false;

try{

SmbFile file =newSmbFile(path);

if(!file.exists()) {

returnbea;

}

if(!file.isDirectory()) {

returnbea;

}

String[] tempList = file.list();

SmbFile temp =null;

for(inti =0; i 

if(path.endsWith("/")) {

temp =newSmbFile(path + tempList[i]);

}else{

temp =newSmbFile(path +"/"+ tempList[i]);

}

if(temp.isFile()) {

temp.delete();

}

if(temp.isDirectory()) {

delAllFile(path +"/"+ tempList[i] +"/");// 先删除文件夹里面的文件

delFolder(path +"/"+ tempList[i] +"/");// 再删除空文件夹

bea =true;

}

}

returnbea;

}catch(Exception e) {

returnbea;

}

}

/**

* 复制整个文件夹的内容

* @param oldPath 准备拷贝的目录

* @param newPath 指定绝对路径的新目录

* @return

*/

publicvoidcopyFolder(String oldPath, String newPath) {

String conStr ="smb://"+ account +":"+ password +"@"+ remoteHostIp +"/"+ shareDocName;

System.err.println(conStr);

try{

/**

* 如果存在文件夹删除文件

* SmbFile exittemp = new SmbFile(conStr + "/"+newPath);

* if(exittemp.exists()){

*      delFolder(conStr+"/"+newPath+"/");

* }

*/

SmbFile exittemps =newSmbFile(conStr +"/"+ newPath);

if(!exittemps.exists()) {

exittemps.mkdirs();// 如果文件夹不存在 则建立新文件夹

}

File a =newFile(oldPath);

String[] file = a.list();

File temp =null;

for(inti =0; i 

if(oldPath.endsWith("/")) {

temp =newFile(oldPath + file[i]);

}else{

temp =newFile(oldPath +"/"+ file[i]);

}

if(temp.isFile()) {

if(temp.exists()) {

writeFile(temp, newPath +"/"+ file[i]);

}

}

if(temp.isDirectory()) {// 如果是子文件夹

copyFolder(oldPath +"/"+ file[i], newPath +"/"+ file[i]);

}

}

}catch(Exception e) {

String message ="复制整个文件夹内容操作出错";

System.out.println(message);

}

}

/**

* 复制文件到远程计算机,如果目标路径不存在则创建,反之不创建

* @param localFileFullName 本地指定文件路径

* @param targetDir 目标路径

*/

publicvoidcopyFileToRemoteDir(String localFileFullName, String targetDir) {

System.err.println(localFileFullName +"--"+ targetDir);

RemoteFileUtil rf =newRemoteFileUtil();

InputStream is =null;

SmbFile smbFile =null;

OutputStream os =null;

byte[] buffer =newbyte[1024*8];

// 构建连接字符串,并取得文件连接

String conStr =null;

conStr ="smb://"+ account +":"+ password +"@"+ remoteHostIp +"/"+ shareDocName +"/"+ targetDir;

System.err.println(conStr);

SmbFile sf;

try{

sf =newSmbFile("smb://"+ account +":"+ password +"@"+ remoteHostIp +"/"+ shareDocName +"/"+ targetDir);

if(!sf.exists()) {

// 新建目标目录

sf.mkdirs();

is =newFileInputStream(newFile(localFileFullName));

// 获取远程文件输出流并写文件到远程共享文件夹

os =newBufferedOutputStream(newSmbFileOutputStream(smbFile));

while((is.read(buffer)) != -1) {

os.write(buffer);

}

}

}catch(Exception e) {

System.err.println("提示:复制整个文件夹内容操作出错。");

}

File file =newFile(localFileFullName);

if(file.isFile()) {

File sourceFile = file;// 源文件

File targetFile =newFile(newFile(targetDir).getAbsolutePath() + File.separator + file.getName());// 目标文件

String name = file.getName();// 文件名

if(targetDir !=null&& targetFile !=null) {

rf.writeFile(sourceFile,"/"+ targetDir +"/"+ name);// 复制文件

}elseif(targetFile !=null) {

rf.writeFile(sourceFile, name);// 复制文件

}

}

}

/**

* 循环获取文件夹内所有匹配的文件

* @param strPath 路径

* @param subStr 匹配字符

* @return

*/

publicArrayList refreshFileList(String strPath, String subStr) {

File dir =newFile(strPath);

File[] files = dir.listFiles();

if(files ==null)

returnnull;

for(inti =0; i 

if(!files[i].isDirectory()) {

String strFileName = files[i].getAbsolutePath().toLowerCase();

if(files[i].getName().indexOf(subStr) >=0) {

filelist.add(files[i].getName());

}

}

}

returnfilelist;

}

// 测试从本地复制文件到远程目标目录,测试已通过

publicstaticvoidmain(String[] args) {

RemoteConfigUtil rc =newRemoteConfigUtil();

RemoteFileUtil util =newRemoteFileUtil();

util.copyFileToRemoteDir(rc.getSourcePath(), rc.getTargetPath());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值