• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Pass/parse a variable to MultipartRequest

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic