FileOutputStream中的FileNotFound异常

时间:2017-06-03 17:53:50

标签: java sockets ftp swingworker

我收到了FileNotFound的错误。基本上,我正在尝试将文件从客户端上传到服务器。

请帮帮我。

这是client.java类 包ftppackage;

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

public class Client { 

    public static void main (String [] args ) throws IOException { 
        Socket socket = new Socket("127.0.0.1",15123); 
        File transferFile = new File ("D:\\AsiaAd.wmv");
        byte [] bytearray = new byte [(int)transferFile.length()];
        FileInputStream fin = new FileInputStream(transferFile);
        BufferedInputStream bin = new BufferedInputStream(fin);
        bin.read(bytearray,0,bytearray.length);
        OutputStream os = socket.getOutputStream();
        System.out.println("Sending Files...");
        os.write(bytearray,0,bytearray.length);
        os.flush();
        socket.close();
        System.out.println("File transfer complete");
    }
} 

这是我的server.java类

package ftppackage;

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

public class Server {

    public static void main (String [] args ) throws IOException {
        int filesize=1022386;
        int bytesRead;
        int currentTot = 0;
        ServerSocket serverSocket = new ServerSocket(15123);
        Socket socket = serverSocket.accept();
        System.out.println("Accepted connection : " + socket);
        byte [] bytearray = new byte [filesize];
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("E:\\0\\"); // it is creating new file not copying the one from client
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(bytearray,0,bytearray.length);
        currentTot = bytesRead;
        do {
            bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
            if(bytesRead >= 0)
                currentTot += bytesRead;
        } while(bytesRead > -1);
        bos.write(bytearray, 0 , currentTot);
        bos.flush();
        bos.close();
        socket.close();
    }
}

另外,请指导我如何使用百分比添加进度条。我在这里阅读了SwingWorker,但无法实现它,因为我对线程概念完全不熟悉。 感谢您考虑我的问题。

1 个答案:

答案 0 :(得分:-1)

如果将文件对象指向该路径中不存在的某个文件,则会获得FileNotFoundException。它表示您尝试上传的文件不在指定路径中。因此,请确保您提供有效的路径。