1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.SequenceInputStream; import java.net.Socket; public class FileClient { /** * @param args */ public static void main(String[] args) { try { Socket s = new Socket("192.168.1.100",8800); File file = new File("h:/DPP3.9_60D(1).rar"); FileInputStream fs = new FileInputStream(file); //定义一个256字节的区域来保存文件信息。 byte[] b = file.getName().getBytes(); byte[] info = Arrays.CopyOf(b,256); ByteArrayInputStream bais = new ByteArrayInputStream(info); //合并流 SequenceInputStream sis = new SequenceInputStream(bais,fs); BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream()); byte[] buf = new byte[1024]; int len = 0; while((len = sis.read(buf))!=-1){ bos.write(buf,0,len); } bos.close(); sis.close(); fs.close(); bais.close(); } catch (Exception e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FileServer { /** * @param args */ public static void main(String[] args) { ExecutorService threadpool = Executors.newFixedThreadPool(10); try { ServerSocket ss = new ServerSocket(8800); while (true) { threadpool.execute(new UploadThread(ss.accept())); } } catch (IOException e) { e.printStackTrace(); } } } class UploadThread implements Runnable { private Socket s; UploadThread(Socket s) { this.s = s; } @Override public void run() { System.out.println(s.getInetAddress().getHostAddress()); try { BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); byte[] info = new byte[256]; bis.read(info); String file_name = new String(info).trim(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:/"+file_name)); byte[] buf = new byte[1024]; int len = 0; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } bos.close(); bis.close(); s.close(); System.out.println(file_name+" 文件传输成功"); } catch (IOException e) { e.printStackTrace(); } } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |