网络编程
模拟web服务器
文件上传
代码如下:
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(“C:\du\yu.yu.jpg”);
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();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210616121853804.png)
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();
InputStream is=socket.getInputStream();
//
File file= new File("D:\\tst");
if(!file.exists()) {
file.mkdirs();
}
FileOutputStream fos=new FileOutputStream(file+"\\1.jfif");
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();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210616121858142.png)
优化:
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 {
//1、创建一个服务器的serversocket 对象
ServerSocket server= new ServerSocket(8887);
while(true) {
Socket socket=server.accept();
new Thread (new Runnable() {
public void run() {
try {
InputStream is=socket.getInputStream();
File file= new File("D:\\tst");
if(!file.exists()) {
file.mkdirs();
}
String fileName="classFive"+System.currentTimeMillis()+new Random().nextInt(99999)+".jfif";
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();
//2、等待客户端连接
}catch(IOException e) {
System.out.println(e);
}
}
}).start();
}
}
}
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("C:\\du\\yu.yu.jpg");
Socket socket =new Socket("127.0.0.1",8887);
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();
}
}