本帖最后由 小石姐姐 于 2019-5-17 11:27 编辑
[Java] 纯文本查看 复制代码 public class TCPServer {
public static void main(String[] args)throws IOException {
//1创建服务器对象
ServerSocket server = new ServerSocket(9898);
//2等待客户端连接 如果有客户端连接 获取到客户端对象
Socket s = server.accept();
//3当前在服务器中 读取数据
InputStream in = s.getInputStream();
//4当前在服务器中 将数据写到流中
FileOutputStream fos = new FileOutputStream("/Users/apple/Documents/复制品.JPG");
byte[] bytes = new byte[1024];
int len = 0 ;
while((len = in.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
//5写完数据提示上传成功
s.getOutputStream().write("上传成功".getBytes());
//6释放资源
fos.close();
s.close();
server.close();
}
}
public class TCPCleint {
public static void main(String[] args)throws IOException {
//1创建服务器对象
Socket s = new Socket("127.0.0.1", 9898);
//2指定路径
FileInputStream fis = new FileInputStream("/Users/apple/Desktop/1.JPG");
//3读取服务器数据
OutputStream out = s.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while((len = fis.read(bytes))!=-1) {
out.write(bytes,0,len);
}
//4告知书写完毕
s.shutdownOutput();
//5书写到服务器
InputStream in = s.getInputStream();
len = in.read(bytes);
System.out.println("服务器:"+new String(bytes,0,len));
//6释放资源
fis.close();
s.close();
}
}
public class TCPClient {
public static void main(String[] args) throws Exception {
//创建 Socket客户端对象
Socket socket = new Socket("127.0.0.1", 8002);
//读服务器数据
InputStream in = socket.getInputStream();//获取输入流
//读数据
byte[] buffer = new byte[1024];
int len = in.read(buffer);
System.out.println(new String(buffer, 0, len));
//释放资源
in.close();
socket.close();
}
}
public class TCPServer {
public static void main(String[] args) throws Exception {
//1创建服务器对象
ServerSocket ss = new ServerSocket(8002);
//2等待客户端连接 如果有客户端连接 获取到客户端对象
Socket socket = ss.accept();
//3当前在服务器中 将数据写到流中
OutputStream out = socket.getOutputStream();
out.write("hello world".getBytes());
//释放资源
out.close();
// ss.close();服务器一般不会关闭
package Inter;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable{
private Socket s ;
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
int count =0;
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
File f = new File("WuYi\\b["+count+"].txt");
while(f.exists()){
count++;
f = new File("WuYi\\b["+count+"].txt");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
String line ;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bf.write("文件传输完毕");
bf.newLine();
bf.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package Inter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class KeHuduan {
public static void main(String[] args) throws IOException {
//创建客户端的对象
//Scanner sc = new Scanner(System.in);
//String s1 = sc.nextLine();
Socket s = new Socket("127.0.0.1",10000);
//获取输出流,写数据
OutputStream ops = s.getOutputStream();
//ops.write(s1.getBytes());
ops.write("俺老孙来也".getBytes());
//接受服务器反馈
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int len=is.read(b);
/*while ((len=is.read(b))!=-1){
System.out.println("客户端:"+new String(b,0,len));
}*/
String s2 = new String(b, 0, len);
System.out.println("客户端:"+s2);
//关闭
s.close();
}
} [Java] 纯文本查看 复制代码 public class KKKk {
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1",10002);
//客户端读取图片文件
//将文件写入服务器
BufferedReader br = new BufferedReader(new FileReader("WuYi\\2.png"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();
BufferedReader brr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s1 = brr.readLine();
System.out.println("服务器反馈:" + s1);
br.close();
s.close();
}
}
package LIu;
import javax.imageio.event.IIOReadProgressListener;
import java.io.*;
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
//文件源目录
File yuanFile = new File("D:\\test");
//目的地
File mudiFile = new File("WuYi\\");
//写方法实现文件夹的复制,参数为数据源文件和目的地目录
copyDir(yuanFile, mudiFile);
}
private static void copyDir(File yuanFile, File mudiFile) throws IOException {
if (yuanFile.isDirectory()) {
File newDir = new File(mudiFile, yuanFile.getName());
if (!newDir.exists()) {
newDir.mkdir();
}
//获取数据源目录下所有文件或者目录的File数组
File[] files = yuanFile.listFiles();
//遍历得到每个文件
for (File file : files) {
//把该File 作为数据源对象递归调用复制文件夹的方法
copyDir(file, newDir);
}
} else {
copyFile(yuanFile, new File(mudiFile, yuanFile.getName()));
}
}
private static void copyFile(File yuanFile, File mudiFile) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(mudiFile));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(yuanFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read()) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
|