• 大小: 5KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-23
  • 语言: Java
  • 标签: HTTP  

资源简介

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


评论

共有 条评论