Sysout文件列表和从java目录中传输文件

时间:2015-05-28 02:30:49

标签: java sockets streaming

我正在试图弄清楚如何获取服务器上的文件列表以及如何单独下载它们。任何人都可以指导我朝正确的方向发展吗?

我收到文件权限错误或(是目录错误),谢谢

更新: 错误是

Exception in thread "main" java.io.FileNotFoundException: /myClientFiles (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at 
xxxxxxpackegename.com.fr(Client.java:38)

Client.java

public class Client {

 private static final int PORT = 2665;
 private static String HOST = "localhost";

public static void main(String[] args) throws UnknownHostException, IOException {


    int filesize = 5000000;   //buffer size 5mb
    int bytesRead;
    int currentTotalNumberOfBytes = 0;

     //connect to port on server - server waits for this after running socket.accept() in the Server class
    Socket socket = new Socket(HOST, PORT);  

    byte[] byteArray = new byte[filesize];   //create a byte array of 5mb        
    InputStream inputStream = socket.getInputStream();  //channel to write to server
    FileOutputStream fileOutStream = new FileOutputStream("/myClientFiles");      
    BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);

    bytesRead = inputStream.read(byteArray, 0, byteArray.length);
    currentTotalNumberOfBytes = bytesRead;


    do { //read till the end and store total in bytesRead and add it to currentTotalNumberOfBytes
        bytesRead = inputStream.read(byteArray, currentTotalNumberOfBytes, (byteArray.length-currentTotalNumberOfBytes)); 
        if(bytesRead >= 0) currentTotalNumberOfBytes += bytesRead; 
        }while(bytesRead > -1); // when bytesRead == -1, there's no more data left and we exit the loop


    bufferOutStream.write(byteArray, 0 , currentTotalNumberOfBytes); //write the bytes to the file

    bufferOutStream.flush(); 
    bufferOutStream.close(); 
    socket.close(); 



 }


}

Server.java

ServerSocket serverSocket = new ServerSocket(2665);

    Socket socket = serverSocket.accept();                  
    System.out.println("Connected to: " + socket);

    //File transferFile = new File("Allcrisis.doc");              //get local file

    File[] transferFiles = new File("/myServerFiles").listFiles(); //array to store pathnames of files in myServerFiles folder

    byte[] bytearray = new byte[(int)transferFiles.length];  


    for(File file: transferFiles){    
    //FileInputStream fileInputStream = new FileInputStream(transferFiles); 
    FileInputStream fileInputStream = new FileInputStream(file);
    BufferedInputStream butterInputStream = new    BufferedInputStream(fileInputStream); 
    butterInputStream.read(bytearray, 0, bytearray.length); 


    OutputStream outStream = socket.getOutputStream();                
    System.out.println("Sending...");                    
    outStream.write(bytearray, 0, bytearray.length);                

    outStream.flush();   
    }



    socket.close();  

1 个答案:

答案 0 :(得分:0)

您有一个文件夹/myClientFiles

您的代码包含

new FileOutputStream("/myClientFiles")

尝试打开FileOutputStream来写入该文件夹。您无法写入文件夹。

您可能希望将new FileOutputStream路径传递到文件进行写入。