Sending Files via HTTP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    Sending Files via HTTP

    Hi everyone!

    I have a problem: In my current project, I have two parts: A client and a servlet. At one point, the servlet is supposed to send Files to the Client.

    So far, I have implemented this by manually splitting the file into arrays of bytes, sending these arrays via an ObjectOutputStr eam, receiving them on Client side with a ObjectInputStre am and writing to a file. It works normally, but it's very slow and when the file is big, the Servlet throws a OutOfMemoryErro r exception, because of the Java heap space. (Not surprising, is it?)

    The reason I'm using several byte arrays is, that I want to implement error correction - the client should check, if the data it received can be valid and if not request it again.

    Now, I could get rid of the Error, if I sent the byte arrays directly after reading them and then overwrite them (maybe ask the Garbage Collector to work in between), but that doesn't solve the problem of speed.

    So, I have a few questions:
    1. Is there a better Option than an ObjectOutputStr eam?
    2. What size would you recommend for one package?
    3. Is this way of doing it total crap and I should rewrite the whole thing? If so, any hints?
    It does have to be solved using HTTP.

    Any suggestions? Tips? Ideas?

    Greetings,
    Nepomuk
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    I think I should mention, that I must use Port 80 on client side because of firewalls, however if you can work with Sockets within a Tomcat Servlet, that might be faster - or might not.

    I would be prepared to completely rewrite the servlets send-class and the clients receive-class, but I need to know, how.

    Can you use a BufferedWriter and -Reader to send/recieve files? (Binary data?) Would it be an improvement of any sort?

    Greetings,
    Nepomuk

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by nepomuk
      I think I should mention, that I must use Port 80 on client side because of firewalls, however if you can work with Sockets within a Tomcat Servlet, that might be faster - or might not.

      I would be prepared to completely rewrite the servlets send-class and the clients receive-class, but I need to know, how.

      Can you use a BufferedWriter and -Reader to send/recieve files? (Binary data?) Would it be an improvement of any sort?

      Greetings,
      Nepomuk
      Try it with something like
      [CODE=java]URI uri = new URI(fromPath);
      URL url = uri.toURL();
      URLConnection conn = url.openConnect ion();
      BufferedReader in = new BufferedReader( new InputStreamRead er(url.openStre am()));
      BufferedOutputS tream bos = new BufferedOutputS tream(new FileOutputStrea m(toPath));
      int val = in.read();
      while(val != -1) {
      bos.write(val);
      val = in.read();
      }
      ......[/CODE]
      I doubt if it will be any improvement though

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by r035198x
        Try it with something like
        [CODE=java]URI uri = new URI(fromPath);
        URL url = uri.toURL();
        URLConnection conn = url.openConnect ion();
        BufferedReader in = new BufferedReader( new InputStreamRead er(url.openStre am()));
        BufferedOutputS tream bos = new BufferedOutputS tream(new FileOutputStrea m(toPath));
        int val = in.read();
        while(val != -1) {
        bos.write(val);
        val = in.read();
        }
        ......[/CODE]
        I doubt if it will be any improvement though
        Some programs (e.g. Adobe Reader) have their own Downloaders for Updates and they download really fast. I'm sure, they must use HTTP because of the same reason, I have to use it. Does anyone have any idea, how these Downloaders can be so fast, while mine is really slow?

        Greetings,
        Nepomuk

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by nepomuk
          Some programs (e.g. Adobe Reader) have their own Downloaders for Updates and they download really fast. I'm sure, they must use HTTP because of the same reason, I have to use it. Does anyone have any idea, how these Downloaders can be so fast, while mine is really slow?

          Greetings,
          Nepomuk
          The general answer is that they split the source file into smaller sections and download them at the same time.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by r035198x
            The general answer is that they split the source file into smaller sections and download them at the same time.
            So, theoretically, if I was to split my file and create several threads both on server- and on client side, I could download it much faster?

            If so, does Tomcat create several threads automatically, when several Clients request data or can it only serve one at a time? And would it make any sense to do implement the project in such a way?

            Of course, I'm open to any other ideas too.

            Greetings,
            Nepomuk

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              I've had an idea myself and would like to hear your opinions on it:

              As I said, I merely have to use Port 80 (the HTTP-Standard-Port) for this download, as the only reason I'm using HTTP is, that FTP is blocked in many cases. But I assume, that only the Standard-FTP-Port (Port 21) is blocked.

              If I would offer a download via FTP from the server and would set the Client to use an FTP Client which should use the local Port 80 - could that work? (I'm assuming, that HTTP uses the local Port 80 as well as the remote Port 80 - is that correct?)

              Of course, the FTP Protocol would be much faster, as File Transfer is what it's designed for.

              Greetings,
              Nepomuk

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                The question is still important - will it work that way?

                Greetings,
                Nepomuk

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  I guess, this isn't the right Forum for this anymore - however, if anyone knows a solution, please feel free to post it. I've asked the Question about ports in the networking Forum now.

                  Greetings,
                  Nepomuk

                  Comment

                  Working...