客æ·ç«¯ä»£ç å¦ä¸ï¼
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* æ件åé客æ·ç«¯ä¸»ç¨åº
* @author admin_Hzw
*
*/
public class BxClient {
/**
* ç¨åºmainæ¹æ³
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
int length = 0;
double sumL = 0 ;
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
FileInputStream fis = null;
boolean bool = false;
try {
File file = new File("D:/天å.zip"); //è¦ä¼ è¾çæ件路å¾
long l = file.length();
socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1", 48123));
dos = new DataOutputStream(socket.getOutputStream());
fis = new FileInputStream(file);
sendBytes = new byte[1024];
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
sumL += length;
System.out.println("å·²ä¼ è¾ï¼"+((sumL/l)*100)+"%");
dos.write(sendBytes, 0, length);
dos.flush();
}
//è½ç¶æ°æ®ç±»åä¸åï¼ä½JAVAä¼èªå¨è½¬æ¢æç¸åæ°æ®ç±»ååå¨åæ¯è¾
if(sumL==l){
bool = true;
}
}catch (Exception e) {
System.out.println("客æ·ç«¯æä»¶ä¼ è¾å¼å¸¸");
bool = false;
e.printStackTrace();
} finally{
if (dos != null)
dos.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
}
System.out.println(bool?"æå":"失败");
}
}
æå¡ç«¯ä»£ç å¦ä¸ï¼
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import com.boxun.util.GetDate;
/**
* æ¥æ¶æ件æå¡
* @author admin_Hzw
*
*/
public class BxServerSocket {
/**
* å·¥ç¨mainæ¹æ³
* @param args
*/
public static void main(String[] args) {
try {
final ServerSocket server = new ServerSocket(48123);
Thread th = new Thread(new Runnable() {
public void run() {
while (true) {
try {
System.out.println("å¼å§çå¬...");
/*
* å¦æ没æ访é®å®ä¼èªå¨çå¾
*/
Socket socket = server.accept();
System.out.println("æé¾æ¥");
receiveFile(socket);
} catch (Exception e) {
System.out.println("æå¡å¨å¼å¸¸");
e.printStackTrace();
}
}
}
});
th.run(); //å¯å¨çº¿ç¨è¿è¡
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
}
/**
* æ¥æ¶æ件æ¹æ³
* @param socket
* @throws IOException
*/
public static void receiveFile(Socket socket) throws IOException {
byte[] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
String filePath = "D:/temp/"+GetDate.getDate()+"SJ"+new Random().nextInt(10000)+".zip";
try {
try {
dis = new DataInputStream(socket.getInputStream());
File f = new File("D:/temp");
if(!f.exists()){
f.mkdir();
}
/*
* æ件åå¨ä½ç½®
*/
fos = new FileOutputStream(new File(filePath));
inputByte = new byte[1024];
System.out.println("å¼å§æ¥æ¶æ°æ®...");
while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
fos.write(inputByte, 0, length);
fos.flush();
}
System.out.println("å®ææ¥æ¶ï¼"+filePath);
} finally {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}