用Socket实现PC和手机的文件传输

来源:互联网 发布:php 互换数组里面的值 编辑:程序博客网 时间:2024/05/01 18:22

PC服务器端代码:




[java] view plaincopyprint?
  1. /* 
  2.  * PC与Android客户端实现文件的传送 
  3.  * PC服务器端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.IOException;  
  12. import java.net.ServerSocket;  
  13. import java.net.Socket;  
  14. public class TransferFileServer {  
  15.     private static final int HOST_PORT = 8821;  
  16.     private void start() {  
  17.         Socket s = null;  
  18.         try {  
  19.             ServerSocket ss = new ServerSocket(HOST_PORT);  
  20.             while (true) {  
  21.                 String filePath = "/home/fan/Pictures/1.jpg";  
  22.                 File file = new File(filePath);  
  23.                 System.out.println("文件长度:" + (int) file.length());  
  24.                 s = ss.accept();  
  25.                 log("建立Socket连接");  
  26.                 DataInputStream dis = new DataInputStream(  
  27.                         new BufferedInputStream(s.getInputStream()));  
  28.                 dis.readByte();  
  29.                 DataInputStream fis = new DataInputStream(  
  30.                         new BufferedInputStream(new FileInputStream(filePath)));  
  31.                 DataOutputStream dos = new DataOutputStream(s.getOutputStream());  
  32.                 dos.writeUTF(file.getName());  
  33.                 dos.flush();  
  34.                 dos.writeLong((long) file.length());  
  35.                 dos.flush();  
  36.                 int bufferSize = 8192;  
  37.                 byte[] buf = new byte[bufferSize];  
  38.                 while (true) {  
  39.                     int read = 0;  
  40.                     if (fis != null) {  
  41.                         read = fis.read(buf);  
  42.                     }  
  43.                     if (read == -1) {  
  44.                         break;  
  45.                     }  
  46.                     dos.write(buf,0,read);  
  47.                 }  
  48.                 dos.flush();  
  49.                  // 注意关闭socket链接哦,不然客户端会等待server的数据过来,  
  50.                 // 直到socket超时,导致数据不完整。  
  51.                 fis.close();  
  52.                 s.close();  
  53.                 log("文件传输完成");  
  54.             }  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.     void log(String msg) {  
  61.         System.out.println(msg);  
  62.     }  
  63.     public static void main(String args[]) {  
  64.         new TransferFileServer().start();  
  65.     }  
  66. }  
[java] view plain copy
 print?
  1. /* 
  2.  * PC与Android客户端实现文件的传送 
  3.  * PC服务器端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.IOException;  
  12. import java.net.ServerSocket;  
  13. import java.net.Socket;  
  14. public class TransferFileServer {  
  15.     private static final int HOST_PORT = 8821;  
  16.     private void start() {  
  17.         Socket s = null;  
  18.         try {  
  19.             ServerSocket ss = new ServerSocket(HOST_PORT);  
  20.             while (true) {  
  21.                 String filePath = "/home/fan/Pictures/1.jpg";  
  22.                 File file = new File(filePath);  
  23.                 System.out.println("文件长度:" + (int) file.length());  
  24.                 s = ss.accept();  
  25.                 log("建立Socket连接");  
  26.                 DataInputStream dis = new DataInputStream(  
  27.                         new BufferedInputStream(s.getInputStream()));  
  28.                 dis.readByte();  
  29.                 DataInputStream fis = new DataInputStream(  
  30.                         new BufferedInputStream(new FileInputStream(filePath)));  
  31.                 DataOutputStream dos = new DataOutputStream(s.getOutputStream());  
  32.                 dos.writeUTF(file.getName());  
  33.                 dos.flush();  
  34.                 dos.writeLong((long) file.length());  
  35.                 dos.flush();  
  36.                 int bufferSize = 8192;  
  37.                 byte[] buf = new byte[bufferSize];  
  38.                 while (true) {  
  39.                     int read = 0;  
  40.                     if (fis != null) {  
  41.                         read = fis.read(buf);  
  42.                     }  
  43.                     if (read == -1) {  
  44.                         break;  
  45.                     }  
  46.                     dos.write(buf,0,read);  
  47.                 }  
  48.                 dos.flush();  
  49.                  // 注意关闭socket链接哦,不然客户端会等待server的数据过来,  
  50.                 // 直到socket超时,导致数据不完整。  
  51.                 fis.close();  
  52.                 s.close();  
  53.                 log("文件传输完成");  
  54.             }  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.     void log(String msg) {  
  61.         System.out.println(msg);  
  62.     }  
  63.     public static void main(String args[]) {  
  64.         new TransferFileServer().start();  
  65.     }  
  66. }  
 

ClientSocket:辅助类

[java] view plaincopyprint?
  1. /* 
  2.  * PC与Android文件传输 
  3.  * Android客户端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.IOException;  
  10. import java.net.Socket;  
  11. import java.net.UnknownHostException;  
  12. public class ClientSocket {  
  13.     private String ip;  
  14.     private int port;  
  15.     private Socket socket = null;  
  16.     DataOutputStream out = null;  
  17.     DataInputStream getMessageStream = null;  
  18.     public ClientSocket(String ip, int port) {  
  19.         this.ip = ip;  
  20.         this.port = port;  
  21.     }  
  22.     public void createConnection() {  
  23.         try {  
  24.             socket = new Socket(ip, port);  
  25.         } catch (UnknownHostException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.             if (socket != null) {  
  29.                 try {  
  30.                     socket.close();  
  31.                 } catch (IOException e1) {  
  32.                     // TODO Auto-generated catch block  
  33.                     e1.printStackTrace();  
  34.                 }  
  35.             }  
  36.         } catch (IOException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.             if (socket != null) {  
  40.                 try {  
  41.                     socket.close();  
  42.                 } catch (IOException e1) {  
  43.                     // TODO Auto-generated catch block  
  44.                     e1.printStackTrace();  
  45.                 }  
  46.             }  
  47.         } finally {  
  48.         }  
  49.     }  
  50.     public void sendMessage(String sendMessage) {  
  51.         try {  
  52.             out = new DataOutputStream(socket.getOutputStream());  
  53.             if (sendMessage.equals("Windows")) {  
  54.                 out.writeByte(0x1);  
  55.                 out.flush();  
  56.                 return;  
  57.             }  
  58.             if (sendMessage.equals("Unix")) {  
  59.                 out.writeByte(0x2);  
  60.                 out.flush();  
  61.                 return;  
  62.             }  
  63.             if (sendMessage.equals("Linux")) {  
  64.                 out.writeByte(0x3);  
  65.                 out.flush();  
  66.             } else {  
  67.                 out.writeUTF(sendMessage);  
  68.                 out.flush();  
  69.             }  
  70.         } catch (IOException e) {  
  71.             // TODO Auto-generated catch block  
  72.             e.printStackTrace();  
  73.             if (out != null) {  
  74.                 try {  
  75.                     out.close();  
  76.                 } catch (IOException e1) {  
  77.                     // TODO Auto-generated catch block  
  78.                     e1.printStackTrace();  
  79.                 }  
  80.             }  
  81.         }  
  82.     }  
  83.     public DataInputStream getMessageStream() {  
  84.         try {  
  85.             getMessageStream = new DataInputStream(new BufferedInputStream(  
  86.                     socket.getInputStream()));  
  87.             // return getMessageStream;  
  88.         } catch (IOException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.             if (getMessageStream != null) {  
  92.                 try {  
  93.                     getMessageStream.close();  
  94.                 } catch (IOException e1) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e1.printStackTrace();  
  97.                 }  
  98.             }  
  99.         }  
  100.         return getMessageStream;  
  101.     }  
  102.     public void shutDownConnection() {  
  103.         try {  
  104.             if (out != null) {  
  105.                 out.close();  
  106.             }  
  107.             if (getMessageStream != null) {  
  108.                 getMessageStream.close();  
  109.             }  
  110.             if (socket != null) {  
  111.                 socket.close();  
  112.             }  
  113.         } catch (IOException e) {  
  114.             // TODO Auto-generated catch block  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118. }  
[java] view plain copy
 print?
  1. /* 
  2.  * PC与Android文件传输 
  3.  * Android客户端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.IOException;  
  10. import java.net.Socket;  
  11. import java.net.UnknownHostException;  
  12. public class ClientSocket {  
  13.     private String ip;  
  14.     private int port;  
  15.     private Socket socket = null;  
  16.     DataOutputStream out = null;  
  17.     DataInputStream getMessageStream = null;  
  18.     public ClientSocket(String ip, int port) {  
  19.         this.ip = ip;  
  20.         this.port = port;  
  21.     }  
  22.     public void createConnection() {  
  23.         try {  
  24.             socket = new Socket(ip, port);  
  25.         } catch (UnknownHostException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.             if (socket != null) {  
  29.                 try {  
  30.                     socket.close();  
  31.                 } catch (IOException e1) {  
  32.                     // TODO Auto-generated catch block  
  33.                     e1.printStackTrace();  
  34.                 }  
  35.             }  
  36.         } catch (IOException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.             if (socket != null) {  
  40.                 try {  
  41.                     socket.close();  
  42.                 } catch (IOException e1) {  
  43.                     // TODO Auto-generated catch block  
  44.                     e1.printStackTrace();  
  45.                 }  
  46.             }  
  47.         } finally {  
  48.         }  
  49.     }  
  50.     public void sendMessage(String sendMessage) {  
  51.         try {  
  52.             out = new DataOutputStream(socket.getOutputStream());  
  53.             if (sendMessage.equals("Windows")) {  
  54.                 out.writeByte(0x1);  
  55.                 out.flush();  
  56.                 return;  
  57.             }  
  58.             if (sendMessage.equals("Unix")) {  
  59.                 out.writeByte(0x2);  
  60.                 out.flush();  
  61.                 return;  
  62.             }  
  63.             if (sendMessage.equals("Linux")) {  
  64.                 out.writeByte(0x3);  
  65.                 out.flush();  
  66.             } else {  
  67.                 out.writeUTF(sendMessage);  
  68.                 out.flush();  
  69.             }  
  70.         } catch (IOException e) {  
  71.             // TODO Auto-generated catch block  
  72.             e.printStackTrace();  
  73.             if (out != null) {  
  74.                 try {  
  75.                     out.close();  
  76.                 } catch (IOException e1) {  
  77.                     // TODO Auto-generated catch block  
  78.                     e1.printStackTrace();  
  79.                 }  
  80.             }  
  81.         }  
  82.     }  
  83.     public DataInputStream getMessageStream() {  
  84.         try {  
  85.             getMessageStream = new DataInputStream(new BufferedInputStream(  
  86.                     socket.getInputStream()));  
  87.             // return getMessageStream;  
  88.         } catch (IOException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.             if (getMessageStream != null) {  
  92.                 try {  
  93.                     getMessageStream.close();  
  94.                 } catch (IOException e1) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e1.printStackTrace();  
  97.                 }  
  98.             }  
  99.         }  
  100.         return getMessageStream;  
  101.     }  
  102.     public void shutDownConnection() {  
  103.         try {  
  104.             if (out != null) {  
  105.                 out.close();  
  106.             }  
  107.             if (getMessageStream != null) {  
  108.                 getMessageStream.close();  
  109.             }  
  110.             if (socket != null) {  
  111.                 socket.close();  
  112.             }  
  113.         } catch (IOException e) {  
  114.             // TODO Auto-generated catch block  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118. }  
 

Android客户端:Acitiviy实现:

[java] view plaincopyprint?
  1. package com.android.test;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. public class Main extends Activity implements OnClickListener{  
  14.     private ClientSocket cs = null;  
  15.     private static final String FILE_PATH = "/mnt/sdcard/";  
  16.     private String ip = "192.168.1.103";  
  17.     private int port = 8821;  
  18.     private String sendMessage = "Linux";  
  19.     private Button mButton;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         mButton = (Button)findViewById(R.id.start);  
  25.         mButton.setOnClickListener(this);  
  26.     }  
  27.     private void start() {  
  28.         if (createConnection()) {  
  29.             sendMessage();  
  30.             getMessage();  
  31.         }  
  32.     }  
  33.     private void getMessage() {  
  34.         if (cs == null)  
  35.             return;  
  36.         DataInputStream inputStream = null;  
  37.         inputStream = cs.getMessageStream();  
  38.         try {  
  39.             String savePath = FILE_PATH;  
  40.             int bufferSize = 8192;  
  41.             byte[] buf = new byte[bufferSize];  
  42.             int passedlen = 0;  
  43.             long len = 0;  
  44.               
  45.             savePath += inputStream.readUTF();  
  46.             Log.d("AndroidClient","@@@savePath"+savePath);  
  47.             DataOutputStream fileOut = new DataOutputStream(  
  48.                     new BufferedOutputStream(new BufferedOutputStream(  
  49.                             new FileOutputStream(savePath))));  
  50.             len = inputStream.readLong();  
  51.             Log.d("AndoridClient","文件的长度为:"+len);  
  52.             Log.d("AndroidClient","开始接收文件");  
  53.             while(true) {  
  54.                 int read = 0;  
  55.                 if (inputStream != null) {  
  56.                     read = inputStream.read(buf);  
  57.                 }  
  58.                 passedlen += read;  
  59.                 if (read == -1) {  
  60.                     break;  
  61.                 }  
  62.                 Log.d("AndroidClient","文件接收了"+(passedlen*100/len)+"%/n");  
  63.                 fileOut.write(buf,0,read);  
  64.             }  
  65.             Log.d("AndroidClient","@@@文件接收完成"+savePath);  
  66.             fileOut.close();  
  67.         } catch (IOException e) {  
  68.             // TODO Auto-generated catch block  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.     private void sendMessage() {  
  73.         if (cs == null)  
  74.             return;  
  75.         cs.sendMessage(sendMessage);  
  76.     }  
  77.     private boolean createConnection() {  
  78.         cs = new ClientSocket(ip, port);  
  79.         cs.createConnection();  
  80.         Log.d("Main""连接服务器成功:");  
  81.         return true;  
  82.     }  
  83.     public void onClick(View v) {  
  84.         start();  
  85.     }  
  86. }  
[java] view plain copy
 print?
  1. package com.android.test;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. public class Main extends Activity implements OnClickListener{  
  14.     private ClientSocket cs = null;  
  15.     private static final String FILE_PATH = "/mnt/sdcard/";  
  16.     private String ip = "192.168.1.103";  
  17.     private int port = 8821;  
  18.     private String sendMessage = "Linux";  
  19.     private Button mButton;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         mButton = (Button)findViewById(R.id.start);  
  25.         mButton.setOnClickListener(this);  
  26.     }  
  27.     private void start() {  
  28.         if (createConnection()) {  
  29.             sendMessage();  
  30.             getMessage();  
  31.         }  
  32.     }  
  33.     private void getMessage() {  
  34.         if (cs == null)  
  35.             return;  
  36.         DataInputStream inputStream = null;  
  37.         inputStream = cs.getMessageStream();  
  38.         try {  
  39.             String savePath = FILE_PATH;  
  40.             int bufferSize = 8192;  
  41.             byte[] buf = new byte[bufferSize];  
  42.             int passedlen = 0;  
  43.             long len = 0;  
  44.               
  45.             savePath += inputStream.readUTF();  
  46.             Log.d("AndroidClient","@@@savePath"+savePath);  
  47.             DataOutputStream fileOut = new DataOutputStream(  
  48.                     new BufferedOutputStream(new BufferedOutputStream(  
  49.                             new FileOutputStream(savePath))));  
  50.             len = inputStream.readLong();  
  51.             Log.d("AndoridClient","文件的长度为:"+len);  
  52.             Log.d("AndroidClient","开始接收文件");  
  53.             while(true) {  
  54.                 int read = 0;  
  55.                 if (inputStream != null) {  
  56.                     read = inputStream.read(buf);  
  57.                 }  
  58.                 passedlen += read;  
  59.                 if (read == -1) {  
  60.                     break;  
  61.                 }  
  62.                 Log.d("AndroidClient","文件接收了"+(passedlen*100/len)+"%/n");  
  63.                 fileOut.write(buf,0,read);  
  64.             }  
  65.             Log.d("AndroidClient","@@@文件接收完成"+savePath);  
  66.             fileOut.close();  
  67.         } catch (IOException e) {  
  68.             // TODO Auto-generated catch block  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.     private void sendMessage() {  
  73.         if (cs == null)  
  74.             return;  
  75.         cs.sendMessage(sendMessage);  
  76.     }  
  77.     private boolean createConnection() {  
  78.         cs = new ClientSocket(ip, port);  
  79.         cs.createConnection();  
  80.         Log.d("Main""连接服务器成功:");  
  81.         return true;  
  82.     }  
  83.     public void onClick(View v) {  
  84.         start();  
  85.     }  


[java] view plaincopyprint?
  1. /* 
  2.  * PC与Android客户端实现文件的传送 
  3.  * PC服务器端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.IOException;  
  12. import java.net.ServerSocket;  
  13. import java.net.Socket;  
  14. public class TransferFileServer {  
  15.     private static final int HOST_PORT = 8821;  
  16.     private void start() {  
  17.         Socket s = null;  
  18.         try {  
  19.             ServerSocket ss = new ServerSocket(HOST_PORT);  
  20.             while (true) {  
  21.                 String filePath = "/home/fan/Pictures/1.jpg";  
  22.                 File file = new File(filePath);  
  23.                 System.out.println("文件长度:" + (int) file.length());  
  24.                 s = ss.accept();  
  25.                 log("建立Socket连接");  
  26.                 DataInputStream dis = new DataInputStream(  
  27.                         new BufferedInputStream(s.getInputStream()));  
  28.                 dis.readByte();  
  29.                 DataInputStream fis = new DataInputStream(  
  30.                         new BufferedInputStream(new FileInputStream(filePath)));  
  31.                 DataOutputStream dos = new DataOutputStream(s.getOutputStream());  
  32.                 dos.writeUTF(file.getName());  
  33.                 dos.flush();  
  34.                 dos.writeLong((long) file.length());  
  35.                 dos.flush();  
  36.                 int bufferSize = 8192;  
  37.                 byte[] buf = new byte[bufferSize];  
  38.                 while (true) {  
  39.                     int read = 0;  
  40.                     if (fis != null) {  
  41.                         read = fis.read(buf);  
  42.                     }  
  43.                     if (read == -1) {  
  44.                         break;  
  45.                     }  
  46.                     dos.write(buf,0,read);  
  47.                 }  
  48.                 dos.flush();  
  49.                  // 注意关闭socket链接哦,不然客户端会等待server的数据过来,  
  50.                 // 直到socket超时,导致数据不完整。  
  51.                 fis.close();  
  52.                 s.close();  
  53.                 log("文件传输完成");  
  54.             }  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.     void log(String msg) {  
  61.         System.out.println(msg);  
  62.     }  
  63.     public static void main(String args[]) {  
  64.         new TransferFileServer().start();  
  65.     }  
  66. }  
[java] view plain copy
 print?
  1. /* 
  2.  * PC与Android客户端实现文件的传送 
  3.  * PC服务器端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.IOException;  
  12. import java.net.ServerSocket;  
  13. import java.net.Socket;  
  14. public class TransferFileServer {  
  15.     private static final int HOST_PORT = 8821;  
  16.     private void start() {  
  17.         Socket s = null;  
  18.         try {  
  19.             ServerSocket ss = new ServerSocket(HOST_PORT);  
  20.             while (true) {  
  21.                 String filePath = "/home/fan/Pictures/1.jpg";  
  22.                 File file = new File(filePath);  
  23.                 System.out.println("文件长度:" + (int) file.length());  
  24.                 s = ss.accept();  
  25.                 log("建立Socket连接");  
  26.                 DataInputStream dis = new DataInputStream(  
  27.                         new BufferedInputStream(s.getInputStream()));  
  28.                 dis.readByte();  
  29.                 DataInputStream fis = new DataInputStream(  
  30.                         new BufferedInputStream(new FileInputStream(filePath)));  
  31.                 DataOutputStream dos = new DataOutputStream(s.getOutputStream());  
  32.                 dos.writeUTF(file.getName());  
  33.                 dos.flush();  
  34.                 dos.writeLong((long) file.length());  
  35.                 dos.flush();  
  36.                 int bufferSize = 8192;  
  37.                 byte[] buf = new byte[bufferSize];  
  38.                 while (true) {  
  39.                     int read = 0;  
  40.                     if (fis != null) {  
  41.                         read = fis.read(buf);  
  42.                     }  
  43.                     if (read == -1) {  
  44.                         break;  
  45.                     }  
  46.                     dos.write(buf,0,read);  
  47.                 }  
  48.                 dos.flush();  
  49.                  // 注意关闭socket链接哦,不然客户端会等待server的数据过来,  
  50.                 // 直到socket超时,导致数据不完整。  
  51.                 fis.close();  
  52.                 s.close();  
  53.                 log("文件传输完成");  
  54.             }  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.     void log(String msg) {  
  61.         System.out.println(msg);  
  62.     }  
  63.     public static void main(String args[]) {  
  64.         new TransferFileServer().start();  
  65.     }  
  66. }  
 

ClientSocket:辅助类

[java] view plaincopyprint?
  1. /* 
  2.  * PC与Android文件传输 
  3.  * Android客户端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.IOException;  
  10. import java.net.Socket;  
  11. import java.net.UnknownHostException;  
  12. public class ClientSocket {  
  13.     private String ip;  
  14.     private int port;  
  15.     private Socket socket = null;  
  16.     DataOutputStream out = null;  
  17.     DataInputStream getMessageStream = null;  
  18.     public ClientSocket(String ip, int port) {  
  19.         this.ip = ip;  
  20.         this.port = port;  
  21.     }  
  22.     public void createConnection() {  
  23.         try {  
  24.             socket = new Socket(ip, port);  
  25.         } catch (UnknownHostException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.             if (socket != null) {  
  29.                 try {  
  30.                     socket.close();  
  31.                 } catch (IOException e1) {  
  32.                     // TODO Auto-generated catch block  
  33.                     e1.printStackTrace();  
  34.                 }  
  35.             }  
  36.         } catch (IOException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.             if (socket != null) {  
  40.                 try {  
  41.                     socket.close();  
  42.                 } catch (IOException e1) {  
  43.                     // TODO Auto-generated catch block  
  44.                     e1.printStackTrace();  
  45.                 }  
  46.             }  
  47.         } finally {  
  48.         }  
  49.     }  
  50.     public void sendMessage(String sendMessage) {  
  51.         try {  
  52.             out = new DataOutputStream(socket.getOutputStream());  
  53.             if (sendMessage.equals("Windows")) {  
  54.                 out.writeByte(0x1);  
  55.                 out.flush();  
  56.                 return;  
  57.             }  
  58.             if (sendMessage.equals("Unix")) {  
  59.                 out.writeByte(0x2);  
  60.                 out.flush();  
  61.                 return;  
  62.             }  
  63.             if (sendMessage.equals("Linux")) {  
  64.                 out.writeByte(0x3);  
  65.                 out.flush();  
  66.             } else {  
  67.                 out.writeUTF(sendMessage);  
  68.                 out.flush();  
  69.             }  
  70.         } catch (IOException e) {  
  71.             // TODO Auto-generated catch block  
  72.             e.printStackTrace();  
  73.             if (out != null) {  
  74.                 try {  
  75.                     out.close();  
  76.                 } catch (IOException e1) {  
  77.                     // TODO Auto-generated catch block  
  78.                     e1.printStackTrace();  
  79.                 }  
  80.             }  
  81.         }  
  82.     }  
  83.     public DataInputStream getMessageStream() {  
  84.         try {  
  85.             getMessageStream = new DataInputStream(new BufferedInputStream(  
  86.                     socket.getInputStream()));  
  87.             // return getMessageStream;  
  88.         } catch (IOException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.             if (getMessageStream != null) {  
  92.                 try {  
  93.                     getMessageStream.close();  
  94.                 } catch (IOException e1) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e1.printStackTrace();  
  97.                 }  
  98.             }  
  99.         }  
  100.         return getMessageStream;  
  101.     }  
  102.     public void shutDownConnection() {  
  103.         try {  
  104.             if (out != null) {  
  105.                 out.close();  
  106.             }  
  107.             if (getMessageStream != null) {  
  108.                 getMessageStream.close();  
  109.             }  
  110.             if (socket != null) {  
  111.                 socket.close();  
  112.             }  
  113.         } catch (IOException e) {  
  114.             // TODO Auto-generated catch block  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118. }  
[java] view plain copy
 print?
  1. /* 
  2.  * PC与Android文件传输 
  3.  * Android客户端 
  4.  */  
  5. package com.android.test;  
  6. import java.io.BufferedInputStream;  
  7. import java.io.DataInputStream;  
  8. import java.io.DataOutputStream;  
  9. import java.io.IOException;  
  10. import java.net.Socket;  
  11. import java.net.UnknownHostException;  
  12. public class ClientSocket {  
  13.     private String ip;  
  14.     private int port;  
  15.     private Socket socket = null;  
  16.     DataOutputStream out = null;  
  17.     DataInputStream getMessageStream = null;  
  18.     public ClientSocket(String ip, int port) {  
  19.         this.ip = ip;  
  20.         this.port = port;  
  21.     }  
  22.     public void createConnection() {  
  23.         try {  
  24.             socket = new Socket(ip, port);  
  25.         } catch (UnknownHostException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.             if (socket != null) {  
  29.                 try {  
  30.                     socket.close();  
  31.                 } catch (IOException e1) {  
  32.                     // TODO Auto-generated catch block  
  33.                     e1.printStackTrace();  
  34.                 }  
  35.             }  
  36.         } catch (IOException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.             if (socket != null) {  
  40.                 try {  
  41.                     socket.close();  
  42.                 } catch (IOException e1) {  
  43.                     // TODO Auto-generated catch block  
  44.                     e1.printStackTrace();  
  45.                 }  
  46.             }  
  47.         } finally {  
  48.         }  
  49.     }  
  50.     public void sendMessage(String sendMessage) {  
  51.         try {  
  52.             out = new DataOutputStream(socket.getOutputStream());  
  53.             if (sendMessage.equals("Windows")) {  
  54.                 out.writeByte(0x1);  
  55.                 out.flush();  
  56.                 return;  
  57.             }  
  58.             if (sendMessage.equals("Unix")) {  
  59.                 out.writeByte(0x2);  
  60.                 out.flush();  
  61.                 return;  
  62.             }  
  63.             if (sendMessage.equals("Linux")) {  
  64.                 out.writeByte(0x3);  
  65.                 out.flush();  
  66.             } else {  
  67.                 out.writeUTF(sendMessage);  
  68.                 out.flush();  
  69.             }  
  70.         } catch (IOException e) {  
  71.             // TODO Auto-generated catch block  
  72.             e.printStackTrace();  
  73.             if (out != null) {  
  74.                 try {  
  75.                     out.close();  
  76.                 } catch (IOException e1) {  
  77.                     // TODO Auto-generated catch block  
  78.                     e1.printStackTrace();  
  79.                 }  
  80.             }  
  81.         }  
  82.     }  
  83.     public DataInputStream getMessageStream() {  
  84.         try {  
  85.             getMessageStream = new DataInputStream(new BufferedInputStream(  
  86.                     socket.getInputStream()));  
  87.             // return getMessageStream;  
  88.         } catch (IOException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.             if (getMessageStream != null) {  
  92.                 try {  
  93.                     getMessageStream.close();  
  94.                 } catch (IOException e1) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e1.printStackTrace();  
  97.                 }  
  98.             }  
  99.         }  
  100.         return getMessageStream;  
  101.     }  
  102.     public void shutDownConnection() {  
  103.         try {  
  104.             if (out != null) {  
  105.                 out.close();  
  106.             }  
  107.             if (getMessageStream != null) {  
  108.                 getMessageStream.close();  
  109.             }  
  110.             if (socket != null) {  
  111.                 socket.close();  
  112.             }  
  113.         } catch (IOException e) {  
  114.             // TODO Auto-generated catch block  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118. }  
 

Android客户端:Acitivyt实现:

[java] view plaincopyprint?
  1. package com.android.test;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. public class Main extends Activity implements OnClickListener{  
  14.     private ClientSocket cs = null;  
  15.     private static final String FILE_PATH = "/mnt/sdcard/";  
  16.     private String ip = "192.168.1.103";  
  17.     private int port = 8821;  
  18.     private String sendMessage = "Linux";  
  19.     private Button mButton;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         mButton = (Button)findViewById(R.id.start);  
  25.         mButton.setOnClickListener(this);  
  26.     }  
  27.     private void start() {  
  28.         if (createConnection()) {  
  29.             sendMessage();  
  30.             getMessage();  
  31.         }  
  32.     }  
  33.     private void getMessage() {  
  34.         if (cs == null)  
  35.             return;  
  36.         DataInputStream inputStream = null;  
  37.         inputStream = cs.getMessageStream();  
  38.         try {  
  39.             String savePath = FILE_PATH;  
  40.             int bufferSize = 8192;  
  41.             byte[] buf = new byte[bufferSize];  
  42.             int passedlen = 0;  
  43.             long len = 0;  
  44.               
  45.             savePath += inputStream.readUTF();  
  46.             Log.d("AndroidClient","@@@savePath"+savePath);  
  47.             DataOutputStream fileOut = new DataOutputStream(  
  48.                     new BufferedOutputStream(new BufferedOutputStream(  
  49.                             new FileOutputStream(savePath))));  
  50.             len = inputStream.readLong();  
  51.             Log.d("AndoridClient","文件的长度为:"+len);  
  52.             Log.d("AndroidClient","开始接收文件");  
  53.             while(true) {  
  54.                 int read = 0;  
  55.                 if (inputStream != null) {  
  56.                     read = inputStream.read(buf);  
  57.                 }  
  58.                 passedlen += read;  
  59.                 if (read == -1) {  
  60.                     break;  
  61.                 }  
  62.                 Log.d("AndroidClient","文件接收了"+(passedlen*100/len)+"%/n");  
  63.                 fileOut.write(buf,0,read);  
  64.             }  
  65.             Log.d("AndroidClient","@@@文件接收完成"+savePath);  
  66.             fileOut.close();  
  67.         } catch (IOException e) {  
  68.             // TODO Auto-generated catch block  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.     private void sendMessage() {  
  73.         if (cs == null)  
  74.             return;  
  75.         cs.sendMessage(sendMessage);  
  76.     }  
  77.     private boolean createConnection() {  
  78.         cs = new ClientSocket(ip, port);  
  79.         cs.createConnection();  
  80.         Log.d("Main""连接服务器成功:");  
  81.         return true;  
  82.     }  
  83.     public void onClick(View v) {  
  84.         start();  
  85.     }  
  86. }  
[java] view plain copy
 print?
  1. package com.android.test;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. public class Main extends Activity implements OnClickListener{  
  14.     private ClientSocket cs = null;  
  15.     private static final String FILE_PATH = "/mnt/sdcard/";  
  16.     private String ip = "192.168.1.103";  
  17.     private int port = 8821;  
  18.     private String sendMessage = "Linux";  
  19.     private Button mButton;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         mButton = (Button)findViewById(R.id.start);  
  25.         mButton.setOnClickListener(this);  
  26.     }  
  27.     private void start() {  
  28.         if (createConnection()) {  
  29.             sendMessage();  
  30.             getMessage();  
  31.         }  
  32.     }  
  33.     private void getMessage() {  
  34.         if (cs == null)  
  35.             return;  
  36.         DataInputStream inputStream = null;  
  37.         inputStream = cs.getMessageStream();  
  38.         try {  
  39.             String savePath = FILE_PATH;  
  40.             int bufferSize = 8192;  
  41.             byte[] buf = new byte[bufferSize];  
  42.             int passedlen = 0;  
  43.             long len = 0;  
  44.               
  45.             savePath += inputStream.readUTF();  
  46.             Log.d("AndroidClient","@@@savePath"+savePath);  
  47.             DataOutputStream fileOut = new DataOutputStream(  
  48.                     new BufferedOutputStream(new BufferedOutputStream(  
  49.                             new FileOutputStream(savePath))));  
  50.             len = inputStream.readLong();  
  51.             Log.d("AndoridClient","文件的长度为:"+len);  
  52.             Log.d("AndroidClient","开始接收文件");  
  53.             while(true) {  
  54.                 int read = 0;  
  55.                 if (inputStream != null) {  
  56.                     read = inputStream.read(buf);  
  57.                 }  
  58.                 passedlen += read;  
  59.                 if (read == -1) {  
  60.                     break;  
  61.                 }  
  62.                 Log.d("AndroidClient","文件接收了"+(passedlen*100/len)+"%/n");  
  63.                 fileOut.write(buf,0,read);  
  64.             }  
  65.             Log.d("AndroidClient","@@@文件接收完成"+savePath);  
  66.             fileOut.close();  
  67.         } catch (IOException e) {  
  68.             // TODO Auto-generated catch block  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.     private void sendMessage() {  
  73.         if (cs == null)  
  74.             return;  
  75.         cs.sendMessage(sendMessage);  
  76.     }  
  77.     private boolean createConnection() {  
  78.         cs = new ClientSocket(ip, port);  
  79.         cs.createConnection();  
  80.         Log.d("Main""连接服务器成功:");  
  81.         return true;  
  82.     }  
  83.     public void onClick(View v) {  
  84.         start();  
  85.     }  
0 0
原创粉丝点击