473,498 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending Files via HTTP

Nepomuk
3,112 Recognized Expert Specialist
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 ObjectOutputStream, receiving them on Client side with a ObjectInputStream and writing to a file. It works normally, but it's very slow and when the file is big, the Servlet throws a OutOfMemoryError 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 ObjectOutputStream?
  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
Sep 14 '07 #1
8 5308
Nepomuk
3,112 Recognized Expert Specialist
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
Sep 14 '07 #2
r035198x
13,262 MVP
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
Expand|Select|Wrap|Line Numbers
  1. URI uri = new URI(fromPath); 
  2.              URL url = uri.toURL(); 
  3.             URLConnection conn = url.openConnection(); 
  4.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
  5.             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toPath)); 
  6.             int val = in.read(); 
  7.             while(val != -1) { 
  8.                 bos.write(val); 
  9.                 val = in.read(); 
  10.             }
  11. ......
I doubt if it will be any improvement though
Sep 14 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
Try it with something like
Expand|Select|Wrap|Line Numbers
  1. URI uri = new URI(fromPath); 
  2.              URL url = uri.toURL(); 
  3.             URLConnection conn = url.openConnection(); 
  4.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
  5.             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toPath)); 
  6.             int val = in.read(); 
  7.             while(val != -1) { 
  8.                 bos.write(val); 
  9.                 val = in.read(); 
  10.             }
  11. ......
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
Sep 14 '07 #4
r035198x
13,262 MVP
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.
Sep 14 '07 #5
Nepomuk
3,112 Recognized Expert Specialist
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
Sep 14 '07 #6
Nepomuk
3,112 Recognized Expert Specialist
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
Sep 14 '07 #7
Nepomuk
3,112 Recognized Expert Specialist
The question is still important - will it work that way?

Greetings,
Nepomuk
Sep 16 '07 #8
Nepomuk
3,112 Recognized Expert Specialist
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
Sep 17 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3124
by: Agenzia Petracca | last post by:
i wanna send html files (even *.doc) in the body of a mail with this code: <? $userfile = "file.html"; $fp = fopen($userfile, "r"); $file = fread($fp, filesize($userfile)); $file =...
1
1835
by: Nazgul_zulus | last post by:
Hello, I have got c++ program, which aim is to modify files in xml format. I would like to send modified xml file using HTTP POST method. I suppose that I need to make socket and connect on 80...
2
2752
by: Ebrahim | last post by:
Does any one have any suggestion of Sending Files over a Network ? using TcpClient class in C#. I need suggestions on transferring huge files ( > 5MB) reliably over the stream . Also any kind of...
1
4113
by: Lessandro Sant'Ana | last post by:
Hi, my error is error while tryng to run project: unable to start debugging on the web server. Server side-error occurred on sending debug HTTP request. Make sure the server is operating...
0
1149
by: Buddy Ackerman | last post by:
I'm sending file to the client (see sample code below) but when I send a large file the user gets a document contains no data message. response.contenttype = "application/x-unknown" myheader =...
3
7430
by: darrel | last post by:
I got a new machine this week. Prior to that, I backed up my WWWROOT folder. Now I have the new machine, moved my files back. I have one projet, though, that refuses to debug/launch: ...
6
2779
by: Piedro | last post by:
Hi, has anyone in here got a good example or some hints on how to send a file directly from one pc to another pc using vb.net, for example I've got my pc's ip and a friends pc's ip and I want to...
0
1501
by: Saverio Tedeschi | last post by:
Hi all gurus, I wrote an Win app with embedded FTP client (well, made some cut and paste from others' projects :-)) to receive and send files from within the app itself. Now the server I connect...
1
1922
by: Srini | last post by:
Hi everyone, I want to transfer a file from servlet page or JSP to an ASP page. Since the two pages are in different server (WAS and IIS), I cannot just send file path to the ASP page and let it...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.