贴不下了 服务器代码贴这里了
- /**
- * 模拟网盘服务器端,支持上传和下载
- * @author CR
- * @version V1.1
- */
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class NetDiskServer {
- public static String defaultAddress = "D:\\MyNetDisk";
- public static int defalutPort = 15678;
- public static void main(String[] args) throws IOException {
- while (true) {
- System.out.println("------欢迎使用模拟网盘服务器端------");
- System.out.println("1.设置默认存储位置");
- System.out.println("2.设置默认端口");
- System.out.println("3.开启服务器");
- System.out.println("4.退出");
- System.out.println("请选择:");
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- String line = br.readLine();
- switch (line) {
- case "1":
- System.out.println("当前服务器存储位置:" + defaultAddress
- + ",请输入你要设置 目录:");
- String address = br.readLine();
- defaultAddress = address;
- System.out.println("设置成功!");
- break;
- case "2":
- System.out.println("当前服务器端口为:" + defalutPort + ",请输入你要设置的端口:");
- String newPort = br.readLine();
- defalutPort = Integer.parseInt(newPort);
- System.out.println("设置成功!");
- break;
- case "3":
- ServerSocket serverSocket = new ServerSocket(defalutPort);
- while (true) {
- Socket socket = serverSocket.accept();
- new Thread(new NetDiskServerThread(socket, defaultAddress))
- .start();
- }
- case "4":
- default:
- System.out.println("谢谢使用!再见");
- System.exit(0);
- }
- }
- }
- }
- class NetDiskServerThread implements Runnable {
- private Socket socket;// 存储传入的socket
- private String serverDiskAddress;// 用来存储服务器的存储目录
- byte[] buf = new byte[1024];
- int len;
- public String getServerDiskAddress() {
- return serverDiskAddress;
- }
- public void setServerDiskAddress(String serverDiskAddress) {
- this.serverDiskAddress = serverDiskAddress;
- }
- public Socket getSocket() {
- return socket;
- }
- public NetDiskServerThread(Socket socket, String serverDiskAddress) {
- this.socket = socket;
- this.serverDiskAddress = serverDiskAddress;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- InputStream is0 = socket.getInputStream();
- len = is0.read(buf);
- String msg = new String(buf, 0, len);
- if (msg.equals("upload")) {
- update();
- } else {
- download();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void update() {
- String ip = socket.getInetAddress().getHostAddress();
- String hostName = socket.getInetAddress().getHostName();// 获取客户端的主机名
- try {
- System.out.println("ip为" + ip + "主机名:" + hostName + "连接服务器!");
- File parent = new File(serverDiskAddress + "\\" + ip);// 每个主机拥有一个独立的目录
- while (true) {
- InputStream is = socket.getInputStream();
- OutputStream os = socket.getOutputStream();
- len = is.read(buf);
- String fileName = new String(buf, 0, len);// 读取上传的文件名
- File file = new File(parent, fileName);
- if (file.exists()) {
- os.write("文件名存在,请重新输入!".getBytes());// 判断文件名是否重复
- continue;
- } else {
- // 不重复提示是否上传
- os.write("文件可以上传,确认上传?(是/否)".getBytes());
- len = is.read(buf);
- if (new String(buf, 0, len).equals("否")) {
- // 否认上传就结束咯
- os.write("谢谢使用,再见!".getBytes());
- socket.close();
- break;
- } else if (new String(buf, 0, len).equals("是")) {
- // 确认上传给提示,开始上传
- os.write("开始上传".getBytes());
- System.out
- .println("正在接收来自" + ip + "的" + file.getName());
- if (!parent.exists()) {
- parent.mkdirs();// 如果目录不存在就创建
- }
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(file));
- while ((len = is.read(buf)) != -1) {
- bos.write(buf, 0, len);
- }
- System.out.println(hostName + "上传完成");
- bos.close();
- socket.close();
- break;
- } else {
- // 指令不对也结束
- os.write("不要乱给指令哇,重来吧!".getBytes());
- socket.close();
- break;
- }
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- throw new RuntimeException(e);
- // e.printStackTrace();
- }
- }
- public void download() {
- String ip = socket.getInetAddress().getHostAddress();
- String hostName = socket.getInetAddress().getHostName();// 获取客户端的主机名
- int fileCount = 0;
- try {
- System.out.println("ip为" + ip + "主机名:" + hostName + "连接服务器!");
- File parent = new File(serverDiskAddress + "\\" + ip);// 每个主机拥有一个独立的目录
- OutputStream os = socket.getOutputStream();
- InputStream is = socket.getInputStream();
- if (!parent.exists()) {
- os.write("你还没有在服务器中上传过文件!".getBytes());// 如果没找到目录则结束下载
- socket.close();
- return;
- } else {
- String[] list = parent.list();
- if (list.length == 0) {
- os.write("你在服务器的文件目录中没有文件!".getBytes());// 如果没找到目录则结束下载
- socket.close();
- return;
- } else {
- while (true) {
- for (String string : list) {
- os.write((++fileCount + ":" + string).getBytes());
- }
- os.write("\r\n请输入编号:".getBytes());
- len = is.read(buf);
- fileCount = Integer.parseInt(new String(buf, 0, len));
- if (fileCount > list.length || fileCount < 1) {
- os.write("请输入正确的编号!".getBytes());
- continue;
- } else {
- os.write("准备下载".getBytes());
- File file = new File(parent, list[fileCount - 1]);
- os.write(file.getName().getBytes());
- len = is.read(buf);
- if (new String(buf, 0, len).equals("是")) {
- os.write("开始下载".getBytes());
- System.out.println("主机" + hostName + "正在下载"
- + list[fileCount - 1]);
- BufferedInputStream bis = new BufferedInputStream(
- new FileInputStream(file));
- while ((len = bis.read(buf)) != -1) {
- os.write(buf, 0, len);
- }
- System.out.println(hostName + "下载成功");
- bis.close();
- socket.close();
- break;
- } else {
- os.write("谢谢使用,再见!".getBytes());
- socket.close();
- break;
- }
- }
- }
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
复制代码
|