• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple File Save  RSS feed

 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code I receive uploaded image file and want to save it in two different sizes.

I am saving it using the given code:

The files of size 0 bytes get saved with the given names.
when I save only one at a time then it gets saved properly.
Please help me to debug the problem.


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1 exhausts the input stream, so there's nothing in step 2 to read any more. You could read the entire contents of the input stream into memory using a ByteArrayOutputStream, and then work with the resulting byte[] as many times as you need to.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or write to both files simultaneously:
Be sure to notice my remark on line 20.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the below code:

Now only one file is getting saved but the the another file is just created with size 0 byte.

 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll still need to flush() and close() the output streams.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob,

I followed your instructions and tried but again the same results. File with both names created but with size 0 byte.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you made sure that the input stream does contain any data?
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my second message after the last line baos.writeTo(out); I flushed both the out and out2 and again tried. This time the same results.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed your instructions and write the code (please see my second message with code), I was able to save one file correctly.

When I try to save only one file it gets saved. It means I am getting the input stream.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what if you try the code from my post?
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the below code to find inputstream has some data!



Then one by one I tried to run codes suggested by both of you. Ulf your doubt is correct, the loop runs not for a single time.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the code and I am getting desired results.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!