- 浏览: 80768 次
- 性别:
- 来自: 上海
文章分类
最新评论
package com.jinqiao.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
/**
* @description: 远程共享读写文件操作工具类
* @author:
* @createDate:
*/
public class RemoteFileUtil {
private String remoteHostIp; //远程主机IP
private String account; //登陆账户
private String password; //登陆密码
private String shareDocName; //共享文件夹名称
//配置文件
private PropertiesReader reader = new PropertiesReader("/uploadpath.properties");
/**
* 默认构造函数
*/
public RemoteFileUtil(){
this.remoteHostIp = reader.getProperty("REMOTE_HOST_IP");
this.account = reader.getProperty("LOGIN_ACCOUNT");
this.password = reader.getProperty("LOGIN_PASSWORD");
this.shareDocName = reader.getProperty("SHARE_DOC_NAME");
}
/**
* 构造函数
* @param remoteHostIp 远程主机Ip
* @param account 登陆账户
* @param password 登陆密码
* @param sharePath 共享文件夹路径
*/
public RemoteFileUtil(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 文件的所有行
*/
// public List<String> readFile(String remoteFileName){
public SmbFile readFile(String remoteFileName){
SmbFile smbFile = null;
// BufferedReader reader = null;
// List<String> resultLines = null;
//构建连接字符串,并取得文件连接
String conStr = null;
conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;
try {
smbFile = new SmbFile(conStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return smbFile;
// //创建reader
// try {
// reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(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 = new ArrayList<String>();
// }
// while (line != null) {
// resultLines.add(line);
// line = reader.readLine();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// //返回
// return resultLines;
}
/**
* 对远程共享文件进行写入
* @param is 本地文件的输入流
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(InputStream is,String remoteFileName){
SmbFile smbFile = null;
OutputStream os = null;
byte[] buffer = new byte[1024*8];
//构建连接字符串,并取得文件连接
String conStr = null;
conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;
try {
smbFile = new SmbFile(conStr);
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
}
//获取远程文件输出流并写文件到远程共享文件夹
try {
os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));
while((is.read(buffer))!=-1){
os.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 对远程共享文件进行写入重载
* @param localFileName 要写入的本地文件全名
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(String localFileFullName ,String remoteFileName){
try {
return writeFile(new FileInputStream(new File(localFileFullName)),remoteFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* 对远程共享文件进行写入重载
* @param localFileName 要写入的本地文件
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(File localFile ,String remoteFileName){
try {
return writeFile(new FileInputStream(localFile),remoteFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
/** ***
* 从smbMachine读取文件并存储到localpath指定的路径
*
* @param smbMachine
* 共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
* @param localpath
* 本地路径
* @return
*/
/* public File readFromSmb(String smbMachine,String localpath){
File localfile=null;
InputStream bis=null;
OutputStream bos=null;
try {
SmbFile rmifile = readFile(smbMachine);
String filename=rmifile.getName();
bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
localfile=new File(localpath+File.separator+filename);
bos=new BufferedOutputStream(new FileOutputStream(localfile));
int length=rmifile.getContentLength();
byte[] buffer=new byte[length];
Date date=new Date();
bis.read(buffer);
bos.write(buffer);
Date end=new Date();
int time= (int) ((end.getTime()-date.getTime())/1000);
if(time>0)
System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return localfile;
} */
/* public boolean removeFile(File file) {
return file.delete();
}*/
public static void main(String[] args){
// RemoteFileUtil util = new RemoteFileUtil();
// List<String> lines = util.readFile("test.txt");
// for (String string : lines) {
// System.out.println(string);
// }
// util.writeFile(new File("f:/a.txt"), "new.txt");
// File file=util.readFromSmb("jdk6.ZH_cn.chm","f:/");
// util.writeFile(file, "123.chm");
//// File file=new File("f:/a.txt");
// new File("f:/jdk6.ZH_cn.chm").delete();
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
/**
* @description: 远程共享读写文件操作工具类
* @author:
* @createDate:
*/
public class RemoteFileUtil {
private String remoteHostIp; //远程主机IP
private String account; //登陆账户
private String password; //登陆密码
private String shareDocName; //共享文件夹名称
//配置文件
private PropertiesReader reader = new PropertiesReader("/uploadpath.properties");
/**
* 默认构造函数
*/
public RemoteFileUtil(){
this.remoteHostIp = reader.getProperty("REMOTE_HOST_IP");
this.account = reader.getProperty("LOGIN_ACCOUNT");
this.password = reader.getProperty("LOGIN_PASSWORD");
this.shareDocName = reader.getProperty("SHARE_DOC_NAME");
}
/**
* 构造函数
* @param remoteHostIp 远程主机Ip
* @param account 登陆账户
* @param password 登陆密码
* @param sharePath 共享文件夹路径
*/
public RemoteFileUtil(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 文件的所有行
*/
// public List<String> readFile(String remoteFileName){
public SmbFile readFile(String remoteFileName){
SmbFile smbFile = null;
// BufferedReader reader = null;
// List<String> resultLines = null;
//构建连接字符串,并取得文件连接
String conStr = null;
conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;
try {
smbFile = new SmbFile(conStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return smbFile;
// //创建reader
// try {
// reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(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 = new ArrayList<String>();
// }
// while (line != null) {
// resultLines.add(line);
// line = reader.readLine();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// //返回
// return resultLines;
}
/**
* 对远程共享文件进行写入
* @param is 本地文件的输入流
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(InputStream is,String remoteFileName){
SmbFile smbFile = null;
OutputStream os = null;
byte[] buffer = new byte[1024*8];
//构建连接字符串,并取得文件连接
String conStr = null;
conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;
try {
smbFile = new SmbFile(conStr);
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
}
//获取远程文件输出流并写文件到远程共享文件夹
try {
os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));
while((is.read(buffer))!=-1){
os.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 对远程共享文件进行写入重载
* @param localFileName 要写入的本地文件全名
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(String localFileFullName ,String remoteFileName){
try {
return writeFile(new FileInputStream(new File(localFileFullName)),remoteFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* 对远程共享文件进行写入重载
* @param localFileName 要写入的本地文件
* @param remoteFileName 远程文件名 说明:参数为共享目录下的相对路径
* 若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);
* 若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;
* @return
*/
public boolean writeFile(File localFile ,String remoteFileName){
try {
return writeFile(new FileInputStream(localFile),remoteFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
/** ***
* 从smbMachine读取文件并存储到localpath指定的路径
*
* @param smbMachine
* 共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
* @param localpath
* 本地路径
* @return
*/
/* public File readFromSmb(String smbMachine,String localpath){
File localfile=null;
InputStream bis=null;
OutputStream bos=null;
try {
SmbFile rmifile = readFile(smbMachine);
String filename=rmifile.getName();
bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
localfile=new File(localpath+File.separator+filename);
bos=new BufferedOutputStream(new FileOutputStream(localfile));
int length=rmifile.getContentLength();
byte[] buffer=new byte[length];
Date date=new Date();
bis.read(buffer);
bos.write(buffer);
Date end=new Date();
int time= (int) ((end.getTime()-date.getTime())/1000);
if(time>0)
System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return localfile;
} */
/* public boolean removeFile(File file) {
return file.delete();
}*/
public static void main(String[] args){
// RemoteFileUtil util = new RemoteFileUtil();
// List<String> lines = util.readFile("test.txt");
// for (String string : lines) {
// System.out.println(string);
// }
// util.writeFile(new File("f:/a.txt"), "new.txt");
// File file=util.readFromSmb("jdk6.ZH_cn.chm","f:/");
// util.writeFile(file, "123.chm");
//// File file=new File("f:/a.txt");
// new File("f:/jdk6.ZH_cn.chm").delete();
}
}
发表评论
-
java ZIP压缩工具类
2015-01-28 14:35 978package com.common.util; i ... -
java ftp工具类
2015-01-28 14:33 1432package com.common.util; i ... -
java socket编程
2014-01-02 16:53 1182引用 /** * 工行实名认证处理方法 * ... -
Spring_Security_多页面登录配置教程
2011-08-08 10:13 17210本文转自百度文库:http://wenku.baidu.com ... -
从数据库中读取带换行的字符串
2011-06-01 09:25 7172数据库中的workExp(工作经历)字段是带换行或回车的字符串 ... -
向已获取的list中插入值
2011-08-08 11:23 1995public List getWfList(String id ... -
正则表达式常用验证方法
2011-05-26 15:02 1477function isDigit(s) { var patr ... -
java常用操作方法(五)金额 MoneyUtil
2011-05-26 14:57 2044package com.jinqiao.util; impo ... -
java常用操作方法(四)数学计算操作 MathUtil
2011-05-26 14:55 8877package com.jinqiao.util; impo ... -
java常用操作方法(三)字符串工具类 StringUtil
2011-05-26 14:52 1844package com.jinqiao.util; impo ... -
java常用操作方法(二)文件操作类 FileUtil
2011-05-26 14:50 4516package com.jinqiao.util; impo ... -
java常用操作方法(一)日期工具类 DateUtil
2011-05-26 14:46 3347/* * Created on 2011-5-26 * ...
相关推荐
NULL 博文链接:https://chong0660.iteye.com/blog/2367232
JAVA代码实现远程操作服务器文件,实现FTP,共享文件夹操作
JAVA实现远程文件读取
本文件是将 Java 中 通过远程url访问 转换成 HTML 文件 ,通过 dom4j转换成Java对象元素
java实现上传文件到远程服务器(spring mvc)
java 读取远程共享文件,一个操作简单的读取远程资源的控件。
java线程常用操作方法笔记,每一点都是经过本人精心筛选出来的重点!
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
JAVA通过SMB的方式下载、上传远程Linux服务器的文件
JAVA读取远程网页文件并保存本地 从远程URL地址获取网页文件下载到本地 这个方法可以生成静态HTML文件使用!
Java文件夹复制(远程复制(网络传输),用于远程备份文件)(支持文件夹,嵌套子文件夹) import java.io.*; import java.util.*; public class FileSelection { private File rootDirectory;//根目录 private ...
用JAVA实现缓冲多线程无阻塞读取远程文件
JAVA代码实现远程服务器的文件操作步骤及JAR包.zip
用Java多线程实现无阻塞读取远程文件!
一个通过Java IO操作的,能够在远程机子上生成批量文件的源码
Java文件操作中的一些常用方法的总结,可以参考参考啦!
NULL 博文链接:https://hb-yym-929.iteye.com/blog/1062642
java通过文件头内容判断文件类型
java 读取远程网站上的 数据,如 :图片,附件,视频等
java上传文件到linux服务器,操作linux服务器上文件,下载linux服务器文件,删除linux服务器文件。ganymed-ssh2远程操作服务器文件