Tcp实现文件上传

package com.wuming.lesson02;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo02 {
    public static void main(String[] args) throws Exception {
        //1.创服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //2.监听客户端连接
        Socket socket = serverSocket.accept();//阻塞式监听,一直等待客户端连接
        //3.获取输入流
        InputStream is = socket.getInputStream();

        //4.文件输出
        FileOutputStream fos = new FileOutputStream(new File("receive2.jpg"));//自动生成图片
        byte[] buffer = new byte[1024];
        int len;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        //通知客户端我接受完毕了
        OutputStream os = socket.getOutputStream();
        os.write("我接受完毕了,你可以断开了".getBytes());

        //关闭资源
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
===
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TCP文件上传与下载的实现需要使用Socket编程。以下是一个简单的Python实现文件上传: 1. 客户端连接服务器,并发送文件名和文件大小。 2. 服务器接收到文件名和文件大小后,创建一个新的文件,并等待客户端发送文件内容。 3. 客户端读取文件内容,并将其发送到服务器。 4. 服务器接收到文件内容后,将其写入文件中。 代码实现: 客户端: ```python import socket def send_file(filename, host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) filesize = os.path.getsize(filename) s.sendall(f"{filename}:{filesize}".encode()) with open(filename, "rb") as f: while True: data = f.read(1024) if not data: break s.sendall(data) print("File sent successfully.") if __name__ == "__main__": send_file("test.txt", "localhost", 8000) ``` 服务器: ```python import socket def receive_file(host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((host, port)) s.listen() conn, addr = s.accept() with conn: print(f"Connected by {addr}") data = conn.recv(1024).decode() filename, filesize = data.split(":") filesize = int(filesize) with open(filename, "wb") as f: while True: data = conn.recv(1024) if not data: break f.write(data) print("File received successfully.") if __name__ == "__main__": receive_file("localhost", 8000) ``` 文件下载: 1. 客户端连接服务器,并发送要下载的文件名。 2. 服务器接收到文件名后,打开文件并将其内容发送给客户端。 3. 客户端接收到文件内容后,将其写入文件中。 代码实现: 客户端: ```python import socket def receive_file(filename, host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) s.sendall(filename.encode()) with open(filename, "wb") as f: while True: data = s.recv(1024) if not data: break f.write(data) print("File received successfully.") if __name__ == "__main__": receive_file("test.txt", "localhost", 8000) ``` 服务器: ```python import socket def send_file(filename, host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((host, port)) s.listen() conn, addr = s.accept() with conn: print(f"Connected by {addr}") with open(filename, "rb") as f: while True: data = f.read(1024) if not data: break conn.sendall(data) print("File sent successfully.") if __name__ == "__main__": send_file("test.txt", "localhost", 8000) ``` 以上代码只是一个简单的实现,实际应用中还需要考虑文件传输过程中的错误处理、断点续传等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值