WIFI下零流量传文件(二)

凡事得要一步一步来,之前写了一个零流量传文字的,今天无聊完善了一下,现在可以传照片了。。其实添加个资源管理器现在理论上可以传所有文件了。

备注:REC是WIFI热点发送者


package com.example.reciver;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity  {
	private TextView text;
	private String t ;
	final private String uploadFile = Environment.getExternalStorageDirectory() + "/finger/myPic.jpg";
	
	private Handler mHandler = new Handler() {  
    	@Override
        public void handleMessage (Message msg) {//此方法在ui线程运行  
            switch(msg.what) {  
                case 1:
            	    text.setText(t);
            
            }  
        } 
    };
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (TextView)this.findViewById(R.id.text);
		
		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					ServerSocket ss = new ServerSocket(30000);
					while(true)
					{
						Socket s = ss.accept();
						/*
						BufferedReader br = new BufferedReader(
								new InputStreamReader(s.getInputStream()));
						String line = br.readLine();				
						t = line ;
						
						mHandler.obtainMessage(1).sendToTarget();
						
						br.close();
						*/
						InputStream fis = s.getInputStream();
						
						FileOutputStream fos = new FileOutputStream (uploadFile);
						int hasRead = 0;
						byte[] buf = new byte[1024];
						while ((hasRead = fis.read(buf))>0)
						{
							fos.write(buf ,0, hasRead);
						}
						
						fis.close();
						fos.close();
						
						s.close();
					}			
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}.start();
	}


}

上面是接收者的代码,下面是发送方代码

package com.example.sender;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView showIp;
	private TextView senderIp;
	private WifiInfo mWifiInfo;
	private WifiManager mWifiManager;
	private DhcpInfo dhcpinfo;  
	private Button sendMsg;
	private EditText setText;
	private String senIp;
	final private String uploadFile = Environment.getExternalStorageDirectory() + "/finger/myPic.jpg";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showIp = (TextView)this.findViewById(R.id.recIp);
		senderIp = (TextView)this.findViewById(R.id.senderIp);
		sendMsg = (Button)this.findViewById(R.id.sendMsg);
		setText = (EditText)this.findViewById(R.id.setText);
		
		sendMsg.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SocketSendMsg();
				
			}
		});
		
					
		Context context = getApplicationContext();//获取应用上下文 
		mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		mWifiInfo = mWifiManager.getConnectionInfo();
		dhcpinfo = mWifiManager.getDhcpInfo(); 
		
		String myIp = intToIp(getIPAddress());
		showIp.setText(myIp);
		
		senIp = intToIp(dhcpinfo.serverAddress);
		senderIp.setText(senIp);
	}
	
	public int getIPAddress() {
        return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
    }
	
	private String intToIp(int ip)
	{
	    return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "."+ ((ip >> 24) & 0xFF);
	}
	
	private void SocketSendMsg()
	{
		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					Socket socket = new Socket(String.valueOf(senIp) ,30000);  
					OutputStream os = socket.getOutputStream();
			        FileInputStream fis =new FileInputStream(uploadFile);				
					//os.write(setText.getText().toString().getBytes("utf-8"));
					
					byte[] buf = new byte[1024];
					int hasRead = 0;
					while ((hasRead = fis.read(buf)) > 0 )
					{
						os.write(buf ,0,hasRead);
					}
								
					os.close();
					fis.close();
					socket.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}.start();
	}


}

做得时候碰到一个问题,write如果用write(buf)的话会照成图片失真,不清楚是什么原因,一定要用到write(buf ,0 ,hasRead)才行。我研究下。


你可能感兴趣的:(安卓,wifi,NetWork,流量)