1、文件上传案例
上传分析图解
[客户端]输入流,从硬盘读取文件数据到程序中。
- [客户端] 输出流,写出文件数据到服务端。
- [服务端]输入流,读取文件数据到服务端程序。
- [服务端]输出流,写出文件数据到服务器硬盘中。
- 创建一个本地字节收入流FileInputStream 对象,构造方法中绑定读取数据的数据源。
- 创建一个socket 对象,构造方法中绑定服务器的IP地址和端口号。
-
使用socket中的方法getoutputstream 获取网络字节输出流。
使用本地字节输入流中的read方法,把读取的文件上传到服务器。
使用网络输出流中的write方法,读取本地文件。
使用socket 中方法,获取输入流对象
通过网络输入流对象中的read 方法,读取回写数据
释放资源
package Demo01FileUpload;import java.io.File;
import java.io.FileNotFoundException;
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 {
ServerSocket server = new ServerSocket(8101);
Socket socket = server.accept();
InputStream is = socket.getInputStream();
File file = new File("D:\\Java\\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();
}
}
客户端实现:
package Demo01FileUpload;import java.io.FileInputStream;
public class TcpClient {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\Java\\01-端口号.bmp");
Socket socket = new Socket("127.0.0.1",8101);
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、文件上传优化分析
文件名称写死问题
循环接收的问题
效率问题
优化实现
运行代码如下:
serverpackage Demo02FileUpload;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;package Demo02FileUpload;
import java.io.FileInputStream;
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:\\Java\\01-端口号.bmp");
Socket socket = new Socket("127.0.0.1",8800);
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();
}
}模拟B\S服务器
模拟网站服务器,使用浏览器访问自己编写的服务端程序,查看网页效果。准备网页数据,web文件夹
模拟服务端,ServerSocket 类监听窗口,使用浏览器访问。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(5050);
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);
}
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);
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();
}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(5050);
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);
}
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();
}
}
} -
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8800);
while(true) {
Socket socket = server.accept();
new Thread(new Runnable() {
public void run() {
try {
InputStream is = socket.getInputStream();
//
File file = new File("D:\\Java\\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();
}
}
}