public void run() {
FileOutputStream fos = null;
try {
// 3.明确目的地
File file = new File("D:\\nihao");
// 如果该目录不存在则创建
if (!file.exists()) {
file.mkdirs();
}
String filename = "oracle" + System.currentTimeMillis() + ".txt";
// 明确目的地
fos = new FileOutputStream(file + File.separator + filename);
// 明确数据源
InputStream in = socket.getInputStream();
// 开始复制
int len = 0;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
// 回复客户端
// 获取字节输出流 ,回复客户端
OutputStream out = socket.getOutputStream();
out.write("上传成功".getBytes());
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 释放资源
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Demo {
public static void main(String[] args) throws IOException {
//1.创建服务器对象
ServerSocket server=new ServerSocket(8888);
while(true){
//创建连接获取客户端对象
Socket socket=server.accept();
/*//创建线程(创建线程任务)
new Thread(new Upload(socket)).start();*/
//创建线程任务
Upload up=new Upload(socket);
//创建线程对象
Thread thread=new Thread(up);
//开启线程
thread.start();
}
}
}