Advertisement
Guest User

FileServer

a guest
Jun 27th, 2011
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class FileServer {
  5.     static ServerSocket servSock;
  6.     static Socket initSocket;
  7.     static String fileSize = "";
  8.     static File myFile = new File("attachment.pdf");
  9.     static byte [] byteArr;
  10.     static FileInputStream fis;
  11.     static BufferedInputStream bis;
  12.     static OutputStream os;
  13.     static final int MAX_THREAD = 3;
  14.     static String[] startAndEnd;
  15.     public static void main(String[] args) throws IOException {
  16.  
  17.         System.out.println("Waiting...");
  18.         if (!initSocket())
  19.             return;
  20.         if (!sendFileSize())
  21.             return;
  22.         if(!initFile())
  23.             return;
  24.        
  25.         while(true){
  26.             listenRecieveConnection();
  27.             receiveOffsetFile();
  28.             sendFile();
  29.         }
  30.            
  31.     }
  32.  
  33.     public static boolean initSocket() {
  34.         try {
  35.             servSock = new ServerSocket(13267);
  36.         } catch (IOException e) {
  37.             System.out.println(e.getLocalizedMessage());
  38.             disconnected();
  39.             return false;
  40.         }
  41.         try {
  42.             initSocket = servSock.accept();
  43.         } catch (IOException e) {
  44.             System.out.println(e.getLocalizedMessage());
  45.             disconnected();
  46.             return false;
  47.         }
  48.         System.out.println("Accepted connection : " + initSocket);
  49.         return true;
  50.     }
  51.    
  52.     public static boolean initFile() {
  53.          try {
  54.             fis = new FileInputStream(myFile);
  55.             bis = new BufferedInputStream(fis);
  56.             byteArr = new byte [(int)myFile.length()];
  57.             bis.read(byteArr,0,byteArr.length);
  58.         } catch (IOException e) {
  59.             System.out.println(e.getLocalizedMessage());
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     public static boolean sendFileSize() {
  66.         fileSize += myFile.length() + "\n";
  67.         try {
  68.             os = initSocket.getOutputStream();
  69.             os.write(fileSize.getBytes(),0,fileSize.getBytes().length);
  70.             os.flush();
  71.         } catch (IOException e) {
  72.             System.out.println(e.getLocalizedMessage());
  73.             disconnected();
  74.             return false;
  75.         }
  76.         return true;
  77.  
  78.     }
  79.  
  80.     public static void disconnected() {
  81.         try {
  82.             servSock.close();
  83.         } catch (IOException e) {
  84.             e.printStackTrace();
  85.         }
  86.     }
  87.  
  88.     public static boolean listenRecieveConnection() {
  89.         try {
  90.             initSocket = servSock.accept();
  91.         } catch (IOException e) {
  92.             System.out.println(e.getLocalizedMessage());
  93.             disconnected();
  94.             return false;
  95.         }
  96.         System.out.println("Accepted connection : " + initSocket);
  97.         return true;
  98.     }
  99.    
  100.     public static boolean receiveOffsetFile() {
  101.         try {
  102.             BufferedReader in = new BufferedReader(new InputStreamReader(initSocket.getInputStream()));
  103.             String line =in.readLine();
  104.             startAndEnd = line.split(" ");
  105.         } catch (IOException e) {
  106.             System.out.println(e.getLocalizedMessage());
  107.             disconnected();
  108.             return false;
  109.         }
  110.         return true;
  111.     }
  112.    
  113.     public static boolean sendFile() {
  114.         int start = Integer.parseInt(startAndEnd[0]) - 1;
  115.         int end = Integer.parseInt(startAndEnd[1]) - 1;
  116.         int size = (end - start) + 1;
  117.         try {
  118.             os = initSocket.getOutputStream();
  119.             os.write(byteArr,start,size);
  120.             os.flush();
  121.             System.out.println("Send file to : " + initSocket);
  122.         } catch (IOException e) {
  123.             System.out.println(e.getLocalizedMessage());
  124.             disconnected();
  125.             return false;
  126.         }
  127.         return true;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement