写入客户端的文件和NegativeArraySizeException

时间:2014-05-30 10:30:16

标签: java file exception client-server zipoutputstream

我有一台服务器,可以发送数以千计的小文件,如下所示:

static File file = null;

static File temp = null;
private static ServerSocket serverSocket;
private static Socket socket;


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

    serverSocket = new ServerSocket(3000);
    socket = serverSocket.accept();
    System.out.println("Connected");



    File folder = new File("C:...\\Desktop\\thousands_of_tiny_files");
    File[] listOfFiles = folder.listFiles();
    File result[]=new File[listOfFiles.length];


    byte [] bytearray  = null;
    FileInputStream fin =null;
    BufferedInputStream bin = null; 



    for(int j=0;j<listOfFiles.length;j++){

        String path= listOfFiles[j].getPath();

        result=sendFile(path);
        for(int i=0;i<result.length;i++)   
        {
            fin = new FileInputStream(result[i]);
            bin = new BufferedInputStream(fin); 
            bytearray  = new byte [(int)result.length];
            bin.read(bytearray,0,bytearray.length);
            OutputStream os = socket.getOutputStream();
            System.out.println("Sending Files "+ result[i]);

            os.write(bytearray,0,bytearray.length);
            os.flush();

            System.out.println("File transfer completed");        

        }
    }
 fin.close();
 bin.close();
 socket.close();
}

public static File[] sendFile(String path)
{
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    File[] resultt = new File[listOfFiles.length];
    for(int i=0;i<listOfFiles.length;i++)
    {
        temp=listOfFiles[i];
        if(temp.isFile() && temp!=null)
            resultt[i]=temp;

    }
    return resultt;
}

它是成功的,但我的问题是在客户端。我不知道如何区分文件并将它们单独写入客户端。

EDIT2: 我使用 ZipOutputStream 更改了服务器端代码,但仍然不知道如何在客户端解压缩和编写(主要是,不知道如何定义 FileOutputStream 在客户端:

    for(int i=0;i<result.length;i++)   
        {
            fin = new FileInputStream(result[i]);

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

            ZipOutputStream zipOpStream = new ZipOutputStream(socket.getOutputStream());
            zipOpStream.putNextEntry(new ZipEntry(result[i].getName()));


            System.out.println("Sending Files "+ result[i]);

            zipOpStream.write(bytearray,0,bytearray.length);
            zipOpStream.flush();

            System.out.println("File transfer completed");        

        }
    }

 socket.close();
} 

和收件人代码:

          socket = new Socket("127.0.0.1",3000);

         String outDir = "C:...\\Desktop\\here";



         BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());            


          ZipInputStream zips = new ZipInputStream(bis);
          ZipEntry zipEntry = null;

          while(null != (zipEntry = zips.getNextEntry())){
          String fileName = zipEntry.getName();
          File outFile = new File(outDir + "/" + fileName);
          System.out.println("----["+outFile.getName()+"], filesize["+zipEntry.getCompressedSize()+"]");

    if(zipEntry.isDirectory()){
    File zipEntryFolder = new File(zipEntry.getName());
    if(zipEntryFolder.exists() == false){
     outFile.mkdirs();
    }

        continue;
    }else{
    File parentFolder = outFile.getParentFile();
    if(parentFolder.exists() == false){
     parentFolder.mkdirs();
    }
   }

   System.out.println("ZipEntry::"+zipEntry.getCompressedSize());



   FileOutputStream fos = new FileOutputStream(outFile);
   int fileLength = (int)zipEntry.getSize();

   byte[] fileByte = new byte[fileLength];
   zips.read(fileByte);
   fos.write(fileByte);
   fos.close();


        socket.close();

     }

现在我得到了一个特殊情况&#34;负面 NegativeArraySizeException &#34;

Exception in thread "main" java.lang.NegativeArraySizeException
    at recieve.Reciever.<init>(Reciever.java:71)
    at recieve.Recieve$1.<init>(Recieve.java:28)
    at recieve.Recieve.main(Recieve.java:28)
ZipEntry::-1

可能是因为

   int fileLength = (int)zipEntry.getSize();

但我怎么解决这个问题呢?我需要zip文件夹的下一个条目的siz用于写入文件。 但大小在而不是 int

1 个答案:

答案 0 :(得分:1)

如何将其写入ZipOutputStream,您可以在另一侧将文件作为单个流接收。获得完整输出后,可以编写反向代码来缩小zip文件并获取所有文件。提供用于将Zip写入套接字流的示例代码,您可以构建代码。

请参阅this length,了解使用ZipOutputStream发送文件的示例服务器和客户端实现Stick2Code