码迷,mamicode.com
首页 > Web开发 > 详细

运用tcp协议创建上传文件

时间:2019-11-25 00:07:48      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:throw   buffer   dwr   cli   row   null   刷新   inpu   rgs   

牢记buffered需要刷新flush

package loadfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// TODO Auto-generated method stub
		Socket s = new Socket("192.168.2.143",10003);
		BufferedReader br = new BufferedReader(new FileReader("client.txt"));
		BufferedWriter bwout = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		
		int ch = 0;
		while((ch = br.read()) != -1)
		{
			bwout.write(ch);
			bwout.flush();
		}
		s.shutdownOutput();
		
		BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line = null;
		line = brin.readLine();
		System.out.println(line);
		
		
		br.close();
		s.close();
		bwout.close();
		brin.close();
	}

}

package loadfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket ss = new ServerSocket(10003);
		
		Socket s = ss.accept();
		BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
		BufferedWriter brw = new BufferedWriter(new FileWriter("server.txt"));
		
		String line = null;
		while((line = brin.readLine()) != null)
		{
			brw.write(line);
			brw.newLine();
			brw.flush();
		}
		
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.write("上传成功");
		
		ss.close();
		out.close();
		brin.close();
		brw.close();
	}

}

  

运用tcp协议创建上传文件

标签:throw   buffer   dwr   cli   row   null   刷新   inpu   rgs   

原文地址:https://www.cnblogs.com/WINDZLY/p/11925239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!