區域網路檔案傳輸和複制,含檔案名
實施區域網路内檔案複制,包括需要把檔案名也拷貝過去。特點是檔案名傳輸和檔案傳輸動用了兩個端口。程序完成這樣的功能:每隔24小時進
行上傳一次,保持兩台機器的根目錄下檔案一緻。
1:伺服器端,也既待上傳端。運作指令:java FileServer 3107
import java.io.*;
import java.net.*;
public class FileServer implements Runnable {
private static int ipsocket = 0;
public static void main(String[] args)throws Exception{
ipsocket = Integer.parseInt(args[0]);
//建議使用3107
System.out.println("系統在端口:" + ipsocket + "等待發送");
Thread t1 = new Thread(new FileServer());
t1.start();
}
public void run(){
while(true){
try{
String[] fileNames = seachFileName(1);
if(fileNames == null || fileNames.length < 1) return;
for(int i = 0; i < fileNames.length; i ++){
sendFileName(fileNames[i]);
sendFile(fileNames[i]);
}
Thread.sleep(24 * 60 * 60 * 1000); //24小時
}catch(Exception e) {
System.out.println(e);
}
}
}
public static String[] seachFileName(int listType) throws Exception{
String directory = ".";
File dir = new File(directory);
File dirTemp;
String[] filesTemp;
String[] fileNames;
String[] re;
int length = 0;
if (!dir.isDirectory())
throw new IllegalArgumentException("FileList: no such directory");
filesTemp = dir.list();
java.util.Arrays.sort(filesTemp);
for(int i = 0; i < filesTemp.length; i++) {
dirTemp = new File(directory + "//" + filesTemp[i]);
if(!dirTemp.isDirectory()){
length++;
}
}
fileNames = new String[length];
length = 0;
for(int i = 0; i < filesTemp.length; i++) {
dirTemp = new File(directory + "//" + filesTemp[i]);
if(!dirTemp.isDirectory()){
fileNames[length] = filesTemp[i];
}
length++;
}
if(listType == 0) re = filesTemp;
else re = fileNames;
return re;
}
private static void sendFileName(String fileNames) throws Exception{
if(fileNames == null) return;
//建立網絡伺服器接受客戶請求
ServerSocket ss=new ServerSocket(ipsocket);
Socket client=ss.accept();
//建立網絡輸出流并提供資料包裝器
OutputStream netOut=client.getOutputStream();
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
//建立檔案讀取緩沖區
byte[] buf=null;
String fileName = fileNames;
buf = fileName.getBytes();
int num=buf.length;
if(num > 0){//是否讀完檔案
doc.write(buf,0,num);//把檔案資料寫出網絡緩沖區
doc.flush();//重新整理緩沖區把資料寫往用戶端
}
doc.close();
ss.close();
}
private static void sendFile(String fileName) throws Exception{
if(fileName == null) return;
//建立檔案流用來讀取檔案中的資料
File file=new File(fileName);
FileInputStream fos=new FileInputStream(file);
//建立網絡伺服器接受客戶請求
ServerSocket ss=new ServerSocket(ipsocket + 1);
Socket client=ss.accept();
//建立網絡輸出流并提供資料包裝器
OutputStream netOut=client.getOutputStream();
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
//建立檔案讀取緩沖區
byte[] buf=new byte[2048];
int num=fos.read(buf);
while(num!=(-1)){//是否讀完檔案
doc.write(buf,0,num);//把檔案資料寫出網絡緩沖區
doc.flush();//重新整理緩沖區把資料寫往用戶端
num=fos.read(buf);//繼續從檔案中讀取資料
}
fos.close();
doc.close();
ss.close();
}
}
2:用戶端,也既檔案接受端。運作指令:java FileClient 127.0.0.1 3107。注意跟伺服器端口要一緻。
import java.io.*;
import java.net.*;
public class FileClient implements Runnable {
private static String ip = "";
private static int ipsocket = 0;
public static void main(String[] args)throws Exception{
ip = args[0];
ipsocket = Integer.parseInt(args[1]);
//建議使用3107
System.out.println("系統在位址@" + ip + ":" + ipsocket + "傾聽");
Thread t1 = new Thread(new FileClient());
t1.start();
}
public void run(){
while(true){
try{
String strTemp = getFileName();
getFile(strTemp);
}catch(Exception e){}
try{
Thread.sleep(5 * 1000); //5秒
}catch(Exception e) {
System.out.println(e);
}
}
}
private static String getFileName() throws Exception{
// 通過Socket連接配接檔案伺服器
Socket server=new Socket(ip,ipsocket);
//建立網絡接受流接受伺服器檔案資料
InputStream netIn=server.getInputStream();
InputStream in=new DataInputStream(new BufferedInputStream(netIn));
//建立緩沖區緩沖網絡資料
byte[] buf=new byte[2048];
int num=in.read(buf);
while(num!=(-1)){//是否讀完所有資料
num=in.read(buf);//繼續從網絡中讀取檔案
}
String fileName = new String(buf);
fileName = fileName.trim();
in.close();
server.close();
return fileName;
}
private static void getFile(String strTemp) throws Exception{
//使用本地檔案系統接受網絡資料并存為新檔案
File file=new File(strTemp);
//如果檔案已經存在,先删除
if(file.exists()) file.delete();
for(int i = 0 ; i < 10000; i++) {}
file.createNewFile();
RandomAccessFile raf=new RandomAccessFile(file,"rw");
// 通過Socket連接配接檔案伺服器
Socket server=new Socket(ip,(ipsocket + 1) );
//建立網絡接受流接受伺服器檔案資料
InputStream netIn=server.getInputStream();
InputStream in=new DataInputStream(new BufferedInputStream(netIn));
//建立緩沖區緩沖網絡資料
byte[] buf=new byte[2048];
int num=in.read(buf);
while(num!=(-1)){//是否讀完所有資料
raf.write(buf,0,num);//将資料寫往檔案
raf.skipBytes(num);//順序寫檔案位元組
num=in.read(buf);//繼續從網絡中讀取檔案
}
in.close();
raf.close();
server.close();
}
}
關于本文:純參考用,有意見歡迎BT
[email protected]或www.10ms.net