黑马程序员技术交流社区
标题:
自己写的模拟网盘客户端及服务器端
[打印本页]
作者:
蔡锐
时间:
2015-8-29 20:59
标题:
自己写的模拟网盘客户端及服务器端
最近终于自学完了网络编程,结合网络编程,IO,和多线程写了个模拟网盘,感觉写的挺笨的,但初步实现了上传下载功能
,我就献丑把代码贴上来了,希望大家给点修改建议客户端代码
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.Socket;
import java.net.UnknownHostException;
/**
* 模拟网盘客户端,支持上传下载
*
* @author CR
* @version 1.1
*/
public class NetDiskClient {
public static String defaultServerIP = "192.168.1.41";// 默认服务器IP,自行修改
public static int defaultServerPort = 15678;// 默认服务器端口
public static File defaultPath = new File("D:\\MyNetDiskDownload");// 默认下载路径
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("------欢迎使用模拟网盘客户端------");
System.out.println("1.设置服务器IP");
System.out.println("2.设置服务器端口");
System.out.println("3.开始上传");
System.out.println("4.开始下载");
System.out.println("5.设置下载路径");
System.out.println("6.退出");
System.out.println("请选择;");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String line = br.readLine();
switch (line) {
case "1":
System.out.println("当前默认的服务器IP为:" + defaultServerIP
+ ",请输入你要设置的IP:");
String newIP = br.readLine();
defaultServerIP = newIP;
System.out.println("设置成功!");
break;
case "2":
System.out.println("当前默认的服务器端口为:" + defaultServerPort
+ ",请输入你要设置的端口:");
String newPort = br.readLine();
defaultServerPort = Integer.parseInt(newPort);
System.out.println("设置成功!");
break;
case "3":
uploadFile();
break;
case "4":
downloadFile();
break;
case "5":
System.out.println("当前下载目录为:" + defaultPath.getAbsolutePath()
+ "请输入你要设置的下载目录:");
System.out.println("路径需要用双\\\\或者/,如:H:\\\\test 或者 H:/test");
String newPath = br.readLine();
defaultPath = new File(newPath);// 路径需要用双\\或者/,如:H:\\test 或者
// H:/test
System.out.println("设置完成!");
break;
case "6":
default:
System.out.println("谢谢使用,再见!");
System.exit(0);
}
}
}
private static void uploadFile() throws UnknownHostException, IOException {
// TODO Auto-generated method stub
Socket socket = new Socket(defaultServerIP, defaultServerPort);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
byte[] bys = new byte[1024];
int len;
os.write("upload".getBytes());
while (true) {
System.out.println("请输入要上传的文件的全路径:");
System.out
.println("路径需要用双\\\\或者/,如:H:\\\\test\\\\test.txt 或者 H:/test/test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String filePath = br.readLine();
File file = new File(filePath);// 将文件路径封装进File
if (!file.isFile()) {
System.out.println("文件名非法,请重新输入!");// 只允许上传文件
continue;
}
String fileName = file.getName();
os.write(fileName.getBytes());// 获取文件名传给服务器
len = is.read(bys);
String message = new String(bys, 0, len);
if (message.equals("文件名存在,请重新输入!")) {
// 读取服务器信息进行判断
System.out.println(message);
continue;
} else {
System.out.println(message);
String order = br.readLine();
// 继续给指令
os.write(order.getBytes());
len = is.read(bys);
message = new String(bys, 0, len);// 读取指令进行判断
if (message.equals("开始上传")) {
System.out.println(message);
long start = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
while ((len = bis.read(bys)) != -1) {
os.write(bys, 0, len);
}
socket.shutdownOutput();
long end = System.currentTimeMillis();
message = "上传成功,共耗时" + (end - start) + "毫秒";
System.out.println(message);
bis.close();
socket.close();
break;
} else {
System.out.println(message);
System.exit(0);
}
}
}
}
public static void downloadFile() throws UnknownHostException, IOException {
Socket socket = new Socket(defaultServerIP, defaultServerPort);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
byte[] bys = new byte[1024];
int len;
os.write("download".getBytes());
if (!defaultPath.exists()) {
defaultPath.mkdir();
}
if (!defaultPath.isDirectory()) {
System.out.println("下载路径有误,请返回重新设置!");
socket.close();
return;
}
while (true) {
String msg = "";
System.out.println("你存储的文件如下:");
while ((len = is.read(bys)) != -1) {
msg = new String(bys, 0, len);
System.out.println(msg);
if (msg.endsWith("请输入编号:")) {
break;
}
if (msg.equals("你还没有在服务器中上传过文件!")
|| msg.equals("你在服务器的文件目录中没有文件!")) {
socket.close();
return;
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
msg = br.readLine();
os.write(msg.getBytes());
len = is.read(bys);
msg = new String(bys, 0, len);
if (msg.equals("请输入正确的编号!")) {
System.out.println(msg);
continue;
} else {
len = is.read(bys);
String name = new String(bys, 0, len);
System.out.println("是否下载" + name + "?(是/否)");
msg = br.readLine();
os.write(msg.getBytes());
len = is.read(bys);
msg = new String(bys, 0, len);
if (msg.equals("开始下载")) {
System.out.println("正在下载,文件存储在" + defaultPath);
long start = System.currentTimeMillis();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(defaultPath, name)));
while ((len = is.read(bys)) != -1) {
bos.write(bys, 0, len);
}
socket.shutdownInput();
long end = System.currentTimeMillis();
msg = "下载成功,共耗时" + (end - start) + "毫秒";
System.out.println(msg);
bos.close();
socket.close();
break;
} else {
System.out.println(msg);
socket.close();
break;
}
}
}
}
}
复制代码
网盘v1.1.zip
(4.12 KB, 下载次数: 51)
2015-8-29 20:58 上传
点击文件名下载附件
作者:
蔡锐
时间:
2015-8-29 21:00
贴不下了 服务器代码贴这里了
/**
* 模拟网盘服务器端,支持上传和下载
* @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();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2