1. 程式人生 > 實用技巧 >Java實現一個簡單的檔案上傳案例

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迴圈與一點多執行緒的知識,以上就是一個檔案上傳的一個簡單案例,如有錯誤還請各位批評指正,喜歡我的可以點贊收藏,我會不定期更新文章,喜歡的也可以關注呀