Java實現一個簡單的文件上傳案例

Java實現一個簡單的文件上傳案例

實現流程:
1.客戶端從硬盤讀取文件數據到程序中
2.客戶端輸出流,寫出文件到服務端
3.服務端輸出流,讀取文件數據到服務端中
4.輸出流,寫出文件數據到服務器硬盤中

在這裡插入圖片描述
下面上代碼

上傳單個文件

服務器端

package FileUpload;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("服務器端啟動");
        //創建一個服務器端對象
        ServerSocket serverSocket = new ServerSocket(8888);
        //使用accept獲取socket對象
        Socket accept = serverSocket.accept();
        //使用位元組輸入流讀取
        InputStream inputStream = accept.getInputStream();
        //創建一個位元組輸出流輸出到本地
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\this\\copy1.jpg",true);
        //創建一個數組循環讀取
        byte[] bytes = new byte[1024];
        int len;
        while ((len=inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }
        System.out.println("執行完畢");
       fileOutputStream.close();
       inputStream.close();

    }
}

客戶端

package FileUpload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
    public static void main(String[] args) throws IOException {
        //創建一個Socket對象
        Socket socket = new Socket("127.0.0.1", 8888);
        //讀取本地文件
        FileInputStream fileInputStream = new FileInputStream("F:\\1.jpeg");
        //獲取輸出流向服務器寫入數據
        OutputStream outputStream = socket.getOutputStream();
        //創建數組讀取
        byte[] bytes = new byte[1024];
        int len;
        //邊都邊寫
        while((len=fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
            outputStream.flush();
        }
        //由於不會寫入-1所以調用socket的shutdownOutput方法把前面的數據都寫入並且正常終止後面的序列
        socket.shutdownOutput();
        System.out.println("文件發送完畢");
        fileInputStream.close();
        outputStream.close();
        socket.close();
    }
}

循環上傳

客戶端代碼

package FileUpload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
    public static void main(String[] args) throws IOException {
        //創建一個Socket對象
        Socket socket = new Socket("127.0.0.1", 8888);
        //讀取本地文件
        FileInputStream fileInputStream = new FileInputStream("F:\\1.jpeg");
        //獲取輸出流向服務器寫入數據
        OutputStream outputStream = socket.getOutputStream();
        //創建數組讀取
        byte[] bytes = new byte[1024];
        int len;
        //邊都邊寫
        while((len=fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
            outputStream.flush();
        }
        //由於不會寫入-1所以調用socket的shutdownOutput方法把前面的數據都寫入並且正常終止後面的序列
        socket.shutdownOutput();
        System.out.println("文件發送完畢");
        fileInputStream.close();
        outputStream.close();
        socket.close();
    }
}

服務器端代碼

package FileUpload;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.InputStream;
        import java.net.ServerSocket;
        import java.net.Socket;
public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("服務器端啟動");
        //創建一個服務器端對象
        ServerSocket serverSocket = new ServerSocket(8888);
        //使用while()持續寫入數據
        while(true){
            //使用accept獲取socket對象
            Socket accept = serverSocket.accept();
            //Socket對象交給子線程處理,進行讀寫操作,
            new Thread(() ->{
               {
                    //使用位元組輸入流讀取
                    InputStream inputStream = null;
                    try {
                        //文件名
                        String name = new String("F:\\this\\"+ System.currentTimeMillis()+"copy1.jpg" );
                        inputStream = accept.getInputStream();
                        //創建一個位元組輸出流輸出到本地
                        FileOutputStream fileOutputStream = new FileOutputStream(name,true);
                        //創建一個數組循環讀取
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len=inputStream.read(bytes))!=-1){
                            fileOutputStream.write(bytes,0,len);
                        }
                        System.out.println("執行完畢");
                        fileOutputStream.close();
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }
}

循環輸入無非就是增加了一個while循環與一點多線程的知識,以上就是一個文件上傳的一個簡單案例,如有錯誤還請各位批評指正,喜歡我的可以點贊收藏,我會不定期更新文章,喜歡的也可以關注呀

在這裡插入圖片描述