package cn.itcast.day_21.upload;
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.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class uploadClient {
public static void main(String[] args) throws Exception {
//2.创建Socket, 指定服务端IP地址和端口号, 连接服务器
Socket socket = new Socket("127.0.0.1", 1234);
//4.获取输入输出流
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
//5.提示用户输入一个路径, 从键盘获取路径, 判断是否存在是否是文件, 如果输入非法给予提示, 重新输入
File file = getFile();
//6.将文件名和大小发送到服务端
ps.println(file.getName());
ps.println(file.length());
//8.接收服务器发送回来的是否存在的结果. 如果文件已存在, 打印提示, 客户端退出. 如果不存在, 准备开始上传.
String msg = br.readLine();
if("已存在".equals(msg)){
System.out.println("文件已存在,请不要重复上传!");
return;
}
long finishLen = Long.parseLong(msg); // 服务器端文件的长度
//9.创建FileInputStream从文件中读取数据, 写出到Socket的输出流
FileInputStream fis = new FileInputStream(file);
OutputStream out = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
fis.skip(finishLen); // 跳过服务端已完成的大小
while((len = fis.read(buffer)) != -1)
out.write(buffer, 0, len);
fis.close();
System.out.println(br.readLine());
socket.close();
}
public static File getFile() throws Exception{ //得到文件的方法
System.out.println("请输入要上传的路径: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
File file = new File(br.readLine());
if(!file.exists())
System.out.println("您输入的路径不存在,请重新输入: ");
else if(file.isDirectory())
System.out.println("占不支持文件夹上传!请输入一个文件路径");
else
return file;
}
}
}
服务器端源码:
package cn.itcast.day_21.upload;
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.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.net.ServerSocketFactory;
import com.sun.corba.se.spi.orbutil.fsm.Input;
public class uploadServer {
public static void main(String[] args) throws Exception {
//1.创建ServerSocket, 在循环中接收客户端请求, 每接收到一个请求就开启新线程处理
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("服务已启动,绑定1234端口!");
while(true){
Socket socket = serverSocket.accept();
new ServerThread(socket).start();
}
}
}
class ServerThread extends Thread{
Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
FileOutputStream fos = null;
try {
//3.获取输入输出流
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
//7.接收文件, 查找是否存在. 如果存在, 给写一个消息给客户端, 服务端线程结束. 如果不存在, 写消息给客户端, 准备开始上传.
String fileName = br.readLine();
long fileLen = Long.parseLong(br.readLine());
File dir = new File("upload");
dir.mkdir();
File file = new File(dir,fileName);
if(file.exists() && file.length() == fileLen){ // 文件已存在, length()即为文件大小, 文件不存在length()为0
ps.println("已存在");
return;
}
else{
ps.println(file.length()); // 文件已存在, length()即为文件大小, 文件不存在length()为0
}
//10.从Socket的输入流中读取数据, 创建FileOutputStream写出到文件中
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String ip = socket.getInetAddress().getHostAddress();
System.out.println(time + " " + ip + (file.exists() ? " 开始断点续传: " : " 开始上传文件: ") + file.getName());
long start = System.currentTimeMillis();
InputStream in = socket.getInputStream();
fos = new FileOutputStream(file, true); // 文件存在就追加, 文件不存在则创建
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) != -1){ //这个地方会堵塞,如果客服端没有关闭socket.服务器端读不到流末尾(-1)
fos.write(buffer, 0, len);
if(file.length() == fileLen) // 文件大小等于客户端文件大小时, 代表上传完毕
break;
}
fos.close();
long end = System.currentTimeMillis();
time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(time + " " + ip + " 上传文件结束: " + file.getName() + ", 耗时: " + (end - start) + "毫秒");
ps.println("上传成功");
socket.close();
} catch (IOException e) {
if(fos != null)
try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
|
|