Java實現(xiàn)一個簡單的文件上傳案例示例代碼
Java實現(xiàn)一個簡單的文件上傳案例
實現(xiàn)流程:
1.客戶端從硬盤讀取文件數(shù)據(jù)到程序中
2.客戶端輸出流,寫出文件到服務(wù)端
3.服務(wù)端輸出流,讀取文件數(shù)據(jù)到服務(wù)端中
4.輸出流,寫出文件數(shù)據(jù)到服務(wù)器硬盤中
下面上代碼
上傳單個文件
服務(wù)器端
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("服務(wù)器端啟動"); //創(chuàng)建一個服務(wù)器端對象 ServerSocket serverSocket = new ServerSocket(8888); //使用accept獲取socket對象 Socket accept = serverSocket.accept(); //使用字節(jié)輸入流讀取 InputStream inputStream = accept.getInputStream(); //創(chuàng)建一個字節(jié)輸出流輸出到本地 FileOutputStream fileOutputStream = new FileOutputStream("F:\\this\\copy1.jpg",true); //創(chuàng)建一個數(shù)組循環(huán)讀取 byte[] bytes = new byte[1024]; int len; while ((len=inputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,len); } System.out.println("執(zhí)行完畢"); 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 { //創(chuàng)建一個Socket對象 Socket socket = new Socket("127.0.0.1", 8888); //讀取本地文件 FileInputStream fileInputStream = new FileInputStream("F:\\1.jpeg"); //獲取輸出流向服務(wù)器寫入數(shù)據(jù) OutputStream outputStream = socket.getOutputStream(); //創(chuàng)建數(shù)組讀取 byte[] bytes = new byte[1024]; int len; //邊都邊寫 while((len=fileInputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); outputStream.flush(); } //由于不會寫入-1所以調(diào)用socket的shutdownOutput方法把前面的數(shù)據(jù)都寫入并且正常終止后面的序列 socket.shutdownOutput(); System.out.println("文件發(fā)送完畢"); fileInputStream.close(); outputStream.close(); socket.close(); } }
循環(huán)上傳
客戶端代碼
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 { //創(chuàng)建一個Socket對象 Socket socket = new Socket("127.0.0.1", 8888); //讀取本地文件 FileInputStream fileInputStream = new FileInputStream("F:\\1.jpeg"); //獲取輸出流向服務(wù)器寫入數(shù)據(jù) OutputStream outputStream = socket.getOutputStream(); //創(chuàng)建數(shù)組讀取 byte[] bytes = new byte[1024]; int len; //邊都邊寫 while((len=fileInputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); outputStream.flush(); } //由于不會寫入-1所以調(diào)用socket的shutdownOutput方法把前面的數(shù)據(jù)都寫入并且正常終止后面的序列 socket.shutdownOutput(); System.out.println("文件發(fā)送完畢"); fileInputStream.close(); outputStream.close(); socket.close(); } }
服務(wù)器端代碼
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("服務(wù)器端啟動"); //創(chuàng)建一個服務(wù)器端對象 ServerSocket serverSocket = new ServerSocket(8888); //使用while()持續(xù)寫入數(shù)據(jù) while(true){ //使用accept獲取socket對象 Socket accept = serverSocket.accept(); //Socket對象交給子線程處理,進行讀寫操作, new Thread(() ->{ { //使用字節(jié)輸入流讀取 InputStream inputStream = null; try { //文件名 String name = new String("F:\\this\\"+ System.currentTimeMillis()+"copy1.jpg" ); inputStream = accept.getInputStream(); //創(chuàng)建一個字節(jié)輸出流輸出到本地 FileOutputStream fileOutputStream = new FileOutputStream(name,true); //創(chuàng)建一個數(shù)組循環(huán)讀取 byte[] bytes = new byte[1024]; int len; while ((len=inputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,len); } System.out.println("執(zhí)行完畢"); fileOutputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } } }
循環(huán)輸入無非就是增加了一個while循環(huán)與一點多線程的知識,以上就是一個文件上傳的一個簡單案例,
到此這篇關(guān)于Java實現(xiàn)一個簡單的文件上傳案例示例代碼的文章就介紹到這了,更多相關(guān)Java實現(xiàn)文件上傳案例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程中的ThreadPoolExecutor使用解析
這篇文章主要介紹了Java多線程中的ThreadPoolExecutor使用解析,作為線程池的緩沖,當新增線程超過maximumPoolSize時,會將新增線程暫時存放到該隊列中,需要的朋友可以參考下2023-12-12Mybatis-Plus自動生成的數(shù)據(jù)庫id過長的解決
這篇文章主要介紹了Mybatis-Plus自動生成的數(shù)據(jù)庫id過長的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12簡單談?wù)凧ava遍歷樹深度優(yōu)先和廣度優(yōu)先的操作方式
這篇文章主要介紹了簡單談?wù)凧ava遍歷樹深度優(yōu)先和廣度優(yōu)先的操作方式的相關(guān)資料,需要的朋友可以參考下2023-03-03