Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件上傳案例示例代碼
Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件上傳案例
實(shí)現(xiàn)流程:
1.客戶端從硬盤讀取文件數(shù)據(jù)到程序中
2.客戶端輸出流,寫出文件到服務(wù)端
3.服務(wù)端輸出流,讀取文件數(shù)據(jù)到服務(wù)端中
4.輸出流,寫出文件數(shù)據(jù)到服務(wù)器硬盤中

下面上代碼
上傳單個(gè)文件
服務(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ù)器端啟動(dòng)");
//創(chuàng)建一個(gè)服務(wù)器端對(duì)象
ServerSocket serverSocket = new ServerSocket(8888);
//使用accept獲取socket對(duì)象
Socket accept = serverSocket.accept();
//使用字節(jié)輸入流讀取
InputStream inputStream = accept.getInputStream();
//創(chuàng)建一個(gè)字節(jié)輸出流輸出到本地
FileOutputStream fileOutputStream = new FileOutputStream("F:\\this\\copy1.jpg",true);
//創(chuàng)建一個(gè)數(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)建一個(gè)Socket對(duì)象
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();
}
//由于不會(huì)寫入-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)建一個(gè)Socket對(duì)象
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();
}
//由于不會(huì)寫入-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ù)器端啟動(dòng)");
//創(chuàng)建一個(gè)服務(wù)器端對(duì)象
ServerSocket serverSocket = new ServerSocket(8888);
//使用while()持續(xù)寫入數(shù)據(jù)
while(true){
//使用accept獲取socket對(duì)象
Socket accept = serverSocket.accept();
//Socket對(duì)象交給子線程處理,進(jìn)行讀寫操作,
new Thread(() ->{
{
//使用字節(jié)輸入流讀取
InputStream inputStream = null;
try {
//文件名
String name = new String("F:\\this\\"+ System.currentTimeMillis()+"copy1.jpg" );
inputStream = accept.getInputStream();
//創(chuàng)建一個(gè)字節(jié)輸出流輸出到本地
FileOutputStream fileOutputStream = new FileOutputStream(name,true);
//創(chuàng)建一個(gè)數(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)輸入無非就是增加了一個(gè)while循環(huán)與一點(diǎn)多線程的知識(shí),以上就是一個(gè)文件上傳的一個(gè)簡(jiǎn)單案例,
到此這篇關(guān)于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件上傳案例示例代碼的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)文件上傳案例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java 網(wǎng)絡(luò)編程之TCP通信和簡(jiǎn)單的文件上傳功能實(shí)例
- Java8實(shí)現(xiàn)FTP及SFTP文件上傳下載
- JavaWeb如何實(shí)現(xiàn)本地文件上傳功能
- java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽
- 簡(jiǎn)單操作實(shí)現(xiàn)Java jsp servlet文件上傳過程解析
- java文件上傳下載代碼實(shí)例
- Java文件上傳與文件下載實(shí)現(xiàn)方法詳解
- java實(shí)現(xiàn)文件上傳下載
- java 文件上傳到讀取文件內(nèi)容的實(shí)例
- java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器
- Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳
相關(guān)文章
Java多線程中的ThreadPoolExecutor使用解析
這篇文章主要介紹了Java多線程中的ThreadPoolExecutor使用解析,作為線程池的緩沖,當(dāng)新增線程超過maximumPoolSize時(shí),會(huì)將新增線程暫時(shí)存放到該隊(duì)列中,需要的朋友可以參考下2023-12-12
Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過長(zhǎng)的解決
這篇文章主要介紹了Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過長(zhǎng)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
簡(jiǎn)單談?wù)凧ava遍歷樹深度優(yōu)先和廣度優(yōu)先的操作方式

