/*
* 线程任务类
*/
public class Task implements Runnable {
private Socket socket;
public Task(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//显示一下哪个IP地址连接上的服务器
String ip = socket.getInetAddress().getHostAddress();
System.out.println("已连接的客户端IP ----" +ip);
//1,获取客户端的Socket对象的输入流 ---- 数据源
//InputStream is = socket.getInputStream();
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
//2,创建目的地输出流
String fileName = ip + "("+System.currentTimeMillis()+").JPG";
FileOutputStream outFile = new FileOutputStream(fileName);
BufferedOutputStream out = new BufferedOutputStream(outFile);
//3,把Socket输入流读到的数据, 写入到目的地文件中
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1) {
//写数据到目的地
out.write(buffer, 0, len);
}
//4,关闭操作文件的流
out.close();
//5,给客户端反馈数据
//OutputStream os = socket.getOutputStream();
//OutputStreamWriter osw = new OutputStreamWriter(os);
//BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("文件上传成功!!!!!");
bw.flush();
//6,关闭客户端的Socket流
socket.close();
} catch(IOException e){
e.printStackTrace();
}
}
} 作者: SandyLiu 时间: 2016-9-6 00:13
怎么没人评论呢? 写得这么好,点个赞!