package com.gatgets.http.webservice;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* socket客户端(下载网页及元素,暂不支持断点传续)
* @author 王正镇
* @date 2011-8-12
*/
public class MyClient {
private static Pattern p = null;
static {
p = Pattern.compile("http://(.+?):?(\\d+)?(/.*)?");
}
public static void getResource(String url) throws Exception {
int port; // 端口号
String host; // 主机
String resourcePath; // 请求路径
Matcher m = p.matcher(url);
if (m.matches()) {
host = m.group(1);
port = m.group(2) == null ? 80 : Integer.parseInt(m.group(2)); // 默认端口 80
resourcePath = m.group(3) == null ? "/" : m.group();
} else {
throw new Exception("请输入合法的连接地址");
}
Socket socket = new Socket(host, port);
// 使用socket get连接
OutputStream os = socket.getOutputStream();
// 请求消息头
String lineSeparator = System.getProperty("line.separator");
os.write(("GET " + resourcePath + " HTTP/1.1" + lineSeparator).getBytes());
os.write(("Host: " + host + lineSeparator).getBytes());
os.write(lineSeparator.getBytes());
// 获得返回结果
InputStream is = socket.getInputStream();
// 读取流(必须用二进制读取)
// 读取头消息(跳过)
BinaryInputStream bis = new BinaryInputStream(is);
String tmp = "";
while (!(tmp = bis.readLine()).equals("")) {
System.out.println(tmp);
}
bis.close();
// 保存(以下所有内容)
String filePath = ClassLoader.getSystemResource("").toString().substring(5); // 文件路径
String filename = filePath + UUID.randomUUID().toString(); // 产生一个随机文件名
// 如果有扩展名,则加上扩展名
int indexDoc = -1;
if ((indexDoc = resourcePath.lastIndexOf(".")) != -1) {
filename += resourcePath.substring(indexDoc);
}
OutputStream filenameOS = new FileOutputStream(filename);
copyStream(is, filenameOS);
System.out.println("----------------------" + filename + "------------------------");
os.close();
is.close();
filenameOS.close();
}
/**
* 复制流
* @throws IOException
*/
public static void copyStream(InputStream is, OutputStream os) {
byte[] b = new byte[2048]; // 缓存
int length = 0;
try {
while ((length = is.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行入口
* @param args
*/
public static void main(String[] args) {
try {
getResource("http://www.baidu.com:80");
getResource("http://www.google.com.hk/intl/zh-CN/images/logo_cn.png");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 不同系统分隔符不相同
* @author 王正镇
* @date 2011-8-12
*/
class BinaryInputStream {
private enum SYSTYPE{LINUX, WINDOWS};
private ByteArrayOutputStream bos = null;
private InputStream is = null;
private SYSTYPE type = null; // 系统类型
public BinaryInputStream(InputStream is) {
this.is = is;
this.bos = new ByteArrayOutputStream();
this.type = System.getProperty("line.separator").length() > 1 ? SYSTYPE.WINDOWS : SYSTYPE.LINUX;
}
/**
* 读取一行
* @return
* @throws IOException
*/
public String readLine() throws IOException {
String str = ""; // 返回结果
int b = -1;
while (is != null && (b = is.read()) != -1) {
// windows下换行符
if (b == '\r' && type == SYSTYPE.WINDOWS) {
if (is.read() == '\n') {
str = bos.toString();
bos.reset(); // 重置
break;
}
}
// linux下换行符
if (b == '\n' && type == SYSTYPE.LINUX) {
str = bos.toString();
bos.reset(); // 重置
break;
}
bos.write(b);
}
return str.trim();
}
public void close() throws IOException {
if (bos != null) {
bos.close();
}
}
}
分享到:
相关推荐
web_socket_channel包为WebSocket连接提供包装器。 它提供了一个跨平台的 API,该API的跨平台实现(可在基础上进行通信), 封装dart:io的WebSocket类的以及封装dart:html的实现。 它还在为WebSocket协议的预定义...
实用html5的web socket的java版实时聊天室 代码完全
软件实现了5500网页配置,可以参考代码进一步开发
使用java socket模拟一个在线的聊天室功能,达到小环境的即时通讯。
Springboot整合Websocket,并通过Websocket实现服务器端向客户端主动推送消息
ws,node.js,websocket
Java实现Socket创建WEB服务器代码WEB server to create Java code to achieve Socket
web_socket django使用龙卷风websocket 参见
简单的web实现,在我以前的TCP修改的
web_socket使用webSocket搭建后台消息推送服务,消息确认,消息日志,内网下app消息推送#svg顺便有个svg文件动态缩放功能,用到了jquery.svg.pan.zoom.js具体操作左键双击可以放大,滚轮也可以操作参考 ...
web_socket 一个新的Flutter应用程序。入门该项目是Flutter应用程序的起点。 如果这是您的第一个Flutter项目,那么有一些资源可以帮助您入门:要获得Flutter入门方面的帮助,请查看我们的,其中提供了教程,示例,...
用java实现了远程控制 功能齐全,比如服务器端屏幕会在客户端显示以及鼠标操作,键盘操作,各种快捷键都涵盖,性能优化,在局域网中速度很快
该存储库是我在Compute Network中的Uniiv项目。
通过websocket线程实时显示数据库中的更新数据,并将数据在所标注的地图中显示出来
在java与flash之间,利用socket传输swf文件,显示在flash play中。这样做可以防止IE缓存保存swf文件。
套接字聊天! 描述 一个非常简单的聊天应用程序。... 从控制台启动应用程序: node socket.js 在浏览器中访问本地测试端口: localhost:1337 贡献 该项目使用 MIT 许可。 随意使用,但你应该完全相信你的来源!
在PXA270开发板上实现WEB服务器。 在主程序中建立TCP类型SOCKET在80端口进行监听连接请求。
使用web形式实现语音识别
web-socket-js亲测可用
Websocket-Arduino, a websocket implementation for Arduino ... Based on previous implementations by ...and ...and ... Some code and concept based off of Webduino library ...Permission is hereby granted, free of ...