Wednesday, January 11, 2012

Web Application to upload a file to any directory

http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml


on multipart/form-data:

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

if you add in the index.jsp

<tr>
<td><b>Choose the destination directory:</b></td>
<td>
<select name="targetDirectory">
<option value="c:/tmp">c:/tmp</option>
<option value="c:/temp">c:/temp</option>
</select>
</td>
</tr>


and you do:


Object targetDirectoryAttribute = request.getAttribute("targetDirectory");
String targetDirectoryParameter = request.getParameter("targetDirectory");
System.out.println("targetDirectoryAttribute=" + targetDirectoryAttribute);
System.out.println("targetDirectoryParameter=" + targetDirectoryParameter);

they are both null



on how to read parameters in a multipart/form-data form:

http://www.jguru.com/faq/view.jsp?EID=1045507

http://commons.apache.org/fileupload/

interesting code snippets here

invoke readHeaders() in org.apache.commons.fileupload.MultipartStream

looks too complicated...


I would rather use

http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html

getParameter(java.lang.String name)

so I download http://www.servlets.com/cos/cos-26Dec2008.zip

but, wait a minute, I find this
http://stackoverflow.com/questions/3238103/extracting-parameters-of-a-httprequest-when-form-type-is-multipart


http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/servlet/ServletFileUpload.html

http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/disk/DiskFileItemFactory.html


Some other posts on the topic:

http://stackoverflow.com/questions/1545717/issue-with-multipart-form-data


http://stackoverflow.com/questions/1748259/get-form-parameters-from-multipart-request-without-getting-the-files

http://stackoverflow.com/questions/3238103/extracting-parameters-of-a-httprequest-when-form-type-is-multipart

Eventually I give up on all those horrible libraries, and I add a second for an a onclick javascript to add in the HTTP query the directory info...

See the working result here:

http://javatoolsforweblogic.googlecode.com/svn/trunk/fileUpload/index.jsp
http://javatoolsforweblogic.googlecode.com/svn/trunk/fileUpload/single_upload_page.jsp


Anyway this is disgusting, the way this HTTP Java Servlet technology turns simple things in a total nightmare.... no wonder people turn to other technologies...

No comments: