简体   繁体   中英

Why Is my code only sending part of a Large File

Im trying to make a Java Socket File Server but I have hit a dead end, It seems to be working until in around loop 4080 then seems to stop, Any ideas as to why this is happening? here is my code:

public class FileSender extends Thread{

private final int PORT = 7777;
private Socket sock;
private DataInputStream fileStream;
private OutputStream out;
private File file ;

public void run() { 

    try {
       file = new File("path");

       if(file.exists()){
           System.out.println("Found File");

           if(file.canRead()){
               System.out.println("Can Read File");
           }
       }
    this.fileStream = new DataInputStream(new FileInputStream(file));



        sock = new Socket("localhost",PORT);
        out = sock.getOutputStream();

        copyStream(fileStream, out, file);

    } catch (IOException ex) {
        Logger.getLogger(FileSender.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public void copyStream(DataInputStream in, OutputStream out, File file) throws IOException{

    byte[] buf = new byte[1024];

    int total = 0;

    while(file.getTotalSpace() != total){
        int r = in.read(buf);


        if(r != -1){
        out.write(buf, 0, r);
        }

        total += r;
    }

    out.flush();

    System.out.println("Total was:" + total);

}

}

This is My Server:

public class FileReceiver extends Thread {

private final int PORT = 7777;
private ServerSocket sSoc;
private DataInputStream in;

public void run() {

    try {

        byte[] buf = new byte[1024];

        sSoc = new ServerSocket(PORT);

        Socket conn = sSoc.accept();

        in = new DataInputStream(new BufferedInputStream(conn.getInputStream()));

        File file = new File("C:\\test.rar");

        if (!file.exists()) {
            file.createNewFile();
        }

        if (file.canWrite()) {

            FileOutputStream fos = new FileOutputStream(file);

            int x= 0 ;
            do {

                in.read(buf);
                System.out.println(x + " : " + buf);

                fos.write(buf);
                x++;

            } while (in.read(buf) != -1);

            System.out.println("Complete");
        }

    } catch (IOException ex) {
        Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

EDIT : The program will send a small text file but onlky sends part of a Larger File.

Your condition in the while loop in copyStream seems wrong. Please try and change it to the following and try.

public void copyStream(DataInputStream in, OutputStream out, File file) throws IOException{

    byte[] buf = new byte[1024];

    int total = 0;

    while(true){
        int r = in.read(buf);    

        if(r != -1){
            out.write(buf, 0, r);
            total += r;
        } else {
            break;
        }  

    }

问题出在我的服务器上,我在两次调用in.read()时强制使用大于单个缓冲区大小的任何值来丢失段。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM