package com.main;
|
import java.io.DataInputStream;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.math.RoundingMode;
|
import java.net.ServerSocket;
|
import java.net.Socket;
|
import java.text.DecimalFormat;
|
|
/**
|
* Îļþ´«ÊäServer¶Ë<br>
|
* ¹¦ÄÜ˵Ã÷£º
|
*
|
* @author ³ÐÇΰ
|
* @Date 2019Äê02ÔÂ14ÈÕ
|
* @version 1.0
|
*/
|
public class FileTransferServer extends ServerSocket {
|
|
private static final int SERVER_PORT = 8899; // ·þÎñ¶Ë¶Ë¿Ú
|
|
private static DecimalFormat df = null;
|
|
static {
|
// ÉèÖÃÊý×Ö¸ñʽ£¬±£ÁôһλÓÐЧСÊý
|
df = new DecimalFormat("#0.0");
|
df.setRoundingMode(RoundingMode.HALF_UP);
|
df.setMinimumFractionDigits(1);
|
df.setMaximumFractionDigits(1);
|
}
|
|
public FileTransferServer() throws Exception {
|
super(SERVER_PORT);
|
}
|
|
/**
|
* ʹÓÃÏ̴߳¦Àíÿ¸ö¿Í»§¶Ë´«ÊäµÄÎļþ
|
* @throws Exception
|
*/
|
public void load() throws Exception {
|
while (true) {
|
// server³¢ÊÔ½ÓÊÕÆäËûSocketµÄÁ¬½ÓÇëÇó£¬serverµÄaccept·½·¨ÊÇ×èÈûʽµÄ
|
Socket socket = this.accept();
|
/**
|
* ÎÒÃǵķþÎñ¶Ë´¦Àí¿Í»§¶ËµÄÁ¬½ÓÇëÇóÊÇͬ²½½øÐеģ¬ ÿ´Î½ÓÊÕµ½À´×Ô¿Í»§¶ËµÄÁ¬½ÓÇëÇóºó£¬
|
* ¶¼ÒªÏȸúµ±Ç°µÄ¿Í»§¶ËͨÐÅÍêÖ®ºó²ÅÄÜÔÙ´¦ÀíÏÂÒ»¸öÁ¬½ÓÇëÇó¡£ ÕâÔÚ²¢·¢±È½Ï¶àµÄÇé¿öÏ»áÑÏÖØÓ°Ïì³ÌÐòµÄÐÔÄÜ£¬
|
* Ϊ´Ë£¬ÎÒÃÇ¿ÉÒÔ°ÑËü¸ÄΪÈçÏÂÕâÖÖÒì²½´¦ÀíÓë¿Í»§¶ËͨÐŵķ½Ê½
|
*/
|
// ÿ½ÓÊÕµ½Ò»¸öSocket¾Í½¨Á¢Ò»¸öеÄÏß³ÌÀ´´¦ÀíËü
|
new Thread(new Task(socket)).start();
|
}
|
}
|
|
/**
|
* ´¦Àí¿Í»§¶Ë´«Êä¹ýÀ´µÄÎļþÏß³ÌÀà
|
*/
|
class Task implements Runnable {
|
|
private Socket socket;
|
|
private DataInputStream dis;
|
|
private FileOutputStream fos;
|
|
public Task(Socket socket) {
|
this.socket = socket;
|
}
|
|
@Override
|
public void run() {
|
try {
|
dis = new DataInputStream(socket.getInputStream());
|
|
// ÎļþÃûºÍ³¤¶È
|
String fileName = dis.readUTF();
|
long fileLength = dis.readLong();
|
File directory = new File("D:\\FTCache");
|
if(!directory.exists()) {
|
directory.mkdir();
|
}
|
File file = new File(directory.getAbsolutePath() + File.separatorChar + fileName);
|
fos = new FileOutputStream(file);
|
|
// ¿ªÊ¼½ÓÊÕÎļþ
|
byte[] bytes = new byte[1024];
|
int length = 0;
|
while((length = dis.read(bytes, 0, bytes.length)) != -1) {
|
fos.write(bytes, 0, length);
|
fos.flush();
|
}
|
System.out.println("======== Îļþ½ÓÊճɹ¦ [File Name£º" + fileName + "] [Size£º" + getFormatFileSize(fileLength) + "] ========");
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if(fos != null)
|
fos.close();
|
if(dis != null)
|
dis.close();
|
socket.close();
|
} catch (Exception e) {}
|
}
|
}
|
}
|
|
/**
|
* ¸ñʽ»¯Îļþ´óС
|
* @param length
|
* @return
|
*/
|
private String getFormatFileSize(long length) {
|
double size = ((double) length) / (1 << 30);
|
if(size >= 1) {
|
return df.format(size) + "GB";
|
}
|
size = ((double) length) / (1 << 20);
|
if(size >= 1) {
|
return df.format(size) + "MB";
|
}
|
size = ((double) length) / (1 << 10);
|
if(size >= 1) {
|
return df.format(size) + "KB";
|
}
|
return length + "B";
|
}
|
|
/**
|
* Èë¿Ú
|
* @param args
|
*/
|
public static void main(String[] args) {
|
try {
|
FileTransferServer server = new FileTransferServer(); // Æô¶¯·þÎñ¶Ë
|
server.load();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|