文件传输服务器到客户端 - java

时间:2014-02-09 23:51:47

标签: java network-programming

我正在尝试用java编写服务器客户端文件传输程序 服务器的作用: -
    a)将文件提供给客户(一个)     b)服务器一次发送1个字节的文件     c)应该能够多次发送文件。

客户的角色: -
    a)客户端下载文件
    b)客户端应该在写入磁盘之前缓冲文件(< = 100kb)I.e。缓冲100KB并写入磁盘然后重复此操作直到1MB

我编写了将文件从服务器传输到客户端的代码,但该程序似乎存在一些问题。服务器开始发送数据,但在事务中丢失了数据。此外,要传输的文件的内容将被删除。因为我在控制台上看不到任何特定错误所以我无法弄清楚错误。如果有人指导我这种方法是否正确,请提出适当的更改。

*Server* 
public class Server {


public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    Socket socket = null;
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    int sizeBuffer = 0;

    try{

            try {
                    serverSocket = new ServerSocket(55555);
                } catch (IOException ex) {
                    System.out.println("Error: Unable to Connect to Server ");
                }
            System.out.println("Created Socket");
            try {
                    socket = serverSocket.accept();
                } catch (IOException ex) {
                    System.out.println("Error: Unable to connect to client ");
                }
            System.out.println("Accepted Client Connection ");
            try {
                    inputStream = socket.getInputStream();
                    sizeBuffer = socket.getReceiveBufferSize();
                    System.out.println("Size of Buffer " + sizeBuffer);
                } catch (IOException ex) {
                        System.out.println("Error: unable to ger socket input stream ");
                }       

            try {
                    fileOutputStream = new FileOutputStream("D:/ServerFile.txt");
                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

                } catch (FileNotFoundException ex) {
                        System.out.println("File not found. ");
                }

            byte[] bytes = new byte[sizeBuffer];

            int count;

            while ((count = inputStream.read(bytes)) > 0) {
                    bufferedOutputStream.write(bytes, 0, count);
            }

            System.out.println("Done");
    }// end of first try

    finally{
    bufferedOutputStream.flush();
    bufferedOutputStream.close();
    inputStream.close();
    socket.close();
    serverSocket.close();
    }
}

}

这是客户端代码!

*Client*
public class Client {
public static void main(String[] args) throws IOException {

FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOuptputStream = null;
Socket socket = null;
int count;

try{
    try {
           socket = new Socket("127.0.0.1", 55555);
        } catch (IOException ex) {
            System.out.println("Error: Unable to create Socket ");
        }

            File file = new File("D:/ClientFile.txt");
            long fileLength = file.length();
            System.out.println(fileLength);
            if ( Integer.MAX_VALUE < fileLength ) {
                    System.out.println("Error: Exceeded the size of transfer");
            }


            byte[] bytes = new byte[(int) fileLength];
            try{
                        fileInputStream = new FileInputStream(file);
                }catch (IOException ex){
                    System.out.println("Error: Unable to open fileInputStream");
                }
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOuptputStream = new BufferedOutputStream(socket.getOutputStream());

while ((count = bufferedInputStream.read(bytes)) > 0) {
    bufferedOuptputStream.write(bytes, 0, count);
}
}

finally{
        bufferedOuptputStream.flush();
        bufferedOuptputStream.close();
        fileInputStream.close();
        bufferedInputStream.close();
        socket.close();
}

}
}

0 个答案:

没有答案