文件上传
项目思路
-
客户端用文件输入流找到文件,再用输出流传给服务端
-
服务端用输入流接收数据,再用文件输出流保存到磁盘中
-
服务端接收完文件后,发信息给客户端告诉他接收完毕
-
客户端确认一下
注意事项
-
accept是阻塞式监听
-
scoket.shutdonwOutput():确认输出完毕,让其他人执行代码
-
文件输出流默认将文件输出到当前目录下
-
尽量一个问题对应一个捕获
-
如果一个捕获中同时有两行代码可能出问题,并且问题相同
-
一旦出错就不好知道是哪行代码出了问题
-
把异常打印出来,直接知道怎么回事
客户端
package com.li.changGe.fileUpload;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* 用文件输入流上传文件
*
* 输出完告诉服务端我结束了
*
* 确认机制
*/
public class Client {
public static void main(String[] args) {
new Client().upload();
}
public void upload() {
InetAddress inetAddress = null;
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
inetAddress = InetAddress.getByName("127.0.0.1");
} catch (Exception e) {
System.out.println("请确认ip地址是否正确");
}
try {
socket = new Socket(inetAddress, 8080);
} catch (IOException e) {
System.out.println("请注意ip地址和端口号");
}
try {
outputStream = socket.getOutputStream();
} catch (IOException e) {
System.out.println("socket可能存在问题");
}
//-------------------------------------------------------------------
try {
fileInputStream = new FileInputStream("E:\\JAVA\\JavaSE\\10网络编程\\李长歌.jpg");
} catch (IOException e) {
System.out.println("文件路径可能存在问题2");
}
byte[] bytes = new byte[1024];
int length;
try {
while ((length = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
}
} catch (IOException e) {
System.out.println("没有读取到文件1");
}
System.out.println("文件上传完成");
try {
/**
* 关掉输出,
*
* 告诉服务端我的业务暂时结束了,你可以执行自己的业务了
*/
socket.shutdownOutput();
} catch (IOException e) {
System.out.println("输出关闭失败");
}
//---------------------------------------------------------------
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
System.out.println("创建输入流失败");
}
byte[] beefier = new byte[1024];
try {
/**
* 尽量一个问题一个捕获
*
* 当两行代码都有可能出问题时,问题又一样
* 一个捕获就不能直接知道是哪一行代码出问题
*/
while((length = inputStream.read(beefier)) != -1){
byteArrayOutputStream.write(beefier,0,length);
System.out.println(byteArrayOutputStream.toString());
}
}catch (Exception e){
/**
* 把异常打印出来
* 知道怎么回事
*/
System.out.println("读取信息失败");
e.printStackTrace();
}
if(byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
}catch (Exception e){
System.out.println("管道流关闭错误");
}
}
if(inputStream != null){
try {
inputStream.close();
}catch (Exception e){
System.out.println("输入流关闭错误");
}
}
if(fileInputStream != null){
try {
fileInputStream.close();
}catch (Exception e){
System.out.println("文件输入流关闭错误");
}
}
if(outputStream != null){
try {
outputStream.close();
}catch (Exception e){
System.out.println("输出流关闭错误");
}
}
if(socket != null){
try {
socket.close();
}catch (Exception e){
System.out.println("套接字关闭错误");
}
}
}//upload
}
服务端
package com.li.changGe.fileUpload;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
/**
* 用文件输出流读取文件
*
* 接收完告诉客户端我结束了
*
* 确认机制
*
* 文件输出流默认输出到当前路径下
*/
public class Service {
public static void main(String[] args) {
new Service().download();
}
public void download(){
ServerSocket serverSocket = null;
InetAddress inetAddress = null;
Socket socket = null;
OutputStream outputStream = null;
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try {
serverSocket = new ServerSocket(8080);
/**
* accept阻塞式监听
*/
socket = serverSocket.accept();
inputStream = socket.getInputStream();
fileOutputStream = new FileOutputStream("E:\\JAVA\\JavaSE\\10网络编程\\LiChangGe.jpg");
byte[] bytes = new byte[1024];
int length;
while ((length = inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes,0,length);
}
/**
* 这里不用停顿
* 接收完文件直接就给客户端发信息了
*/
//socket.shutdownInput();
outputStream = socket.getOutputStream();
outputStream.write("文件接收完毕".getBytes(StandardCharsets.UTF_8));
}catch (Exception e){
System.out.println("请检查端口号,网络环境");
e.printStackTrace();
}finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
System.out.println("输出流关闭错误");
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
System.out.println("文件输入流关闭错误");
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
System.out.println("输入流关闭错误");
}
}
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
System.out.println("套接字关闭错误");
}
}
}//finally
}
}