Код сервера


Создать и редактировать файл сервера можно в любом текстовом редакторе. Файл необходимо сохранить с расширением java.



import java.io.*;
import java.net.*;

public class ServerPort {
    public static int PORT = 0;
    public static void main(String[] args) throws IOException {
        File saveDir = new File("/WebDVR");
        if (!saveDir.exists())
            saveDir.mkdirs();

        PORT = Integer.parseInt(args[0]);

        ServerSocket serverSocket = new ServerSocket(PORT);
        System.out.println("Waiting. Port: " + args[0] + " ...");

        try {
            while(true) {
                Socket socket = serverSocket.accept();
                try {
                    new ServeOneJabber(socket);
                } catch(IOException e) {
                    socket.close();
                }
            }
        } finally {
            serverSocket.close();
        }
    }
}

class ServeOneJabber extends Thread {
    private Socket socket;
    private InputStream inputStream;
    private DataInputStream dataInputStream;
    private FileOutputStream fileOutputStream;
    private String fileName, folderName;
    private int size, fileNameFormat;

    public ServeOneJabber(Socket s) throws IOException {
        socket = s;
        inputStream = socket.getInputStream();
        dataInputStream = new DataInputStream(inputStream);
        start();
    }

    public void run() {
        try {
            while (true) {
                //String string = String.valueOf(socket);
                fileNameFormat = dataInputStream.readInt();

                if (fileNameFormat == 2) {
                    folderName = dataInputStream.readUTF();
                    File saveDir = new File("/WebDVR/" + folderName);
               
                    if (!saveDir.exists())
                        saveDir.mkdirs();
                }

                size = dataInputStream.readInt();

                fileName = dataInputStream.readUTF();

                if (fileNameFormat == 2)
                    fileOutputStream = new FileOutputStream("/WebDVR/" +
                                                            folderName + "/" + fileName);
                else
                    fileOutputStream = new FileOutputStream("/WebDVR/" +
                                                            fileName);

                byte [] bytes = new byte[8*1024];
                int count, total = 0;

                while ((count = dataInputStream.read(bytes)) != -1){
                    total += count;
                    fileOutputStream.write(bytes, 0, count);

                    if(total == size) {
                        break;
                    }
                }
                fileOutputStream.flush();
                fileOutputStream.close();

                System.out.println("FileName " + fileName);
                System.out.println("FileSize " + size + " bytes");
                System.out.println("Socket close.");
            }
        } catch(IOException e) {
            System.err.println("Client BREAK");
        } finally {
            try {
                socket.close();
            } catch(IOException e) {
                System.err.println("Socket not closed");
            }
        }
    }
}


Скачать файл сервера   (zip)