如题,问题都写到注释里里,有大神帮忙解答一下吗?
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Question1_Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(9000);
while(true) {
final Socket socket = server.accept();
new Thread() {
public void run() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String fileName = br.readLine();
File dir = new File("Update");
dir.mkdir();
File file = new File(dir,fileName);
PrintStream ps = new PrintStream(socket.getOutputStream());
if(file.exists()) {
ps.println("false");
socket.close();
return;
}
ps.println("true");
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
int temp;
System.out.println("开始接收!");
/*-------------------------未知 bug-----------------------------------
* 如果Client不close()Socket( or socket.outputstream )这个while一直不能结束。
* 这样导致设想中的Server写完文件后“发送消息给Client,确认上传成功完成”不能实现。
* *****暂时认为是读不到-1(bis不能达到流末尾)
*-------------------------未知 bug----------------------------------- */
while((temp = bis.read()) != -1) {
bos.write(temp);
}
/*-------------------------未知 bug-----------------------------------
* 如果Client不close()Socket( or socket.outputstream )这个while一直不能结束。
*-------------------------未知 bug----------------------------------- */
bos.close();
System.out.println("接收完成!");
//设想中的功能,确认完成后发送信息给Client
//ps.println("true");
socket.close();
} catch(Exception e) {
e.printStackTrace();
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Question1_Client {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
File file;
while(true) {
System.out.println("请录入上传的文件路径");
file = new File(sc.nextLine());
if(!file.exists()) {
System.out.println("文件不存在,请重新录入");
}else if (file.isDirectory()) {
System.out.println("该路径指向的是文件夹,请重新录入");
}else {
sc.close();
break;
}
}
try {
Socket socket = new Socket("127.0.0.1", 9000);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println(file.getName());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String answer = br.readLine();
if ("false".equals(answer)) {
System.out.println("上传失败,该文件已经存在");
socket.close();
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
System.out.println("开始上传!");
int temp;
while((temp = bis.read()) != -1) {
ps.write(temp);
}
bis.close();
System.out.println("上传结束!");
//设想中的功能,服务器端成功写完文件后发送"true"过来。
/*if("true".equals(br.readLine())) {
System.out.println("上传成功!");
socket.close();
}else {
System.out.println("上传失败!未知错误");
socket.close();
}*/
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
|