socket傳文件

用socket,安卓客戶端向pc服務器端傳文件。

一開始傳出的文件有多出來的字節,後來通過局部修改後沒有任何問題了。

安卓客戶端代碼:

package com.example.androidclient2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;

/**
 * 對傳文件客戶端的測試
 * @author nm 2014-12-10
 */
public class ClientActivity extends Activity {
	private Socket socket;
	private OutputStream streamOut;
	private InputStream streamIn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		socketPhone();
	}

	private void socketPhone() {
		new Thread(){
			@Override
			public void run() {
				super.run();
				try {
					socket = new Socket("192.168.0.90", 10000);
					streamIn = socket.getInputStream();
					streamOut = socket.getOutputStream();
					interaction();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	private void interaction() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					while (true) {
						String path = "/mnt/sdcard/abc.mp3";
						FileInputStream fileIn = new FileInputStream(path);
						
						// 這個循環有限發送
						int a = 0,b = 0;
						while (a != -1) {
							byte[] fileByte = new byte[1024];
							a = (fileIn.read(fileByte, 0, 1024));
							b += a;
							System.out.println("a==================" + a+  " ,b="+b);
							if(a > 0){
								System.out.println("a---------------------------------" + a);
								streamOut.write(fileByte, 0, a);
								streamOut.flush();
							}
						}
						fileIn.close();
						break;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
pc服務器端的代碼如下:

import java.io.FileOutputStream;
import java.io.IOException;  
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;  
import java.net.Socket;  
  
public class MyServer {  
	private static ServerSocket server;
	private static Socket socket;
	private static OutputStream streamOut;
	private static InputStream streamIn;
	
    public static void main(String[] args) throws IOException {
        server = new ServerSocket(10000);
        acceptClient();
    }
    
    private static void acceptClient() throws IOException {
        new Thread(new Runnable() {  
            public void run() { 
            	while (true) {
	                try {
	                	socket = server.accept();  //Socket[addr=/192.168.0.90,port=53418,localport=10000]
	                	streamIn = socket.getInputStream();
	            		streamOut = socket.getOutputStream();
	            		invoke();
	                } catch(Exception ex) {  
	                    ex.printStackTrace();  
	                }
            	}
            }  
        }).start();  
    }

    private static FileOutputStream file;
	private static String savePath = "E:/test/nmsong.mp3";
	protected static void invoke() {
		new Thread(){
			@Override
			public void run() {
				super.run();
				try {
					int index =0;
					file = new FileOutputStream(savePath, false);
					while(true){
						int size = streamIn.available();
						index++;
						//下面先放到文件裏面試試能不能成功,結果不能成功。
						byte[] buffer = new byte[1024];
						int buffer1 = streamIn.read(buffer);
						System.out.println("index = "+index + " size=" + size + ", buffer1=" + buffer1);
						file.write(buffer, 0 ,buffer1);//後面的兩個參數是必須的,否則字節不足默認bugger的長度,則補0。
					}
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}
			}
		}.start();
	}
}
完。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章