This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes how to convert a bunch of string into a file array and then upload it ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "how to convert a bunch of string into a file array and then upload it ? " Watch "how to convert a bunch of string into a file array and then upload it ? " New topic
Author

how to convert a bunch of string into a file array and then upload it ?

Vishwas Gagrani
Greenhorn

Joined: Sep 15, 2010
Posts: 5
Hi!,
i have a bunch of strings, that i need to convert into n number of files ( file array) , so that i may upload it. How can i accomplish this ?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If the data is in memory already, why convert it back to files before uploading it?
Vishwas Gagrani
Greenhorn

Joined: Sep 15, 2010
Posts: 5
Lester Burnham wrote:If the data is in memory already, why convert it back to files before uploading it?



because my uploader code takes in a file[] as parameter. Also, the text to be uploaded may vary from 1byte to say... 1 mb in size. So i can't just upload it as simple string.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Why not just rewrite the uploader code to work with strings?

Also, the text to be uploaded may vary from 1byte to say... 1 mb in size. So i can't just upload it as simple string.

Not following. You said "i have a bunch of strings" - so the data is in memory already, yes? What does the size of the texts have to do with it?
Vishwas Gagrani
Greenhorn

Joined: Sep 15, 2010
Posts: 5
Lester Burnham wrote:Why not just rewrite the uploader code to work with strings?

Also, the text to be uploaded may vary from 1byte to say... 1 mb in size. So i can't just upload it as simple string.

Not following. You said "i have a bunch of strings" - so the data is in memory already, yes? What does the size of the texts have to do with it?


It won't be a good idea to upload such a big size of string, in the form of string. It would be a slow process. Instead, it is a good idea to first convert it into a file object ( but not on the local drive, but on the fly ) and then insert all such files in a file array and then upload it.
thanks.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
It won't be a good idea to upload such a big size of string, in the form of string.

How do you think the file uploading works? By creating a byte[], and then sending it in the body of an HTTP POST (assuming that's how the upload works - you haven't said anything about that). It is true, though, that not the complete byte[] needs to be in memory - it can be read and sent piecemeal. But writing the contents of a String to disk, only to read it back into memory will certainly not be faster than converting it to a byte[] in memory.

Instead, it is a good idea to first convert it into a file object ( but not on the local drive, but on the fly ) and then insert all such files in a file array and then upload it.

The contents of File objects do not exist in memory - it exists in actual files on a disk (with all the security implications that entails for applets).
Vishwas Gagrani
Greenhorn

Joined: Sep 15, 2010
Posts: 5
Lester Burnham wrote:
It won't be a good idea to upload such a big size of string, in the form of string.

How do you think the file uploading works? By creating a byte[], and then sending it in the body of an HTTP POST (assuming that's how the upload works - you haven't said anything about that). It is true, though, that not the complete byte[] needs to be in memory - it can be read and sent piecemeal. But writing the contents of a String to disk, only to read it back into memory will certainly not be faster than converting it to a byte[] in memory.

Instead, it is a good idea to first convert it into a file object ( but not on the local drive, but on the fly ) and then insert all such files in a file array and then upload it.

The contents of File objects do not exist in memory - it exists in actual files on a disk (with all the security implications that entails for applets).


Yeah, i know that converting it into a physical file on the local drive, would be an unnecessary step. That's why, i just want to have some file object, in the memory only, because that is what my uploader code accepts.
But i think, as you said, it is not possible to make such file object ( without pointing it on the drive).

So, i am again left with the problem... how to make a communication between uploader code that accepts only a file[ ] , and the number of strings ( say a string array ) .
I cannot write the string uploader separately, because the code is a part of very complex bigger code, and it would take a huge amount of time to make it work with it.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If you can't make changes to the uploader code (which I think would be a much better way of handling this), check out RAM disk implementations like Apache Commons VFS: http://commons.apache.org/vfs/filesystems.html#ram

Note that you may still have to sign the applet since it uses classes in the java.io.* package.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to convert a bunch of string into a file array and then upload it ?