• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass/parse a variable to MultipartRequest

 
Greg Kearn
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to give the user the ability to choose which directory they want to store a file. On my web page I have a input field "subdir" which I want to pass the contents Upload.java:
MultipartRequest multi = new MultipartRequest(req, subdir, maxsize);
How do I pass or parse "subdir" the value.
Thanks
Greg
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a slight problem with this approach.

Nothing in the HTTP spec says that form fields need to be submitted in the order in which they appear in the HTML source. So there is no guarantee as you are parsing the fields, that your 'source dir' would even come before your file upload.

The solution would be to upload the files to a constant temp directory, parse the request, and use the supplied value for 'directory' and Java File I/O methods to move the file after upload.

This is even covered in the cos FAQ as the preferred method of doing file uploads to dynamic directories
( http://www.servlets.com/cos/faq.html ) search that page for the word 'safest'
[This message has been edited by Mike Curwen (edited December 10, 2001).]
 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,
I also encounter the same situation.
The problem with MultipartRequest is that the uploading is done in the Constructor. The "SubDir" parameter cannot be retieved using MultipartRequest.getParamater() if the instance is not constructed. However to create the MultipartRequest object, the "SubDir" parameter is needed.
What I did is that I modify the MultipartRequest class so that the Uploading of files is invoked by a seperate method. The MutipartRequest class does not take in the "SaveDiectory" in its constructor. I added the methods setDir() and uploadFiles() to the class.
So my UploadTest class uses the MultipartRequest in the following way:
MultipartRequest multi =
new MultipartRequest(req, 5 * 1024 * 1024);
multi.setDir(multi.getParameter("subdir"));
multi.uploadFiles(multi.getDir());
Hope it helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!