本文来自http://blog.csdn.net/hellogv/ ,引用.      

       最近半年都忙着Android TV项目,在春节假期才有时间写点东西。先在这里给大家拜个年,祝大家龙年快乐...

       直接进入主题:本文将会教大家如何实现一个简单的代理服务器(仅支持Http Get),与Android的MediaPlayer结合,从而可以扩展出“播放 防盗链的媒体文件”,“边播放边保存”等的功能。

       本文的代码可以到这里下载:http://download.csdn.net/detail/hellogv/4047134,代码分为两个工程:

  1. J2SE工程:HttpGetProxy,在PC上实现简单的代理服务器,核心类是HttpGetProxy.java,非常容易使用,这里就不唠叨了直接贴出运行效果图:Android MediaPlayer与Http Proxy结合之基础篇_Android
  2. Android工程:本文重点,必须唠叨一下。MediaPlayer播放网络音频(http://blog.csdn.net/hellogv/article/details/6406732)与HttpGetProxy.java结合,通过代理服务器播放网络音频。

   接下来贴出HttpGetProxy.java的原理图:

Android MediaPlayer与Http Proxy结合之基础篇_java_02

 接下来贴出HttpGetProxy.java的源码:

通过RemoteSocket的out_remoteSocket可以访问防盗链资源,HttpGetProxy通过2个线程来实现转发,可以在两个线程内实现保存的功能。

[java]  view plain copy print ?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">package com.musicplayer;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.InetAddress;  
  7. import java.net.InetSocketAddress;  
  8. import java.net.ServerSocket;  
  9. import java.net.Socket;  
  10. import java.net.SocketAddress;  
  11. import java.net.UnknownHostException;  
  12.   
  13. import android.util.Log;  
  14.   
  15. public class HttpGetProxy {  
  16.     final private String LOCAL_IP_ADDRESS = "127.0.0.1";  
  17.     final private int HTTP_PORT = 80;  
  18.   
  19.     private ServerSocket localServer = null;  
  20.     private Socket localSocket = null;  
  21.     private Socket remoteSocket = null;  
  22.     private String remoteIPAddress;  
  23.   
  24.     private InputStream in_remoteSocket;  
  25.     private OutputStream out_remoteSocket;  
  26.     private InputStream in_localSocket;  
  27.     private OutputStream out_localSocket;  
  28.       
  29.     private interface OnFinishListener {  
  30.         void onFinishListener();  
  31.     }  
  32.       
  33.     public HttpGetProxy(int localport) {  
  34.   
  35.         // --------建立代理中转服务器-----------//  
  36.         try {  
  37.             localServer = new ServerSocket(localport, 1,  
  38.                     InetAddress.getByName(LOCAL_IP_ADDRESS));  
  39.         } catch (UnknownHostException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         } catch (IOException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.   
  48.     /** 
  49.      * 结束时,清除所有资源 
  50.      */  
  51.     private OnFinishListener finishListener =new OnFinishListener(){  
  52.   
  53.         @Override  
  54.         public void onFinishListener() {  
  55.             System.out.println("..........release all..........");  
  56.             Log.e("---->","..........release all..........");  
  57.             try {  
  58.                 in_localSocket.close();  
  59.                 out_remoteSocket.close();  
  60.                   
  61.                 in_remoteSocket.close();  
  62.                 out_localSocket.close();  
  63.                   
  64.                 localSocket.close();  
  65.                 remoteSocket.close();  
  66.             } catch (IOException e) {  
  67.                 // TODO Auto-generated catch block  
  68.                 e.printStackTrace();  
  69.             }  
  70.         }  
  71.     };  
  72.       
  73.     public void startProxy(String remoteIpAddr) throws IOException {  
  74.         remoteIPAddress = remoteIpAddr;  
  75.         SocketAddress address = new InetSocketAddress(remoteIPAddress,HTTP_PORT);  
  76.   
  77.         // --------连接目标服务器---------//  
  78.         remoteSocket = new Socket();  
  79.         remoteSocket.connect(address);  
  80.         System.out.println("..........remote Server connected..........");  
  81.         Log.e("---->","..........remote Server connected..........");  
  82.         in_remoteSocket = remoteSocket.getInputStream();  
  83.         out_remoteSocket = remoteSocket.getOutputStream();  
  84.         System.out.println("..........init remote Server I/O..........");  
  85.         /** 
  86.          * 接收本地request,并转发到远程服务器 
  87.          */  
  88.         new Thread() {  
  89.             public void run() {  
  90.                 int bytes_read;  
  91.                 byte[] local_request = new byte[5120];  
  92.                 try {  
  93.                     // 本地Socket  
  94.                     localSocket = localServer.accept();  
  95.                     System.out.println("..........localSocket connected..........");  
  96.                     Log.e("---->","..........localSocket connected..........");  
  97.                     in_localSocket = localSocket.getInputStream();  
  98.                     out_localSocket = localSocket.getOutputStream();  
  99.   
  100.                     System.out.println("..........init local Socket I/O..........");  
  101.                     Log.e("---->","..........init local Socket I/O..........");  
  102.                     String buffer = "";  
  103.                     while ((bytes_read = in_localSocket.read(local_request)) != -1) {  
  104.                         String str = new String(local_request);  
  105.                         System.out.println("localSocket     " + str);  
  106.                         Log.e("localSocket---->",str);  
  107.                         buffer = buffer + str;  
  108.                         if (buffer.contains("GET")  
  109.                                 && buffer.contains("\r\n\r\n")) {  
  110.                             //---把request中的本地ip改为远程ip---//  
  111.                             buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteIPAddress);  
  112.                             System.out.println("已经替换IP");  
  113.                             out_remoteSocket.write(buffer.getBytes());  
  114.                             out_remoteSocket.flush();  
  115.                             continue;  
  116.                         } else{  
  117.                             out_remoteSocket.write(buffer.getBytes());  
  118.                             out_remoteSocket.flush();  
  119.                         }  
  120.                     }  
  121.                     System.out.println("..........local finish receive...........");  
  122.                     Log.e("---->","..........local finish receive..........");  
  123.                     finishListener.onFinishListener();  
  124.                 } catch (IOException e) {  
  125.                     // TODO Auto-generated catch block  
  126.                     e.printStackTrace();  
  127.                 }  
  128.             }  
  129.         }.start();  
  130.   
  131.         /** 
  132.          * 接收远程服务器reply,并转发到本地客户端 
  133.          */  
  134.         new Thread() {  
  135.             public void run() {  
  136.                 int bytes_read;  
  137.                 byte[] remote_reply = new byte[5120];  
  138.                 try {  
  139.                     System.out.println("..........remote start to receive...........");  
  140.                     Log.e("---->","..........remote start to receive..........");  
  141.                     while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) {  
  142.                           
  143.                         //System.out.println("remoteSocket     " + remote_reply.length);  
  144.                         //System.out.println("remoteSocket     " + new String(remote_reply));  
  145.                         out_localSocket.write(remote_reply, 0, bytes_read);  
  146.                         out_localSocket.flush();  
  147.                     }  
  148.                     System.out.println("..........remote finish receive...........");  
  149.                     Log.e("---->","..........remote finish receive..........");  
  150.                 } catch (IOException e) {  
  151.                     // TODO Auto-generated catch block  
  152.                     e.printStackTrace();  
  153.                 }  
  154.             }  
  155.         }.start();  
  156.     }  
  157. }  
  158. </span>