- 浏览: 81522 次
- 性别:
- 来自: 上海
文章分类
最新评论
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 1017package com.common.util; i ... -
java ftp工具类
2015-01-28 14:33 1468package com.common.util; i ... -
java socket编程
2014-01-02 16:53 1217引用 /** * 工行实名认证处理方法 * ... -
Spring_Security_多页面登录配置教程
2011-08-08 10:13 17269本文转自百度文库:http://wenku.baidu.com ... -
从数据库中读取带换行的字符串
2011-06-01 09:25 7205数据库中的workExp(工作经历)字段是带换行或回车的字符串 ... -
向已获取的list中插入值
2011-08-08 11:23 2024public List getWfList(String id ... -
正则表达式常用验证方法
2011-05-26 15:02 1511function isDigit(s) { var patr ... -
java常用操作方法(五)金额 MoneyUtil
2011-05-26 14:57 2088package com.jinqiao.util; impo ... -
java常用操作方法(四)数学计算操作 MathUtil
2011-05-26 14:55 8902package com.jinqiao.util; impo ... -
java常用操作方法(三)字符串工具类 StringUtil
2011-05-26 14:52 1879package com.jinqiao.util; impo ... -
java常用操作方法(二)文件操作类 FileUtil
2011-05-26 14:50 4549package com.jinqiao.util; impo ... -
java常用操作方法(一)日期工具类 DateUtil
2011-05-26 14:46 3402/* * Created on 2011-5-26 * ...
相关推荐
"JAVA代码实现远程操作服务器文件" Titulo: JAVA代码实现远程操作服务器文件 JAVA代码实现远程操作服务器文件是指使用JAVA语言实现远程操作服务器文件的功能,实现FTP,共享文件夹操作。该功能可以实现远程服务器...
Java远程传输文件 Java是一种跨平台的语言,在实际开发中,经常需要传输文件,该代码是一个很好的传输文件的例子。...该代码实现了Java远程传输文件的功能,使用了多种技术和方法来实现文件的下载和上传。
`uploadFile`方法接收文件路径、文件键名、请求URL以及额外参数等信息,然后调用内部私有方法进行实际的文件上传操作。 ##### 内部上传逻辑 ```java private static String uploadFile(File file, String fileKey,...
标题 "Java读取远程主机文件" 涉及的核心知识点主要集中在Java远程操作技术,特别是使用SSH(Secure Shell)协议来实现对远程主机的文件访问。在这个场景中,开发人员可能需要从远程服务器上下载文件或者实时读取...
JAVA下载远程文件到本地的最精简代码,就一行代码,不信自己看,而且是官方提供,绝对适合你,一个远程URL,一个本地路径,文件就在你的电脑上了
在Java编程领域,远程文件操作是一项重要的功能,特别是在分布式系统和云计算环境中。"Java远程批量文件生成"这个项目就是利用Java的IO(输入/输出)功能来实现在远程服务器上创建多个文件的功能。下面我们将详细...
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
总的来说,Java 文件夹复制和远程备份涉及到了文件操作、网络通信、异常处理、效率优化以及数据安全等多个方面,这些知识点对于构建一个可靠的远程备份系统至关重要。在实际应用中,还需要根据具体需求进行定制和...
在Java编程环境中,实现远程文件传输是一项常见的任务,特别是在分布式系统和网络应用中。这个小程序可能涉及到了几个关键的Java技术点,包括网络编程、I/O流处理以及可能的多线程技术。以下是对这些知识点的详细...
5. **断开连接**:完成远程桌面操作后,记得调用`disconnect()`方法关闭连接。 接下来,我们转向在Java Web应用中使用`properJavaRDP`。这种方式通常用于为用户提供网页上的远程桌面访问功能。实现过程包括: 1. *...
本篇文章将深入探讨如何使用Java来实现对Linux服务器的文件上传、操作、下载和删除,以及如何借助ganymed-ssh2库实现远程操作。 首先,让我们了解基础概念。Linux服务器是一种基于Linux操作系统并提供网络服务的...
(java 中实现对 access 数据库的远程访问) java 中实现对 access ...此外,在实现对 access 数据库的远程访问时,需要了解 RmiJdbc 服务的工作原理,以及 jdbc:rmi 链接字符串的使用方法,以便更好地实现远程访问。
6. **BSFile.java**: 文件操作是Java开发中的常见任务,这个文件可能包含了创建、删除、移动、复制文件或目录的方法,如`createFile()`、`deleteFile()`、`copyFile()`、`renameFile()`等。 7. **...
在Java编程中,判断文件类型通常不是通过文件扩展名,而是通过读取文件的头部字节来识别。这是因为有些文件可能...理解并掌握这个方法,能够帮助我们在实际开发中更好地处理各种文件操作,提高程序的健壮性和安全性。
在IT行业中,Java是一种广泛应用的编程语言,尤其在文件操作和网络通信方面有着广泛的应用。本文将详细讨论如何使用Java连接FTP(File Transfer Protocol)服务器,进行文件的上传、下载、删除以及复制等操作。 ...
本文档的标题是"Java 运用 Ganymed-SSH2 库远程连接操作 Linux 服务器",这意味着我们将使用 Java 语言来远程连接 Linux 服务器,并使用 Ganymed-SSH2 库来实现远程连接和文件传输。 描述解释 描述部分提到使用 ...
RMI使用Java语言接口定义了远程对象,并集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。RMI使原先的程序在同一操作系统的方法调用,变成了不同操作系统之间程序的方法调用。 RMI的优点: * ...
Java语言中写入大数据文件是指使用Java语言编写程序将大量数据写入到文件中的一种操作。这种操作在实际应用中非常常见,如数据分析、数据挖掘、科学计算等领域。在Java中,写入大数据文件通常需要考虑文件的大小、...
1分让你得到sftp常用操作工具,工具实现如下操作: 1)得到当前工作目录地址 2)改变目录为配置的远程目录 3)取文件目录列表 4)取文件列表 5)下载文件 6)复制文件 7)删除文件 8)目录是否存在,文件是否存在 9)移动文件 ...
在Java编程环境中,有时我们需要在Windows系统中远程访问Linux服务器以获取或操作文件。`JSch`库提供了一个这样的解决方案,它是一个纯Java实现的SSH2库,允许开发者连接到远程计算机并执行命令,传输文件等。本篇将...