It's not a secret anymore!*
The moose likes Servlets and the fly likes Cannot download the whole text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cannot download the whole text file" Watch "Cannot download the whole text file" New topic
Author

Cannot download the whole text file

Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
Hello

I am writing servlet which is to download files to my desktop app and then save it on my hdd. I have tested the code and it works fine for binary files like video ot images or even doc files but it doesn't work with txt. It looses text.

Here is the problem code:



For example I have just downloaded a txt file so its original length is 1722bytes but the downloaded version contains 1696bytes ... So where I could lost its final 26 bytes ? I suspect the final chunk occures the problem but I don't know how to fix it. Please help


Any useful comment is appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Have you compared the files? Could the difference be perhaps in how lines are terminated?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
Bear Bibeault wrote:Have you compared the files? Could the difference be perhaps in how lines are terminated?


Yes, I opened it and watched its content... I don't know why but I get lost the final bytes Please help me to understand why
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

So you are actually missing characters at the end of the file? Not just comparing lengths?
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
here is the original text:

I don't know when it happened.
I don't know when she came,
But, she's the one I always knew,
Grandma was her name.

She taught me how to tie my shoes.
She taught me how to talk.
And, though I can't remember,
I think she taught me how to walk.

When all the other kids in school
Would talk about Mom and Dad,
I wondered where my parents were,
That made me kinda’ sad.


here is the downloaded text:

I don't know when it happened.
I don't know when she came,
But, she's the one I always knew,
Grandma was her name.

She taught me how to tie my shoes.
She taught me how to talk.
And, though I can't remember,
I think she taught me how to walk.

When all the other kids in school
Would talk about Mom and Dad,
I wondered where my parents were,
That made



Why the code doesn't download the whole txt files ? As you can see the downloaded file missing text How to fix it?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

BeForthrightWhenCrossPostingToOtherSites
http://stackoverflow.com/questions/5505418/downloaded-txt-file-become-shrinked
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Your code indentation is all wonky, so I won't be reading the code, but check your loops to make sure that you're not dropping the last chunk on either writing or reading.
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25


It is not . They don't answer for long time so maybe they simply don't know how to help me with the question. So I hope you can
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Have you debugged your loop as I suggested?
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
Bear Bibeault wrote:Have you debugged your loop as I suggested?


Yes

I want to show you the decipher code:

the decipher defined the same as for encryping...:



How you think maybe there is a problem? I heard that to decrypt there should be some differ alg parameters but I am not sure...
Kasper Pedersen
Greenhorn

Joined: Apr 03, 2011
Posts: 4
Andrew Watson wrote:here is the original text:

I don't know when it happened.
I don't know when she came,
But, she's the one I always knew,
Grandma was her name.

She taught me how to tie my shoes.
She taught me how to talk.
And, though I can't remember,
I think she taught me how to walk.

When all the other kids in school
Would talk about Mom and Dad,
I wondered where my parents were,
That made me kinda’ sad.


here is the downloaded text:

I don't know when it happened.
I don't know when she came,
But, she's the one I always knew,
Grandma was her name.

She taught me how to tie my shoes.
She taught me how to talk.
And, though I can't remember,
I think she taught me how to walk.

When all the other kids in school
Would talk about Mom and Dad,
I wondered where my parents were,
That made



Why the code doesn't download the whole txt files ? As you can see the downloaded file missing text How to fix it?


I just noticed that you are missing the same number of characters as there are newlines in the text (suggesting some \n\r issue). Try doing the same with a text without any linebreaks ... just a thought.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot download the whole text file
 
Similar Threads
Java nio
Download remote jar file using java
Losing newlines in text file when zipping
Problem zipping the file
moving files over the network