其他分享
首页 > 其他分享> > 六十三、TCP上传文件

六十三、TCP上传文件

作者:互联网

服务器

package demo1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Serve1 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
			ServerSocket socket = new ServerSocket(9994);
			Socket accept = socket.accept();
			InputStream is = accept.getInputStream();
			FileOutputStream fos = new FileOutputStream(new File("D://receive.png"));
			byte[] bt = new byte[1024];
			int len;
			while ((len=is.read(bt))!=-1) {
				fos.write(bt,0,len);
			}
			fos.close();
			is.close();
			accept.close();
			socket.close();
			
	}

}

客户端


package demo1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client1 {

public static void main(String[] args) throws UnknownHostException, IOException {
	// TODO Auto-generated method stub
		Socket socket = new Socket(InetAddress.getByName("127.0.0.1."), 9994);
		OutputStream os = socket.getOutputStream();
		FileInputStream fis = new FileInputStream(new File("D://0319.png"));
		byte[] bt = new byte[1024];
		int len;
		while ((len=fis.read(bt))!=-1) {
			os.write(bt,0,len);
		}
		System.out.println("已上传");
		fis.close();
		os.close();
		socket.close();
}

}


已上传

标签:java,close,TCP,bt,io,new,六十三,import,上传
来源: https://blog.csdn.net/qq_41995845/article/details/119903685