日期:2014-05-20 浏览次数:20790 次
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.net.ServerSocket; import java.net.Socket; public class Server { private ServerSocket serverSocket = null; private Socket socket = null; public void getFile(int port) { try { serverSocket = new ServerSocket(port); System.out.println("server start...."); while (true) { socket = serverSocket.accept(); System.out.println(socket.getInetAddress() + " is connected the server..."); Thread serverThread = new ServerThread(socket); serverThread.start(); } } catch (IOException e) { e.printStackTrace(); } } class ServerThread extends Thread { private Socket socket; public ServerThread(Socket socket) { this.socket = socket; } public void run() { File file = null; DataInputStream in = null; DataOutputStream out = null; try { in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); if (in.readChar() == '?') { downloadFile(in, out, file); } else { uploadFile(in, out, file); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); if (out != null) out.close(); if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void uploadFile(DataInputStream in, DataOutputStream out, File file) { try { String fileName = null; StringBuffer stringBuffer = new StringBuffer(); char c = '\\'; while ((c = in.readChar()) != '\\') { stringBuffer.append(c); } fileName=stringBuffer.toString().substring(0, stringBuffer.length()); file = new File("e:\\back\\" + fileName); long pointer=in.readLong(); out.writeLong(file.length()); if(file.length()==0){ out.write(-1); }else if(file.length()-pointer > -1){ in = new DataInputStream(new FileInputStream(file)); int i = -1; byte[] buffer = new byte[1024*512]; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); pointer+=i; } out.write(-1); }else{ out.write(-1); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); if (out != null) out.close(); if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void downloadFile(DataInputStream in, DataOutputStream out, File file) { try { String fileName = null; StringBuffer stringBuffer = new StringBuffer(); char c = '\\'; while ((c = in.readChar()) != '\\') { stringBuffer.append(c); } fileName = socket.getInetAddress() + "-" + stringBuffer.toString(); file = new File("e:\\back\\" + fileName); out.writeLong(file.length()); out = new DataOutputStream(new FileOutputStream(file, true)); int i = -1; byte[] buffer = new byte[1024]; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); if (out != null) out.close(); if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } } }