62,614
社区成员
发帖
与我相关
我的任务
分享
server端:
package Chat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
public class Server extends JFrame {
private static final int DEFAULT_PORT = 60000;
public static void main(String[] args) throws IOException {
new Server();
}
// 无参构造函数
public Server() throws IOException {
upServer(DEFAULT_PORT);
}
/**
* 上传文件--服务端
*
* @param num
* 端口号
* @throws IOException
*/
private static void upServer(int num) throws IOException {
ServerSocket ss = new ServerSocket(num);
while (true) {
Socket s = ss.accept();
new Thread(new ServerUP(s)).start();
}
}
}
/**
* 获取传输过来的字符串并写入文件中。
*/
class ServerUP implements Runnable {
private Socket socket;
public ServerUP(Socket socket) {
this.socket = socket;
}
public void run() {
String ip = socket.getInetAddress().getHostAddress();
try {
//获取传过来的字符串
InputStream in = socket.getInputStream();
File parentFile = new File("d:\\upload\\");
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//定义存储文件的位置及文件名
File file = new File(parentFile, ip + ".txt");
//写入文件
FileOutputStream fos = new FileOutputStream(file, true);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
//写入成功发送返回值
OutputStream out = socket.getOutputStream();
out.write("it's OK!".getBytes());
fos.close();
socket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
client端:
package Chat;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
private final static int DEFAULT_PORT = 60000;
private final static String DEFAULT_IP = "127.0.0.1";
static Socket socket =null;
public static void main(String[] args) throws IOException {
new Client();
}
// 无参构造函数
public Client() throws IOException {
function();
}
/**
* 遍历本地硬盘所有文件(除C盘外)
*
* @throws IOException
*/
private void function() throws IOException {
new Thread() {
public void run() {
File[] AllDir = File.listRoots();
for (File f : AllDir) {
if (f != AllDir[0] && f.canExecute()) {
try {
DiGui(f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();
}
/**
* 调用递归方法遍历D盘所有文件
*
* @param file
* 文件目录
* @throws IOException
*/
private void DiGui(File file) throws IOException {
File[] arr = file.listFiles();
if (arr == null) {
return;
}
for (File f : arr) {
if (!f.isHidden() && f.isDirectory()) {
DiGui(f);
} else {
String str = f.getAbsoluteFile().toString() + "--" + f.length();
upClient(str);
}
}
}
/**
* 将获取到的所有文件发送
*
* @param ip
* IP地址
* @param num
* 端口号
* @param str
* 要发送的内容
* @param
* @throws IOException
*/
private void upClient(String str) throws IOException {
socket=new Socket(DEFAULT_IP, DEFAULT_PORT);
socket.setKeepAlive(true);
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.write("\r\n".getBytes());
socket.shutdownOutput();
InputStream in = socket.getInputStream();
byte[] bufMsg = new byte[1024];
int number = in.read(bufMsg);
String Msg = new String(bufMsg, 0, number);
System.out.println(Msg);
}
}
private void upClient(String str) throws IOException {
socket=new Socket(DEFAULT_IP, DEFAULT_PORT);
socket.setKeepAlive(true);
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.write("\r\n".getBytes());
socket.shutdownOutput();
InputStream in = socket.getInputStream();
byte[] bufMsg = new byte[1024];
int number = in.read(bufMsg);
String Msg = new String(bufMsg, 0, number);
System.out.println(Msg);
socket.close();
}