Results 1 to 17 of 17
Thread: Transfering files over HTTP
- 10-20-2008, 10:12 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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 .. )
- 10-20-2008, 03:19 PM #2
Read a buffer full, write that buffer, read next buffer full, write that buffer, etc
- 10-20-2008, 04:50 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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 ?
- 10-20-2008, 05:35 PM #4commit the changes
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.
- 10-20-2008, 10:59 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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".
- 10-20-2008, 11:49 PM #6
Not sure how my solution would be a problem with a regular browser. Can you explain?
I don't just need it to "work".
- 10-21-2008, 08:14 AM #7
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.
- 10-21-2008, 10:53 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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.
- 10-21-2008, 11:06 PM #9
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-21-2008, 11:23 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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.
- 10-21-2008, 11:27 PM #11
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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.
- 10-21-2008, 11:30 PM #12
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
- 10-22-2008, 12:05 AM #13
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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 ?
- 10-22-2008, 07:21 AM #14
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.
- 11-09-2008, 03:26 PM #15
reliability != it works
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);//
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
- 11-09-2008, 08:54 PM #16
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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 ?
-
Similar Threads
-
HTTP Status 405 - HTTP method GET is not supported by this URL
By javanewbie in forum Java ServletReplies: 7Last Post: 11-11-2009, 09:29 PM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 04:20 PM -
Http change to HTTP\SSL
By ballyv24 in forum Advanced JavaReplies: 0Last Post: 05-14-2008, 12:32 PM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 05:43 AM -
how to convert mpeg files to .wav files
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:14 AM
Bookmarks