java socket實現檔案的上傳和下載試例
阿新 • • 發佈:2019-01-24
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientScoket {
//客戶端
private String ip = "localhost";// 設定成伺服器IP
private int port = 8822;//設定埠號
public void UpFile(String filePath){
//上傳檔案,將本地檔案傳輸到伺服器端
try {
Socket socket = new Socket(ip,port);
while (true) {
// 選擇進行傳輸的檔案
File fi = new File(filePath);
System.out.println("檔案長度:" + (int) fi.length());
/* DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dis.readByte();
*/
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(socket.getOutputStream());
//將檔名及長度傳給客戶端。這裡要真正適用所有平臺,例如中文名的處理,還需要加工,具體可以參見Think In Java 4th裡有現成的程式碼。
ps.writeUTF(fi.getName());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// 注意關閉socket連結哦,不然客戶端會等待server的資料過來,
// 直到socket超時,導致資料不完整。
fis.close();
socket.close();
System.out.println("檔案傳輸完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void DownFile(){
//從伺服器端下載檔案
try {
Socket socket = new Socket(ip,port);
while(true){
DataInputStream is = new DataInputStream(socket.getInputStream());
OutputStream os = socket.getOutputStream();
//1、得到檔名
String filename="E:\\";
filename += is.readUTF();
System.out.println("新生成的檔名為:"+filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] b = new byte[1024];
int length = 0;
while((length=is.read(b))!=-1){
//2、把socket輸入流寫到檔案輸出流中去
fos.write(b, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String arg[]) {
String filepath="D:\\test.txt";
new ClientScoket().UpFile(filepath);;
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientScoket {
//客戶端
private String ip = "localhost";// 設定成伺服器IP
private int port = 8822;//設定埠號
public void UpFile(String filePath){
//上傳檔案,將本地檔案傳輸到伺服器端
try {
Socket socket = new Socket(ip,port);
while (true) {
// 選擇進行傳輸的檔案
File fi = new File(filePath);
System.out.println("檔案長度:" + (int) fi.length());
/* DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dis.readByte();
*/
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(socket.getOutputStream());
//將檔名及長度傳給客戶端。這裡要真正適用所有平臺,例如中文名的處理,還需要加工,具體可以參見Think In Java 4th裡有現成的程式碼。
ps.writeUTF(fi.getName());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// 注意關閉socket連結哦,不然客戶端會等待server的資料過來,
// 直到socket超時,導致資料不完整。
fis.close();
socket.close();
System.out.println("檔案傳輸完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void DownFile(){
//從伺服器端下載檔案
try {
Socket socket = new Socket(ip,port);
while(true){
DataInputStream is = new DataInputStream(socket.getInputStream());
OutputStream os = socket.getOutputStream();
//1、得到檔名
String filename="E:\\";
filename += is.readUTF();
System.out.println("新生成的檔名為:"+filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] b = new byte[1024];
int length = 0;
while((length=is.read(b))!=-1){
//2、把socket輸入流寫到檔案輸出流中去
fos.write(b, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String arg[]) {
String filepath="D:\\test.txt";
new ClientScoket().UpFile(filepath);;
}
}