如何实现当前程序在其他机器上的备份

newjavamaker 2008-12-19 02:55:29
最近在研究文件备份功能,现在代码初步实现了本机的备份!
代码如下,问题是,当我的地址是例如10.100.1.28的linux服务器的/home/ebadmin/beifen目录下我该如何用当前的程序去备份,实验了很多种地址路径,均没有实现。当前ebadmin的权限是可以往当前beifen文件夹内写入的。
地址一 String url1是个固定的地址
地址二 我的目标地址应该如何写?才能存到相应的10.100.1.28指定的目录beifen下?

import java.io.*; 

public class copyDirectory {
public static void main(String args[]) throws IOException {
String url1="C:/ccc/";
String url2="C:/baobaobao/aaa/";
copyDirectory a=new copyDirectory();
System.out.println(a.copyDirectiory(url1, url2));
}
public static boolean copyDirectiory(String file1,String file2) throws IOException{
try{
(new File(file2)).mkdirs();
File[] file=(new File(file1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){ //如果是对某个文件的备份
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(file2+"/"+file[i].getName());
byte[] b=new byte[1024*10];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){ //如果是对某个文件包的备份
copyDirectiory(file2+"/"+file[i].getName(),file1+"/"+file[i].getName());
return true;
}

}
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}

}
}

谢谢大家帮助讨论和解决!
...全文
91 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hammer400 2008-12-19
  • 打赏
  • 举报
回复

reiz6153 2008-12-19
  • 打赏
  • 举报
回复
他好像只是想用java实现这个功能罢了,不见得是真实的项目;如果是真实的项目,那ftp也用不上啊,在linux下做远程备份,可以不需要人来干预。
jiang_jiajia10 2008-12-19
  • 打赏
  • 举报
回复
帮顶
wfeng007 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 newjavamaker 的回复:]
[Quote=引用   5   楼   wfeng007   的回复:]
你写个ftpclient   传输过去不就好了....
或者直接让linux   共享目录呀...
[/Quote]
这个只是一个备份JAVA程序的目的是用一个跑批,将系统当前日期的前一天所有目录下的文件进行备份,直接传输?没有用过请赐教!
[/Quote]

要看你目的到底是啥,是开发备份软件? 或者就是为了解决备份问题?...
如果是后者,直接用shell写脚本就行了 tar一下然后ftp....你要定时处理的话直接用crontab哦 ...

如果是后者,我所说的FTPclient...就是你自己写一个ftp操作的客户端阿..java有很多ftp组件包的都很简单的...
然后打包么直接用java内置的zip组件包就行了...zip一下然后ftp...
这个应该很简单的阿.... 你非要自己写程序将文件这个读入JVM干吗呢?没必要么...



bobskay 2008-12-19
  • 打赏
  • 举报
回复
很久以前从别的地方抄过来的,看下对你有没用

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import sun.net.TelnetOutputStream;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

public class download {
String localfilename;

String remotefilename;

FtpClient ftpClient;

// server:服务器名字
// user:用户名
// password:密码
// path:服务器上的路径
public void connectServer(String ip, int port, String user, String password, String path) {

try {
ftpClient = new FtpClient();
ftpClient.openServer(ip, port);
ftpClient.login(user, password);
System.out.println("login success!");
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
} catch (IOException ex) {
System.out.println("not login");
System.out.println(ex);
}
}

public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
System.out.println(ex);
}
}

public void upload() {

this.localfilename = "D://test2//test.txt";
this.remotefilename = "test.txt";

try {
TelnetOutputStream os = ftpClient.put(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
is.close();
os.close();
} catch (IOException ex) {
System.out.println("not upload");
System.out.println(ex);
}
}

public void download() {

try {
TelnetInputStream is = ftpClient.get(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}

System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}

public void download(String remotePath, String remoteFile, String localFile) {

try {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}

System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}

public void download(String remoteFile, String localFile) {

try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}

System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}

public static void main(String agrs[]) {

String filepath[] = { "/callcenter/index.jsp", "/callcenter/ip.txt",
"/callcenter/mainframe/image/processing_bar_2.gif", "/callcenter/mainframe/image/logo_01.jpg" };
String localfilepath[] = { "C:\\FTP_Test\\index.jsp", "C:\\FTP_Test\\ip.txt",
"C:\\FTP_Test\\processing_bar_2.gif", "C:\\FTP_Test\\logo_01.jpg" };

download fu = new download();
fu.connectServer("172.16.1.66", 22, "web_test", "123456", "/callcenter");
for (int i = 0; i < filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}

// fu.upload();
// fu.download();
fu.closeConnect();

}
}
reiz6153 2008-12-19
  • 打赏
  • 举报
回复
和远程机器建立socket连接,取得输入输出流,其他的和你做本机copy一样
newjavamaker 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wfeng007 的回复:]
你写个ftpclient 传输过去不就好了....
或者直接让linux 共享目录呀...
[/Quote]
这个只是一个备份JAVA程序的目的是用一个跑批,将系统当前日期的前一天所有目录下的文件进行备份,直接传输?没有用过请赐教!
wfeng007 2008-12-19
  • 打赏
  • 举报
回复
你写个ftpclient 传输过去不就好了....
或者直接让linux 共享目录呀...
newjavamaker 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 reiz6153 的回复:]
如果是standalone程序的话,用socket了。
[/Quote]
您说的是什么DD啊 @_@不懂,我就是想实现用普通的JAVA程序在远程机器上的备份!
reiz6153 2008-12-19
  • 打赏
  • 举报
回复
如果是standalone程序的话,用socket了。
newjavamaker 2008-12-19
  • 打赏
  • 举报
回复
谢谢楼上的兄弟,貌似跟那个路径间隔符没太大关系LINUX是只认识“/"的我直接在String url2="10.100.1.28/home/ebadmin/policybak/";我是直接写死的。请研究过这个问题的兄弟发表下意见谢谢,很着急啊呵呵!
思無芷盡 2008-12-19
  • 打赏
  • 举报
回复
你要保存另一台机子上 确定的存放路径的话 各种系统的路径间隔符是不一样的。
比如获取该计算机系统对应的 系统路径间隔符。
public String getPathSpace(){
return System.getProperties().getProperty("file.separator");
}
你试试不知道与这个有关么有。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧