Попытка показа видео через proxy
This commit is contained in:
64
src/main/java/kinosearch/webapp/servlets/ProxyServlet.java
Normal file
64
src/main/java/kinosearch/webapp/servlets/ProxyServlet.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package kinosearch.webapp.servlets;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.Part;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016
|
||||
*/
|
||||
public class ProxyServlet extends HttpServlet {
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String path = request.getPathInfo().substring("/onlinelife/".length());
|
||||
|
||||
URL url = new URL("http://" + path);
|
||||
HttpURLConnection con =(HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(true);
|
||||
|
||||
for (Enumeration names = request.getHeaderNames(); names.hasMoreElements();) {
|
||||
String headerName = names.nextElement().toString();
|
||||
if (headerName.equalsIgnoreCase("referer")) continue;
|
||||
con.setRequestProperty(headerName, request.getHeader(headerName));
|
||||
}
|
||||
|
||||
con.connect();
|
||||
|
||||
int statusCode = con.getResponseCode();
|
||||
response.setStatus(statusCode);
|
||||
|
||||
for (Map.Entry<String, List<String>> stringListEntry : con.getHeaderFields().entrySet()) {
|
||||
Map.Entry mapEntry = (Map.Entry) stringListEntry;
|
||||
if (mapEntry.getKey() != null) {
|
||||
response.setHeader(mapEntry.getKey().toString(), ((List) mapEntry.getValue()).get(0).toString());
|
||||
}
|
||||
}
|
||||
|
||||
BufferedInputStream webToProxyBuf = new BufferedInputStream(con.getInputStream());
|
||||
BufferedOutputStream proxyToClientBuf = new BufferedOutputStream(response.getOutputStream());
|
||||
|
||||
int oneByte;
|
||||
while ((oneByte = webToProxyBuf.read()) != -1) {
|
||||
proxyToClientBuf.write(oneByte);
|
||||
}
|
||||
|
||||
proxyToClientBuf.flush();
|
||||
proxyToClientBuf.close();
|
||||
webToProxyBuf.close();
|
||||
con.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user