• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Problem in streaming & creating files using FileOutputStream

Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am having two image files with its size.
Now what i want to do is ,i want to send the file size & then the whole data of the file to a servlet.

What i am doing is on the client side:

Opening the connection to that file
Getting the fileSize
Opening the httpConnection to the servlet
opening an outputstream on that httpconnection
writing the file size to that outputstream.
closing all the connection & streams.

Then again doing the same procedure mentioned above for writing the file data to the output stream.

On the server side that is in the servlet iam doing the following:

Opening an DataInputstream dis = new DataInputstream(request.getInputStream )
int i = dis.readInt();
Closing the inputstream

Then again for reading the file data coming from the client side I am opening another DataInputStream,reading the whole data coming into an byte[] array & then closing the inputstream.

But the servlet side its throwing IO exception:Could not read the whole file or EOF exception.

So please tell me where i am going wrong.
I want to have both the filesize & the data of the file available to the servlet.

One more thing :
With my request i have two files. For sending the data of both the files to servlet, if the data is successfully received by the servlet & if i want to create individual files for both of them on the server file system using FileOutputStream how can i achieve this.

I tried to give a String path with a counter appended to it & provided that String to the FileOutputStream constructor.
But the server is giving the exception that system cannot find the path specified.
On the other hand what is specified is that if the file doesn't exist it gets created.
One file with a path like "C:/Images" i am able to create but different files are not getting created for both the files whose data i am sending in my request.
So how to solve this problem.

Thanking you,
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd bet closing and opening connections and streams is not doing what you expect. By reading and writing the streams this way you're going around HTTP standards. Look into a more standard approach maybe with the Apache HTTPClient and FileUpload packages.
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for the reply.
The httpclient & fileupload are mostly for Html forms on the client side.
But the image fileswhich i want to send are in the mobile phones whose data i am getting by interacting with the filesystem of the phone .

So what can i do regarding this???

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic