第二十五次学习Java
1、
package Demo01FileUpload;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“D:\th\java\01_端口号.bmp”);
Socket socket =new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes= new byte[1024];
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
//告诉服务器,文件已经发送完毕
socket.shutdownOutput();
//获取网络输入流
InputStream is=socket.getInputStream();
while((len=is.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
//释放资源
fis.close();
socket.close();
}
}
2、
package Demo01FileUpload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
//1、创建一个服务器的serversocket对象
ServerSocket server= new ServerSocket(8888);
//2、等待客户端连接
Socket socket=server.accept();
//3、获取输入流
InputStream is=socket.getInputStream();
//
File file= new File("E:\\20210601");
if(!file.exists()) {
file.mkdirs();
}
FileOutputStream fos= new FileOutputStream(file+"\\1.bmp");
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
socket.getOutputStream().write("图片上传成功".getBytes());
fos.close();
socket.close();
server.close();
}
}
3、
package Demo02FileUpload;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“D:\th\java\01_端口号.bmp”);
Socket socket =new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes= new byte[1024];
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
//告诉服务器,文件已经发送完毕
socket.shutdownOutput();
//获取网络输入流
InputStream is=socket.getInputStream();
while((len=is.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
//释放资源
fis.close();
socket.close();
}
}
4、
package Demo02FileUpload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket server= new ServerSocket(8888);
while(true) {
Socket socket=server.accept();
new Thread (new Runnable() {
public void run() {
try {
InputStream is=socket.getInputStream();
File file= new File("E:\\20210601");
if(!file.exists()) {
file.mkdirs();
}
String fileName= "classFive"+System.currentTimeMillis()+new Random().nextInt(99999)+".bmp";
FileOutputStream fos= new FileOutputStream(file+"\\"+fileName);
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
socket.getOutputStream().write("图片上传成功".getBytes());
fos.close();
socket.close();
}catch(IOException e) {
System.out.println(e);
}
}
}).start();
}
}
}
5、
package Demo03BS;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket server= new ServerSocket(9999);
Socket socket=server.accept();
InputStream is=socket.getInputStream();
byte[] bytes=new byte[1024];
int len=0;
//while((len= is.read(bytes))!=-1) {
//System.out.println(new String(bytes,0,len));
//}
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line= br.readLine();
String [] arr= line.split(" ");
String htmlpath =arr[1];
FileInputStream fis= new FileInputStream(System.getProperty("user.dir"+htmlpath));
OutputStream os= socket.getOutputStream();
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
fis.close();
socket.close();
server.close();
}
}
6、
package Demo03BS;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerThread {
public static void main(String[] args) throws IOException {
ServerSocket server= new ServerSocket(9999);
while(true) {
Socket socket=server.accept();
new Thread(new Runnable() {
public void run() {
try {
InputStream is=socket.getInputStream();
byte[] bytes=new byte[1024];
int len=0;
//while((len= is.read(bytes))!=-1) {
//System.out.println(new String(bytes,0,len));
//}
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line= br.readLine();
String [] arr= line.split(" ");
String htmlpath =arr[1];
FileInputStream fis= new FileInputStream(System.getProperty("user.dir"+htmlpath));
OutputStream os= socket.getOutputStream();
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
fis.close();
socket.close();
}catch(IOException e) {
System.out.println(e);
}
}
}).start();
}
}
}