欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

有两台电脑,我想把一台电脑上的文件拷到另一台上,当中的传输协议要基于HTTP的,谁能给点提示或者示例  

程序员文章站 2022-07-13 09:37:58
...
httpServer + httpClient 下面是个简单的示例

在这个基础上改改。

Java code

package com.fzr.learn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoService {

    private int port = 8999;

    private ServerSocket serverSocket;

    public EchoService() throws IOException {

        serverSocket = new ServerSocket(port);// 创建tcp服务对象
        System.out.println("服务启动");

    }

    public String echo(String message) {
        return "echo : " + message;
    }

    private PrintWriter getWriter(Socket socket) throws IOException {
        OutputStream socketOut = socket.getOutputStream();
        return new PrintWriter(socketOut, true);
    }

    private BufferedReader getReader(Socket socket) throws IOException {
        InputStream socketIn = socket.getInputStream();
        return new BufferedReader(new InputStreamReader(socketIn));
    }

    public void service() {
        while (true) {
            Socket socket = null;
            System.out.println("11");
            try {
                socket = serverSocket.accept();
                System.out.println("new connection accepted "
                        + socket.getInetAddress() + ":" + socket.getPort());
                System.out.println(socket);
                BufferedReader br = getReader(socket);
                PrintWriter pw = getWriter(socket);
                String message = null;
                System.out.println("22");
                while((message=br.readLine())!=null){
                    System.out.println("33");
                    System.out.println(message);
                    pw.println(echo(message));
                    if(message.equals("bye")){
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                    try {
                        if(socket!=null){
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

        }
    }
    
    public static void main(String[] args) throws IOException {
        new EchoService().service();
        System.out.println("aa");
    }
    
}


Java code
package com.fzr.learn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {

    private String host = "localhost";
    private int port = 8999;
    
    private Socket socket ;
    
    public EchoClient () throws UnknownHostException, IOException{
        socket = new Socket (host,port);
        
    }
    
    private PrintWriter getWriter(Socket socket) throws IOException {
        OutputStream socketOut = socket.getOutputStream();
        return new PrintWriter(socketOut, true);
    }
    
    private BufferedReader getReader(Socket socket) throws IOException {
        InputStream socketIn = socket.getInputStream();
        return new BufferedReader(new InputStreamReader(socketIn));
    }
    
    public void talk(){
        try {
            BufferedReader br = getReader(socket);
            PrintWriter pw = getWriter(socket);
            BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
            String message =null ;
            while((message=localReader.readLine())!=null){
                pw.println(message);
                System.out.println(br.readLine());
                if(message.equals("bye")){
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
    
    public static void main(String[] args) throws UnknownHostException, IOException {
        new EchoClient().talk();
    }
    
    
    
}





楼上的,你是通过TCP传输的。。。。


有啊。以下代码是用HttpClient去Get一个你要下载的文件:http://www.mysite.com/download.tar.gz
Java code
public class MyHttpClientGetFile {
    public static String url =
              "http://www.mysite.com/download.tar.gz";

    public static void main(String[] args){

        //创建HttpClient
        HttpClient httpClient = new HttpClient();

        //创建 method instance
        GetMethod getMethod = new GetMethod(url);

        try{

          //执行Get方法
          int statusCode =
                 httpClient.executeMethod(getMethod);

          //读取响应。
          InputStream in =
                 getMethod.getResponseBodyAsStream();

          byte[] b = new byte[1024];//1K缓冲区读
          int len;

          while ((len = in.read(b)) != -1) {
            //将数据写到。。。。 System.out
            //你可改为:存到文件中。
            for(int i = 0; i < len; i++){
                 System.out.print((char)b[i]);
            }
          }

          in.close();

         }catch(HttpException e1){
           //System.out.println(e1);
         }catch(IOException e2){
           //System.out.println(e2);
         }finally{
           //释放连接
           getMethod.releaseConnection();
         }

    }

}


非常谢谢,你这个是下载,我想要的是上传的。
需求是这样的:客户端选择本地文件,然后点击上传。当然中间是通过HTTP上传


转自:http://bbs.csdn.net/topics/310108970