资源简介
JAVA实现http服务端和客户端
服务器功能 提供HTML,JPG 等MIME 类型的资源
客户端功能 访问服务器,获取HTML 和JPG 资源,保存到本地
磁盘
访问服务器,获取WMV 等其他资源类型,保存到
本地磁盘
代码片段和文件信息
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Created by Lawrence on 3/14/16.
*/
public class Client {
public static void main(String[] args) {
Socket client;
Scanner scanner = new Scanner(System.in);
try {
String fileName;
String host;
int port;
System.out.println(“Please input HTTP link:“);
host = scanner.nextLine();
StringTokenizer address = new StringTokenizer(host “/“);
host = address.nextToken();
System.out.println(“Please input Port:“);
port = scanner.nextInt();
client = new Socket(host port);
PrintStream writer = new PrintStream(client.getOutputStream());
fileName = address.hasMoreTokens() ? address.nextToken() : “index.html“;
writer.println(“GET /“ + fileName + “ HTTP/1.1“);
writer.println(“Host:“ + host);
writer.println(“Connection:keep-alive“);
writer.println();
writer.flush();
DataInputStream in = new DataInputStream(client.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String firstLineOfResponse = reader.readLine();
String secondLineOfResponse = reader.readLine();
String thirdLineOfResponse = reader.readLine();
String fourthLineOfResponse = reader.readLine();
if(firstLineOfResponse.split(“ “)[1].equals(“200“)) {
File file = new File(“./get/“ + fileName);
if(!file.exists()) {
while(!file.createNewFile()) {
System.out.println(“Failed to create file “ + fileName);
}
} else {
while(!file.setLastModified(System.currentTimeMillis())) {
System.out.println(“Failed to edit file “ + fileName);
}
}
byte[] buffer = new byte[1024];
OutputStream fout = new FileOutputStream(file);
int length = in.read(buffer);
System.out.println(length);
while(length != -1) {
// for(byte i: buffer) {
// System.out.print(i);
// }
fout.write(buffer 0 length);
length = in.read(buffer);
}
in.close();
fout.close();
} else {
System.out.println(fourthLineOfResponse);
}
reader.close();
writer.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....... 24 2017-03-17 16:58 HTTPServer-master\.gitignore
....... 2865 2017-03-17 16:58 HTTPServer-master\src\Client.java
....... 515 2017-03-17 16:58 HTTPServer-master\src\HTTPServer.java
....... 2841 2017-03-17 16:58 HTTPServer-master\src\ServerThread.java
....... 3058 2017-03-17 16:58 HTTPServer-master\src\TestClient.java
....... 3239 2017-03-17 16:58 HTTPServer-master\src\TestServer.java
目录 0 2018-01-17 10:33 HTTPServer-master\src
目录 0 2017-03-17 16:58 HTTPServer-master
----------- --------- ---------- ----- ----
12542 8
相关资源
- okhttp3.8.0-jdk1.6.zip
- httpcore-4.0.jar
- Http器 FTP器java
- HttpClient for android 4 3 5 jar
- Http请求Restful风格的远程调用工具类
- HttpUtil工具类支持POSTGETHTTPHTTPS
- okhttp-2.4.0和okio-1.4.0库
- org.apache.http.legacy.jar -sdk 23
- Android电子拍卖系统
- Android Http实现文件的上传和
- httpmime-4.1.2.jar
- Java 调用Http Rest接口 说明2
- http客户端java程序
- HttpClient jar包
- httpcore-4.1.2.jar
- Java 解析http请求处理结果
- httpclient-4.3.4.jar
- httpclient-4.3.6.jar
- org.apache.http.entity.mime
- Android使用OkHttp访问自签名证书的接口
- java访问https网址文件(含证书)
- HttpServletRequest源码 HttpServletResponse源码
- okHttp-3.11.0
- ApacheJMeter_http.jar60720
- OKHttp3 所有Jarb包
- http接口需要的jar包
- jstl.jar和standard.jar 报错Can not find \“
- java访问https网址文件
- httpclient get/post请求工具类map参数封装
- HttpClient4.5 实现https忽略SSL证书验证
评论
共有 条评论