JAVA学习第二十五天

一、FileUpload

  • 实现步骤:
    *1、创建一个本地字节收入流 FileInputStream 对象,构造方法中绑定读取的数据源
    *2、创建一个socket对象,构造方法中绑定 服务器的IP地址和端口号
    *3、使用socket中的方法getoutputStream 获取网络字节输出流
    *4、使用本地字节输入流 中的read方法,读取本地文件
    *5、使用网络输出流中的write方法,把读取到的文件上传到服务器
    *6、使用socket中方法,获取输入流对象
    *7、通过网络输入流对象中的read方法,读取回写数据
    *8、释放资源

客户端

在这里插入代码片public class Client {
	public static void main(String[] args) throws IOException {
		// *1、创建一个本地字节收入流 FileInputStream 对象,构造方法中绑定读取的数据源
		FileInputStream fis = new FileInputStream("E:\\eclipse\\1.jpg");
		//2、创建一个socket对象,构造方法中绑定 服务器的IP地址和端口号
		Socket socket = new Socket("127.0.0.1",8888);
		// *3、使用socket中的方法getoutputStream 获取网络字节输出流
		OutputStream os = socket.getOutputStream();
		
		int len =0;
		byte[] bytes= new byte[1024];
		//4、使用本地字节输入流 中的read方法,读取本地文件
		//5、使用网络输出流中的write方法,把读取到的文件上传到服务器
		while((len=fis.read(bytes))!=-1) {
			os.write(bytes,0,len);
		}
		//告诉服务器文件已经被发送完毕
		socket.shutdownOutput();
		//获得网络输入流
		InputStream is=socket.getInputStream();
		
		while((len= is.read(bytes))!=-1) {
			System.out.println(new String(bytes,0,len));
		}
		
		fis.close();
		socket.close();
		
	}

}

服务器

public class TcpServer {
	public static void main(String[] args) throws IOException {
		//1.创建一个服务器的serversocket对象
		ServerSocket server= new ServerSocket(8888);
		
		//2.等待客户端连接
		Socket socket = server.accept();
		//3.获取输入流
		InputStream is=socket.getInputStream();
		//4.准备数据存放位置

		File file=new File("E:\\20210601");
		if(!file.exists()) {
			file.mkdirs();
		}
		
		FileOutputStream fos= new FileOutputStream(file+"1.jpg");
		
		int len=0;
		byte[] bytes=new byte[1024];
		
		while((len=is.read(bytes))!=-1) {
			fos.write(bytes,0,len);
		}
		
		socket.getOutputStream().write("图片上传成功".getBytes());
		
		fos.close();
		socket.close();
		server.close();
		
	}

}

更改版

服务器

public class TcpServer {
	public static void main(String[] args) {
		//1.创建一个服务器的serversocket对象
		ServerSocket server= new ServerSocket(8888);
		while(true) {
			//2.等待客户端连接
			Socket socket = server.accept();
		    new Thread (new Runnable() {

			    public void run() {//3.获取输入流
					InputStream is=socket.getInputStream();
					//4.
					File file=new File("E:\\20210601");
					if(!file.exists()) {
						file.mkdirs();
					}
					
					String fileName="classFive"+System.currentTimeMillis()+new Random().nextInt(99999)+".jpq";
					FileOutputStream fos= new FileOutputStream(file+"2.jpg");
					
					int len=0;
					byte[] bytes=new byte[1024];
					
					while((len=is.read(bytes))!=-1) {
						fos.write(bytes,0,len);
					}
					
					socket.getOutputStream().write("图片上传成功".getBytes());
					
					fos.close();
					socket.close();
					
				
				try {
					
					
				}catch(IOException e) {
					System.out.println(e);
				}
				
			}
		 }	
	}	    


客户端

public class Client {
	public static void main(String[] args) throws IOException {
		// *1、创建一个本地字节收入流 FileInputStream 对象,构造方法中绑定读取的数据源
		FileInputStream fis = new FileInputStream("E:\\eclipse\\1.jpg");
		//2、创建一个socket对象,构造方法中绑定 服务器的IP地址和端口号
		Socket socket = new Socket("127.0.0.1",8888);
		// *3、使用socket中的方法getoutputStream 获取网络字节输出流
		OutputStream os = socket.getOutputStream();
		
		int len =0;
		byte[] bytes= new byte[1024];
		//4、使用本地字节输入流 中的read方法,读取本地文件
		//5、使用网络输出流中的write方法,把读取到的文件上传到服务器
		while((len=fis.read(bytes))!=-1) {
			os.write(bytes,0,len);
		}
		//告诉服务器文件已经被发送完毕
		socket.shutdownOutput();
		//获得网络输入流
		InputStream is=socket.getInputStream();
		
		while((len= is.read(bytes))!=-1) {
			System.out.println(new String(bytes,0,len));
		}
		
		fis.close();
		socket.close();
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值