Results 1 to 7 of 7
  1. #1
    masoud is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default Issue with IOUtils.copy

    Hello everyone.

    I am using IOUtils to copy a file to a socket. When I call my method just one time, it works perfectly, but when I call it more than one time, it appends the new file to the previous one. Here is my code

    private void SendChunks(String FileLocation) throws IOException
    {
    String pathname = FileLocation;
    FileInputStream fis = new FileInputStream(pathname);
    dos.flush();
    int res = IOUtils.copy(fis, dos);
    System.out.println("File successfully sent!");
    fis.close();
    // dos.close();
    }
    I should mention that I am also not able to close dos which is DataOutStream because it closes my socket. I need to create a separate file in the destination for each call. Please help me.
    Thanks

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Issue with IOUtils.copy

    Send the number of bytes of each file before you start sending it. That way the receiver will know after how many bytes the file is done. Ofcourse it would need a filename as well.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Issue with IOUtils.copy

    What is a IOUtils? It doesn't exist in the core set of classes ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    masoud is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default Re: Issue with IOUtils.copy

    Sending the file name is necessary? I can not simply create a new name in destination? In this example, how can I use filename and size to be able to get multiple files?
    Thanks

    in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    out = new FileOutputStream("D:\\newfile.txt");
    String fileName=in.readUtf();
    int filelength=in.readLong();
    byte[] bytes = new byte[16*1024];
    int count;
    while ((count = in.read(bytes)) > 0) {
    out.write(bytes, 0, count);
    }
    out.close();
    in.close();

  5. #5
    masoud is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default Re: Issue with IOUtils.copy

    You need to import commons-io-2.4.jar from Apache into your program to be able to use it.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Issue with IOUtils.copy

    If you can name the files randomly, then fine, whatever your protocol demands. It was my assumption that the filename was desired at the receiving end as well.

    StackOverflow has a nice answer that uses this concept.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Issue with IOUtils.copy

    Quote Originally Posted by masoud View Post
    You need to import commons-io-2.4.jar from Apache into your program to be able to use it.
    Moved from New to Java to Apache Commons.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 8
    Last Post: 06-30-2015, 01:24 PM
  2. Copy Constructor with Shallow Copy
    By Wnt2bsleepin in forum New To Java
    Replies: 1
    Last Post: 04-11-2012, 12:42 AM
  3. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. how to issue the command of Ctrl-C (copy) in Java
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •