package com.itheima01;
import java.io.*;
import java.net.Socket;
public class FileStocket {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.14.24", 2019);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\itcast.txt"),"GBK"));//GBK--UTF-8
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));// 8bits -- UTF-8
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
s.shutdownOutput();
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("服务端反馈:" + br1.readLine());
s.close();
br.close();
}
}
package com.itheima02;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Test03 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(2019);
while (true){
Socket s = ss.accept();
Thread t = new Thread(new Test04(s));
t.start();
}
}
}
package com.itheima02;
import java.io.*;
import java.net.Socket;
public class Test04 implements Runnable{
private Socket s;
//constructor
public Test04(Socket s ){
this.s = s;
}
@Override
public void run() {
int count = 0;
while (true){
File file = new File("D:\\itcast["+count+"].txt");
if(!file.exists()){
break;
}
count++;
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\itcast["+count+"].txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine())!= null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw1.write("文件传输完成!");
bw1.newLine();
bw1.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\itcast.txt"),"GBK"));
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |