Results 1 to 17 of 17
  1. #1
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default Transfering files over HTTP

    I have a simple http server right now that works ( I checked it )

    basically I have a "task" class that has an HTTP request and response routed to it.

    this class needs to read a file , and transfer it through the response output stream to the user ..

    How exactly do I open a binary file for reading in java and transfer it to a user? (I can't just load the whole file to memory .. )

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Read a buffer full, write that buffer, read next buffer full, write that buffer, etc

  3. #3
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    What you are saying is I can just write a buffer of whatever length I want and commit the changes every time , and the connection wouldn't close ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    commit the changes
    Not sure what that means in the context of copying a file from a server to a client.
    What I meant to say was the sending program would read a buffer of data, send that buffer and then read the next buffer of data and send that buffer of data. The read/write would be in a loop in the sending program. It would continue until it reached the EOF. The receiving program would have the corresponding logic: read a buffer of data, write a buffer of data in a loop until EOF.

    This logic would NOT require reading the whole file into memory on the sending side or in holding the whole file in memory on the receiving side.

  5. #5
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    woah there .. careful..

    I need the server to be standard HTTP compatible (meaning regular browsers can also access the file)

    I don't just need it to "work".

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Not sure how my solution would be a problem with a regular browser. Can you explain?
    I don't just need it to "work".
    What more to it is there than if it works? What is there beyond working? This is more in Nick's realm. I'm sure he'd be able to explain what's out there beyond working.

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by DannyZB View Post
    I need the server to be standard HTTP compatible (meaning regular browsers can also access the file)
    I'm lost. What you do inside your HTTP server is invisible to the outside.
    @norm's initial suggestion is a fine idea. Just do it.

    I have no idea what you mean by "regular browsers can access the file"
    What file? the one on your server's disk? That depends on the access rules you setup.

    The idea of a server allowing anyone to upload any file, and then let anyone else read the file scares the heck out of me.

    the HTTP protocol is simple. Nearly trivial. All you must implement are GET and PUT.

  8. #8
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    You know how when everyone asks you about basic java .. c++ programming or anything of the sort everything sounds trivial to you?

    Well , think back to when you just looked at java for the first time , it didn't : )
    So bear with me ..

    suppose you have access to an http response and http request..
    The user is requesting a file (ignore access permissions thats the rest of the code it's irrelevent at the moment)

    What do I do step-by-step on the server-side to send the user the file?
    assume the user is using a standard web browser.

    Thanks for the help anyway.

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    What is a file? Seriously.

    It now sounds like you are concerned with returning a file from the server to the client's browser. When I read the first posting, I read it that you wanted to accept a file from the client. Which is it?

    It would help if you use precise terms. Sound to me (and this is a guess) that you want to fire up your HTTP server, wait for a GET command, and return the contents of a file. This is fundamental to what a HTTP server has to do.

    So you write a servlet that:

    • parses the URI to find out which thing the user wants
    • map the exposed 'thing' to your internal data store structure
    • set the headers as appropriate
    • open the 'file' on your disk that was specified by step 2
    • go into a read/write loop
    • read a buffer
    • write the buffer to the 'out' stream of the servlet


    You can skip the header stuff until you have the rest of it working.
    The key you will want to do is set the MIME type so the browser knows what to do with the byte stream it gets. It can display it, save it to the user's disk, play it, etc.

  10. #10
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    Ok lets make a long story short ..
    look at the code and you should easily guess what I want to do ..

    public void run(){
    try {
    String target = request.getTarget();
    File f = new File("funvideo05.wmv");
    JOptionPane.showMessageDialog(null, f.length());
    int length = 0;

    length = (int)f.length();
    response.set("Content-Type", "application/octet-stream");
    response.set("Content-Disposition", "attachment; filename="" + "funvideo05.wmv" + """);
    response.setDate("Date", System.currentTimeMillis());
    response.set("Server", "Extending Bittorrent Tracker");

    OutputStream out = response.getOutputStream();

    byte[] bbuf = new byte[8];
    DataInputStream in = new DataInputStream(new FileInputStream(f));

    while ((in != null) && ((length = in.read(bbuf)) != -1))
    {
    out.write(bbuf,0,length);
    }

    out.close();

    } catch (Exception ex) {
    }
    /*

    if (target.matches(mappedString)) {
    response.
    }
    OutputStream out = resp.getOutputStream();
    context.getContent(target).write(out);
    out.close();
    */
    }

    It's a thread thats supposed to serve the file to the user.

    the headers are sent, the browser recognizes the file..
    but no file content is sent.
    (Yes I know I don't use a variable for a file name but I checked and the server successfully loads the file)

    Wouldn't hurt if you can tell me what I need to notice so everything is thread safe.

  11. #11
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    Ignore my last response I forgot to set content length..
    the code works at last.

    Just one thing - what about the code isn't thread safe ?
    it stalls my server.
    I don't have much experience using threads in java so expect noob mistakes.

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Get rid of all the thread stuff until it works at the basics of delivering the file.

    And use a rational buffer size, say 2048, not 8

  13. #13
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    It already does ..
    I'm not getting rid of no thread stuff thats basically the whole server code.

    looking at that code , do you see any problems ? (the download works)
    increasing buffer size helped.

    Whats the ideal buffer size to use btw ?

  14. #14
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You should debug your code without threads. Once it works, just add threads.

    You do not have to follow my suggestions, but you might want to seriously consider it.

  15. #15
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Cool reliability != it works

    Quote Originally Posted by Norm View Post
    ....This is more in Nick's realm. I'm sure he'd be able to explain what's out there beyond working.
    Well at least one person in the universe understands me, I think......

    Trying to transport printable documents over http is one thing that is not working, so I am on safe ground trying to explain working:

    Working: (derogatory) A delusory state where all functions pass testing and one is hung out to dry on a network feed at a product roll-out.

    I did some fiddling in STL and decided on buffers to be the native page size of whatever os and hardware the thing is supposed to run on, eg:
    Java Code:
    try
    {
        java.lang.String osName = System.getProperty("os.name");
        // Whether we are working around Solaris.
        final boolean solarisWorkaround = (osName != null &&
        osName.indexOf("Solaris") != -1);
        /* ==== Loop counter. === */
        int loopIndex = 0;
        //
        /* ==== Thread management. === */
        final java.lang.Thread currentlyExecutingThread =  java.lang.Thread.currentThread();
        final java.lang.Integer betweenThreadLaunches = new  java.lang.Integer(java.lang.Thread.NORM_PRIORITY - 1);
        //
        /**
        * on Unice, Max File Handles == 60
        * on M$ #define FOPEN_MAX 20
        */
        final java.lang.Integer MAX_THREADS = new java.lang.Integer(solarisWorkaround?60:20);
                    final java.lang.Integer MAX_OPEN_FILE_HANDLES = new java.lang.Integer(MAX_THREADS.intValue() - 4);// Word count has three file handles per instantiation.
        final java.lang.Integer BUFFER_SIZE = new java.lang.Integer(solarisWorkaround?8192:4096);//
    which is in the first few hundred lines of the first Java program I wrote.

    If your project is a commercial effort, make sure your resume is being mailed at about a thousand a day and the company insurance coverage is a pricey front-line provider with excellent non-compete's in your employer's favor before putting valuable commercial data on a N-Tier'd server written solely in Java.

    Transferring files over HTTP can be done from servlets using getOutputStream but I do not see how that is actually done as there is no way to get the "What do you want to do with this file: PreFormatted.ps?" to come up with a displayed name - and Norm, that is exactly what I am working on this morning. If you want to be like me try to come up with a workable solution for Printer-Friendly documents over http, avoiding proprietary like cats avoid water.

    { domestic felicus are good rodent control }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  16. #16
    DannyZB is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    Hehe you people are crazy .. remind me of myself (:

    This is my pet project , it consists of several parts :
    - An extensible http server ( based on reflections )
    - Task handlers for : bittorrent , torrent download and torrent upload ( though uploading of all file formats is possible ) :also easily extensible
    - A simple applet for bittorrent based downloads ( so that people can download straight from a popup window)

    I'm Israeli and I'm getting drafted in 10 days ..
    I have a big pile of good code like this lying around ,
    what do you think a good use for this will be ?

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't help you with your code, but do offer the hope that your tour of duty be a safe one.

Similar Threads

  1. Replies: 7
    Last Post: 11-11-2009, 09:29 PM
  2. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 04:20 PM
  3. Http change to HTTP\SSL
    By ballyv24 in forum Advanced Java
    Replies: 0
    Last Post: 05-14-2008, 12:32 PM
  4. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 05:43 AM
  5. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:14 AM

Posting Permissions

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