问题描述
客户端代码:importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.InetAddress;importjava.net.Socket;importjava.net.UnknownHostException;publicclassClient1{privateSocketsocket;privatePrintWriterout;finalintport=8110;Stringpath="/home/ocs/cfg/batchTransfer";privateInetAddressaddr;publicClient1(InetAddressaddr){this.addr=addr;}publicvoidrun(){while(true){try{Stringpath="/home/ocs/cfg/batchTransfer";Filef=newFile(path);File[]flist=f.listFiles();System.out.println("flist.length="+flist.length);StringfileName=null;for(inti=0;i<flist.length;i++){fileName=flist[i].getName();//System.out.println(fileName);Stringstr=null;System.out.println("");Filefile=newFile("/home/ocs/cfg/client/bakFile.txt");if(!file.exists())file.createNewFile();System.out.println("notprocess="+BatchTransferLogic.notProcessedFile(fileName));if(BatchTransferLogic.notProcessedFile(fileName)){System.out.println("notProcessfileName="+fileName);socket=newSocket(addr,port);BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(path+"/"+fileName)));System.out.println("br="+br);PrintWriterout=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),true);System.out.println("out="+out);StringBuffersb=newStringBuffer();intsize=0;char[]buf=newchar[1024];while((size=br.read(buf,0,1024))!=-1){System.out.println("size="+size);out.write(buf,0,size);out.flush();}BatchTransferLogic.readFile(path+"/"+fileName);if(br!=null)br.close();if(out!=null)out.close();if(socket!=null)socket.close();}}}catch(Exceptione){System.out.println("inexcepiton");}try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsUnknownHostException,InterruptedException{InetAddressaddr=InetAddress.getByName("10.7.127.100");Client1cl=newClient1(addr);cl.run();}}--------------------------------------------------------------------//查看文件是否被拷贝importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintWriter;publicclassBatchTransferLogic{publicstaticvoidreadFile(StringfileName)throwsIOException{Filef=newFile(fileName);StringrealName=fileName.substring(fileName.lastIndexOf("/")+1);System.out.println("realName="+realName);Filefile=newFile("/home/ocs/cfg/client/bakFile.txt");if(!file.exists())file.createNewFile();OutputStreamos=newFileOutputStream("/home/ocs/cfg/client/bakFile.txt",true);BufferedWriterbw=newBufferedWriter(newPrintWriter(os));byte[]buf=newbyte[8192];StringBuffersb=newStringBuffer();intsize=0;sb.append(realName);sb.append("n");bw.write(sb.toString());bw.flush();if(bw!=null)bw.close();}publicstaticbooleannotProcessedFile(Stringname)throwsIOException{Filefile=newFile("/home/ocs/cfg/client/bakFile.txt");if(!file.exists())file.createNewFile();BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream("/home/ocs/cfg/client/bakFile.txt")));Stringstr=null;while((str=br.readLine())!=null){if(name.equals(str))returnfalse;}returntrue;}publicstaticvoidmain(String[]args)throwsIOException{Filef=newFile("/home/ocs/cfg/client");File[]flist=f.listFiles();for(inti=0;i<flist.length;i++){if(notProcessedFile(flist[i].getName())){readFile("/home/ocs/cfg/client"+flist[i].getName());}}}}//--------------------------------------------------------------------//服务端代码:packagecom.test.logic;importjava.io.BufferedInputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassSer1{publicSer1(Sockets,Stringc)throwsIOException{}publicvoidrun(){}publicstaticvoidmain(String[]args)throwsIOException{ServerSocketserver=newServerSocket(8110);Sockets=null;while(true){try{s=server.accept();InputStreamis=s.getInputStream();BufferedReaderbr=newBufferedReader(newInputStreamReader(is));System.out.println("flag="+is.available());intc=0;Stringpath="E:\bak5";StringfileName=path+"\"+"temp"+System.currentTimeMillis()+".txt";FileOutputStreamfos=null;PrintWriterout=null;intlen=-1;char[]buf=newchar[1024];Stringstr=null;while((len=br.read(buf,0,1024))!=-1){if(!newFile(fileName).exists()){fos=newFileOutputStream(newFile(fileName));out=newPrintWriter(fos);}out.write(buf,0,len);out.flush();}if(out!=null)out.close();}catch(IOExceptione){}}}}