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

PC服务器端代码:


  1. /*
  2. *PC与Android客户端实现文件的传送
  3. *PC服务器端
  4. */
  5. packagecom.android.test;
  6. importjava.io.BufferedInputStream;
  7. importjava.io.DataInputStream;
  8. importjava.io.DataOutputStream;
  9. importjava.io.File;
  10. importjava.io.FileInputStream;
  11. importjava.io.IOException;
  12. importjava.net.ServerSocket;
  13. importjava.net.Socket;
  14. publicclassTransferFileServer{
  15. privatestaticfinalintHOST_PORT=8821;
  16. privatevoidstart(){
  17. Sockets=null;
  18. try{
  19. ServerSocketss=newServerSocket(HOST_PORT);
  20. while(true){
  21. StringfilePath="/home/fan/Pictures/1.jpg";
  22. Filefile=newFile(filePath);
  23. System.out.println("文件长度:"+(int)file.length());
  24. s=ss.accept();
  25. log("建立Socket连接");
  26. DataInputStreamdis=newDataInputStream(
  27. newBufferedInputStream(s.getInputStream()));
  28. dis.readByte();
  29. DataInputStreamfis=newDataInputStream(
  30. newBufferedInputStream(newFileInputStream(filePath)));
  31. DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
  32. dos.writeUTF(file.getName());
  33. dos.flush();
  34. dos.writeLong((long)file.length());
  35. dos.flush();
  36. intbufferSize=8192;
  37. byte[]buf=newbyte[bufferSize];
  38. while(true){
  39. intread=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(IOExceptione){
  56. //TODOAuto-generatedcatchblock
  57. e.printStackTrace();
  58. }
  59. }
  60. voidlog(Stringmsg){
  61. System.out.println(msg);
  62. }
  63. publicstaticvoidmain(Stringargs[]){
  64. newTransferFileServer().start();
  65. }
  66. }

ClientSocket:辅助类

  1. /*
  2. *PC与Android文件传输
  3. *Android客户端
  4. */
  5. packagecom.android.test;
  6. importjava.io.BufferedInputStream;
  7. importjava.io.DataInputStream;
  8. importjava.io.DataOutputStream;
  9. importjava.io.IOException;
  10. importjava.net.Socket;
  11. importjava.net.UnknownHostException;
  12. publicclassClientSocket{
  13. privateStringip;
  14. privateintport;
  15. privateSocketsocket=null;
  16. DataOutputStreamout=null;
  17. DataInputStreamgetMessageStream=null;
  18. publicClientSocket(Stringip,intport){
  19. this.ip=ip;
  20. this.port=port;
  21. }
  22. publicvoidcreateConnection(){
  23. try{
  24. socket=newSocket(ip,port);
  25. }catch(UnknownHostExceptione){
  26. //TODOAuto-generatedcatchblock
  27. e.printStackTrace();
  28. if(socket!=null){
  29. try{
  30. socket.close();
  31. }catch(IOExceptione1){
  32. //TODOAuto-generatedcatchblock
  33. e1.printStackTrace();
  34. }
  35. }
  36. }catch(IOExceptione){
  37. //TODOAuto-generatedcatchblock
  38. e.printStackTrace();
  39. if(socket!=null){
  40. try{
  41. socket.close();
  42. }catch(IOExceptione1){
  43. //TODOAuto-generatedcatchblock
  44. e1.printStackTrace();
  45. }
  46. }
  47. }finally{
  48. }
  49. }
  50. publicvoidsendMessage(StringsendMessage){
  51. try{
  52. out=newDataOutputStream(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(IOExceptione){
  71. //TODOAuto-generatedcatchblock
  72. e.printStackTrace();
  73. if(out!=null){
  74. try{
  75. out.close();
  76. }catch(IOExceptione1){
  77. //TODOAuto-generatedcatchblock
  78. e1.printStackTrace();
  79. }
  80. }
  81. }
  82. }
  83. publicDataInputStreamgetMessageStream(){
  84. try{
  85. getMessageStream=newDataInputStream(newBufferedInputStream(
  86. socket.getInputStream()));
  87. //returngetMessageStream;
  88. }catch(IOExceptione){
  89. //TODOAuto-generatedcatchblock
  90. e.printStackTrace();
  91. if(getMessageStream!=null){
  92. try{
  93. getMessageStream.close();
  94. }catch(IOExceptione1){
  95. //TODOAuto-generatedcatchblock
  96. e1.printStackTrace();
  97. }
  98. }
  99. }
  100. returngetMessageStream;
  101. }
  102. publicvoidshutDownConnection(){
  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(IOExceptione){
  114. //TODOAuto-generatedcatchblock
  115. e.printStackTrace();
  116. }
  117. }
  118. }

Android客户端:Acitivyt实现:

  1. packagecom.android.test;
  2. importjava.io.BufferedOutputStream;
  3. importjava.io.DataInputStream;
  4. importjava.io.DataOutputStream;
  5. importjava.io.FileOutputStream;
  6. importjava.io.IOException;
  7. importandroid.app.Activity;
  8. importandroid.os.Bundle;
  9. importandroid.util.Log;
  10. importandroid.view.View;
  11. importandroid.view.View.OnClickListener;
  12. importandroid.widget.Button;
  13. publicclassMainextendsActivityimplementsOnClickListener{
  14. privateClientSocketcs=null;
  15. privatestaticfinalStringFILE_PATH="/mnt/sdcard/";
  16. privateStringip="192.168.1.103";
  17. privateintport=8821;
  18. privateStringsendMessage="Linux";
  19. privateButtonmButton;
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. mButton=(Button)findViewById(R.id.start);
  25. mButton.setOnClickListener(this);
  26. }
  27. privatevoidstart(){
  28. if(createConnection()){
  29. sendMessage();
  30. getMessage();
  31. }
  32. }
  33. privatevoidgetMessage(){
  34. if(cs==null)
  35. return;
  36. DataInputStreaminputStream=null;
  37. inputStream=cs.getMessageStream();
  38. try{
  39. StringsavePath=FILE_PATH;
  40. intbufferSize=8192;
  41. byte[]buf=newbyte[bufferSize];
  42. intpassedlen=0;
  43. longlen=0;
  44. savePath+=inputStream.readUTF();
  45. Log.d("AndroidClient","@@@savePath"+savePath);
  46. DataOutputStreamfileOut=newDataOutputStream(
  47. newBufferedOutputStream(newBufferedOutputStream(
  48. newFileOutputStream(savePath))));
  49. len=inputStream.readLong();
  50. Log.d("AndoridClient","文件的长度为:"+len);
  51. Log.d("AndroidClient","开始接收文件");
  52. while(true){
  53. intread=0;
  54. if(inputStream!=null){
  55. read=inputStream.read(buf);
  56. }
  57. passedlen+=read;
  58. if(read==-1){
  59. break;
  60. }
  61. Log.d("AndroidClient","文件接收了"+(passedlen*100/len)+"%/n");
  62. fileOut.write(buf,0,read);
  63. }
  64. Log.d("AndroidClient","@@@文件接收完成"+savePath);
  65. fileOut.close();
  66. }catch(IOExceptione){
  67. //TODOAuto-generatedcatchblock
  68. e.printStackTrace();
  69. }
  70. }
  71. privatevoidsendMessage(){
  72. if(cs==null)
  73. return;
  74. cs.sendMessage(sendMessage);
  75. }
  76. privatebooleancreateConnection(){
  77. cs=newClientSocket(ip,port);
  78. cs.createConnection();
  79. Log.d("Main","连接服务器成功:");
  80. returntrue;
  81. }
  82. publicvoidonClick(Viewv){
  83. start();
  84. }
  85. }



你可能感兴趣的:(socket)