• 大小: 108KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-11
  • 语言: Java
  • 标签: MediaPlayer  Http  Get  Proxy  

资源简介

如何实现一个简单的代理服务器(仅支持Http Get),与Android的MediaPlayer结合,从而可以扩展出“播放 反盗链的媒体文件”,“边播放边保存”的功能。

资源截图

代码片段和文件信息


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

public class HttpGetProxy {
final private String LOCAL_IP_ADDRESS = “127.0.0.1“;
final private int HTTP_PORT = 80;

private ServerSocket localServer = null;
private Socket localSocket = null;
private Socket remoteSocket = null;
private String remoteIPAddress;

private InputStream in_remoteSocket;
private OutputStream out_remoteSocket;
private InputStream in_localSocket;
private OutputStream out_localSocket;

private interface onfinishListener {
void onfinishListener();
}

public HttpGetProxy(int localport) {

// --------建立代理中转服务器-----------//
try {
localServer = new ServerSocket(localport 1
InetAddress.getByName(LOCAL_IP_ADDRESS));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
 * 结束时,清除所有资源
 */
private onfinishListener finishListener =new onfinishListener(){

@Override
public void onfinishListener() {
System.out.println(“..........release all..........“);
//Log.e(“---->““..........release all..........“);
try {
in_localSocket.close();
out_remoteSocket.close();

in_remoteSocket.close();
out_localSocket.close();

localSocket.close();
remoteSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};

public void startProxy(String remoteIpAddr) throws IOException {
remoteIPAddress = remoteIpAddr;
SocketAddress address = new InetSocketAddress(remoteIPAddressHTTP_PORT);

// --------连接目标服务器---------//
remoteSocket = new Socket();
remoteSocket.connect(address);
System.out.println(“..........remote Server connected..........“);
//Log.e(“---->““..........remote Server connected..........“);
in_remoteSocket = remoteSocket.getInputStream();
out_remoteSocket = remoteSocket.getOutputStream();
System.out.println(“..........init remote Server I/O..........“);
/**
 * 接收本地request,并转发到远程服务器
 */
new Thread() {
public void run() {
int bytes_read;
byte[] local_request = new byte[5120];
try {
// 本地Socket
localSocket = localServer.accept();
System.out.println(“..........localSocket connected..........“);
// Log.e(“---->““..........localSocket connected..........“);
in_localSocket = localSocket.getInputStream();
out_localSocket = localSocket.getOutputStream();

System.out.println(“..........init local Socket I/O..........“);
// Log.e(“---->““..........local start to receive..........“);
String buffer = ““;
while ((byt

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-02-06 10:24  HttpGetProxy\
     目录           0  2012-02-06 10:27  HttpGetProxy\HttpGetProxy\
     文件         232  2012-02-06 10:26  HttpGetProxy\HttpGetProxy\.classpath
     文件         388  2012-02-06 10:26  HttpGetProxy\HttpGetProxy\.project
     目录           0  2012-02-06 10:50  HttpGetProxy\HttpGetProxy\.settings\
     文件          88  2012-02-06 10:50  HttpGetProxy\HttpGetProxy\.settings\org.eclipse.core.resources.prefs
     文件         629  2012-02-06 10:27  HttpGetProxy\HttpGetProxy\.settings\org.eclipse.jdt.core.prefs
     目录           0  2012-02-06 11:50  HttpGetProxy\HttpGetProxy\bin\
     文件        1308  2012-02-06 11:45  HttpGetProxy\HttpGetProxy\bin\HttpGetProxy$1.class
     文件        2897  2012-02-06 11:45  HttpGetProxy\HttpGetProxy\bin\HttpGetProxy$2.class
     文件        1298  2012-02-06 11:45  HttpGetProxy\HttpGetProxy\bin\HttpGetProxy$3.class
     文件         223  2012-02-06 11:45  HttpGetProxy\HttpGetProxy\bin\HttpGetProxy$onfinishListener.class
     文件        3535  2012-02-06 11:45  HttpGetProxy\HttpGetProxy\bin\HttpGetProxy.class
     文件         728  2012-02-06 11:50  HttpGetProxy\HttpGetProxy\bin\testHttpGetProxyForJ2SE.class
     目录           0  2012-02-06 11:59  HttpGetProxy\HttpGetProxy\src\
     文件        5023  2012-02-06 11:26  HttpGetProxy\HttpGetProxy\src\HttpGetProxy.java
     文件         582  2012-02-06 11:59  HttpGetProxy\HttpGetProxy\src\testHttpGetProxyForJ2SE.java
     目录           0  2012-02-06 13:17  HttpGetProxy\musicplayer_demo\
     文件         288  2012-02-06 10:38  HttpGetProxy\musicplayer_demo\.classpath
     文件         847  2011-05-09 09:15  HttpGetProxy\musicplayer_demo\.project
     目录           0  2012-02-06 09:02  HttpGetProxy\musicplayer_demo\.settings\
     文件         170  2012-02-06 09:02  HttpGetProxy\musicplayer_demo\.settings\org.eclipse.core.resources.prefs
     文件         629  2011-05-09 09:29  HttpGetProxy\musicplayer_demo\.settings\org.eclipse.jdt.core.prefs
     文件         756  2011-05-09 13:47  HttpGetProxy\musicplayer_demo\AndroidManifest.xml
     目录           0  2011-05-09 09:14  HttpGetProxy\musicplayer_demo\assets\
     目录           0  2012-02-06 09:02  HttpGetProxy\musicplayer_demo\bin\
     目录           0  2012-02-06 13:17  HttpGetProxy\musicplayer_demo\bin\classes\
     文件       13436  2012-02-06 09:02  HttpGetProxy\musicplayer_demo\bin\classes.dex
     文件       13032  2012-02-06 13:17  HttpGetProxy\musicplayer_demo\bin\classes\classes.dex
     目录           0  2012-02-06 13:17  HttpGetProxy\musicplayer_demo\bin\classes\com\
     目录           0  2012-02-06 13:17  HttpGetProxy\musicplayer_demo\bin\classes\com\musicplayer\
............此处省略64个文件信息

评论

共有 条评论