//麻烦帮我看一下是哪里出了问题,谢谢!
//服务器
package FileInput;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Servers {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String info = null;
ArrayList<User> users= new ArrayList<User>();
User u = new User();
System.out.println("服务器开始运行");
ServerSocket ss = new ServerSocket(8810);
Socket s = ss.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(is);
FileWriter fw = new FileWriter("README1.md");
while (true) {
info = br.readLine();
if (info != null)
System.out.println(info);
if (info == null) {
continue;
} else if (info.equals("log")) {
pw.println("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("exit")) {
pw.println("exit");
pw.flush();
System.out.println("exit已发送");
break;
} else if (info.equals("logUp")) {
pw.println("logUp");
pw.flush();
System.out.println("logUp已发送");
User user = null;
while ((user = (User) ois.readObject()) == null);
boolean flag = true;
for (int i = 0; i < users.size(); i++) {
if (user.getName().equals(users.get(i).getName())) {
flag = false;
break;
}
}
if (flag) {
users.add(user);
pw.print("logUpSuccess");
pw.flush();
System.out.println("logUpSuccess已发送");
} else {
pw.println("logUpFailure");
pw.flush();
System.out.println("logUpFailure已发送");
}
} else if (info.equals("logIn")) {
pw.println("logIn");
pw.flush();
System.out.println("logIn已发送");
User user = null;
while ((user = (User) ois.readObject()) == null);
boolean flagN = false;
boolean flagP = false;
for (int i = 0; i < users.size(); i++) {
if (user.getName().equals(users.get(i).getName())) {
flagN = true;
if (user.getPassword().equals(users.get(i).getPassword())) {
flagP = true;
u = users.get(i);
}
break;
}
}
if (flagN && flagP) {
pw.print("logInSuccess");
pw.flush();
System.out.println("logInSuccess已发送");
} else if (flagN && !flagP) {
pw.print("logInFailurePassword");
pw.flush();
System.out.println("logInFailurePassword已发送");
} else {
pw.print("logInFailureName");
pw.flush();
System.out.println("logInFailureName已发送");
}
} else if (info.equals("logOut")) {
pw.println("logOut");
pw.flush();
System.out.println("logOut已发送");
} else if (info.equals("code")) {
pw.println("code");
pw.flush();
System.out.println("code已发送");
} else if (info.equals("putFlie")) {
pw.println("file");
pw.flush();
System.out.println("file已发送");
while (true) {
int l = 0;
char[] c = new char[3072];
while ((l = isr.read(c, 0, c.length)) == -1);
do {
fw.write(c, 0, l);
} while ((l = isr.read(c, 0, c.length)) == -1);
System.out.println("文件已写出");
}
}
}
fw.close();
s.close();
ss.close();
}
}
//客户端
package FileInput;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Client{
public static void main(String[] args) throws UnknownHostException, IOException {
String info = null;
Scanner input = new Scanner(System.in);
Client client = new Client();
Socket s = new Socket("localhost", 8810);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ObjectOutputStream oos = new ObjectOutputStream(os);
FileReader fr = new FileReader("src/Internet/README.md");
pw.println("log");
pw.flush();
System.out.println("log已发送");
while (true) {
info = br.readLine();
if (info != null) System.out.println(info);
if (info == null) {
continue;
} else if (info.equals("logUpSuccess")) {
System.out.println("注册成功!");
pw.print("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("logUpFailure")) {
System.out.println("用户已存在!");
pw.print("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("logInFailurePassword")) {
System.out.println("密码错误!");
pw.print("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("logUpFailureName")) {
System.out.println("用户不存在!");
pw.print("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("logUpSuccess")) {
System.out.println("登录成功!");
pw.print("code");
pw.flush();
System.out.println("code已发送");
} else if (info.equals("logOut")) {
System.out.println("登出成功!");
pw.print("log");
pw.flush();
System.out.println("log已发送");
} else if (info.equals("log")) {
String select = client.log(input);
pw.println(select);
pw.flush();
System.out.println(select+"已发送");
} else if (info.equals("logUp")) {
User user = client.logUp(input);
oos.writeObject(user);
oos.flush();
System.out.println("user已发送");
} else if (info.equals("file")) {
String select = client.fileInput(input);
char[] c = new char[10 * 1024];
int l = 0;
while ((l = fr.read(c, 0, c.length))!= -1) {
pw.write(c, 0, l);
}
System.out.println("文件已发送");
} else if (info.equals("exit")) {
System.out.println("文件传输系统已退出!");
break;
} else if (info.equals("logIn")) {
User user = client.logIn(input);
oos.writeObject(user);
oos.flush();
System.out.println("user已发送");
} else if (info.equals("code")) {
String select = client.code(input);
pw.println(select);
pw.flush();
System.out.println(select+"已发送");
}
}
fr.close();
s.close();
}
public String log(Scanner input) {
System.out.println("欢迎使用文件上传系统,请先登录或注册。");
System.out.println("1. 注册");
System.out.println("2. 登录");
System.out.println("3. 退出");
System.out.println("请输入相应的序号:");
int select = 0;
do {
try {
select = input.nextInt();
} catch (NoSuchElementException e) {
System.out.println("请输入相应的序号!");
continue;
} catch (NullPointerException e) {
System.out.println("请输入相应的序号!");
continue;
}
if (select > 3 || select < 1) {
System.out.println("请输入相应的序号!");
continue;
}
} while (select > 3 || select < 1);
String s = null;
switch (select) {
case 1:
s = "logUp";
break;
case 2:
s = "logIn";
break;
default:
s = "exit";
break;
}
return s;
}
public User logUp(Scanner input) {
User user = new User();
String name = null;
String password = null;
System.out.println("请输入用户名:");
name = input.next();
user.setName(name);
while (true) {
System.out.println("请输入密码:");
password = input.next();
user.setPassword(password);
System.out.println("请再次输入密码:");
password = input.next();
if (!password.equals(user.getPassword())) {
System.out.println("密码与重复密码不匹配,请重新输入密码!");
} else {
break;
}
}
return user;
}
public User logIn(Scanner input) {
User user = new User();
String name = null;
String password = null;
System.out.println("请输入用户名:");
name = input.next();
user.setName(name);
System.out.println("请输入密码:");
password = input.next();
user.setPassword(password);
return user;
}
public String code(Scanner input) {
System.out.println("请选择功能");
System.out.println("1. 上传文件");
System.out.println("2. 登出");
int select = 0;
do {
try {
select = input.nextInt();
} catch (NoSuchElementException e) {
System.out.println("请输入相应的序号!");
continue;
} catch (NullPointerException e) {
System.out.println("请输入相应的序号!");
continue;
}
if (select > 2 || select < 1) {
System.out.println("请输入相应的序号!");
continue;
}
} while (select > 2 || select < 1);
String s = null;
switch (select) {
case 1:
s = "putFile";
break;
case 2:
s = "logOut";
break;
default:
}
return s;
}
public String fileInput(Scanner input) {
System.out.println("请输入要上传的文件路径(相对路径或绝对路径)");
String select = input.next();
return select;
}
}
//user
package FileInput;
import java.io.Serializable;
public class User implements Serializable{
private String name;
private String password;
User() {
name = null;
password = null;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getPassword() {
return password;
}
void setPassword(String password) {
this.password = password;
}
}