日期:2014-05-20 浏览次数:20824 次
import java.io.DataInputStream; import java.io.FileOutputStream; import java.net.Socket; import java.io.File; public class FileDowProjClient extends Thread { private Socket socket = null; DataInputStream getMessageStream = null; byte b[] = new byte[102400]; int fileNum; public FileDowProjClient() { try { socket = new Socket("127.0.0.1",9999); } catch(Exception ex) { System.out.println("远程服务连接失败:"); ex.printStackTrace(); return; } } int n; File f=null; public boolean getMessageStream() throws Exception { try { DataInputStream in = new DataInputStream(socket.getInputStream()); String myFile = in.readUTF(); String tempFile = "D"+myFile.substring(myFile.indexOf(":")); String strDiretory = ""; int tempIndex = 0; while((tempIndex = tempFile.indexOf("\\")) != -1){ strDiretory += tempFile.substring(0,tempIndex+1); tempFile = tempFile.substring(tempIndex+1); } System.out.println(strDiretory+" ,tempFile is :"+tempFile); File d = new File(strDiretory); d.mkdirs(); File f = new File(strDiretory+tempFile); f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); int ch = 0; while ((ch = in.read()) != -1) { System.out.print((char) ch); fos.write(ch); } fos.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return false; } public static void main(String[] args) { FileDowProjClient fp=new FileDowProjClient(); try { fp.getMessageStream(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class FileDowProjServer implements Runnable { public static FileDowProjServer fileDowProjServer = null; public boolean running = false; DataInputStream fis = null; DataOutputStream out=null; protected FileDowProjServer() { madetree(new File("C:\\src")); start(); } public static FileDowProjServer getInstance() { if (fileDowProjServer == null) { fileDowProjServer = new FileDowProjServer(); } return fileDowProjServer; } public void start() { if (!running) { running = true; (new Thread(this)).start(); } } String[] filePathArray=new String[1000] ; int fileNum; public void run() { System.out.println("服务器监听中..."); ServerSocket server = null; Socket you = null; try { server = new ServerSocket(9999); } catch (IOException ex) { System.out.println("建里服务器:" + ex.getMessage()); } while (true) { try { you = server.accept(); } catch (IOException ee) { System.out.println("正在等待客户:" + ee.getMessage()); } if (you != null){ new DownServer_Thread(you).start(); }else{ continue; } } } void madetree(File dothis) { File[] farray = dothis.listFiles(); for (int i = 0; i < farray.length; i++) { if (farray[i].isFile()){ filePathArray[fileNum++] = farray[i].getAbsolutePath(); }else if(farray[i].isDirectory()) { madetree(farray[i]); } } } class DownServer_Thread extends Thread { Socket socket = null; DataInputStream in = null; String filename = null; DownServer_Thread(Socket c) { socket = c; try { in = new DataInputStream(socket.getInputStream()); } catch (IOException ex) { ex.printStackTrace(); } } public void run() { while (true) { try { for(int k = 0;k < filePathArray.length;k++){ if(filePathArray[k]!=null) { out = new DataOutputStream(socket.getOutputStream()); out.writeUTF(filePathArray[k]); FileInputStream fis = new FileInputStream(filePathArray[k]); int ch = 0; while ((ch = fis.read()) != -1) { out.write(ch); } fis.close(); } } out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) { FileDowProjServer f=new FileDowProjServer(); //f.start(); } }