{"id":248,"date":"2007-09-19T00:00:00","date_gmt":"2007-09-19T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=248"},"modified":"2007-09-19T00:00:00","modified_gmt":"2007-09-19T00:00:00","slug":"Example JSP File Upload Script","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=248","title":{"rendered":"Example JSP File Upload Script"},"content":{"rendered":"<p><P><SPAN class=mainText>Below shows a JSP file upload script that is used to print out the name-value pair received from the earlier <A href=\"http:\/\/www.strongd.net\/blog\/show\/245\">XHTML MP document<\/A> and save the uploaded file to a certain location on the WAP server. Remember to change the <I>action<\/I> attribute of the &lt;form&gt; element in the earlier <A href=\"http:\/\/www.strongd.net\/blog\/show\/245\">XHTML MP document<\/A> from <I>file_upload.php<\/I> to <I>file_upload.jsp<\/I> for this example to work.<\/SPAN><\/P><br \/><DIV><br \/><P><SPAN class=codeText>&lt;?xml version=&#8221;1.0&#8243;?&gt;<BR>&lt;!DOCTYPE html PUBLIC &#8220;-\/\/WAPFORUM\/\/DTD XHTML Mobile 1.0\/\/EN&#8221; &#8220;http:\/\/www.wapforum.org\/DTD\/xhtml-mobile10.dtd&#8221;&gt;<BR><BR>&lt;%@ page import=&#8221;org.apache.commons.fileupload.*, org.apache.commons.fileupload.servlet.ServletFileUpload, org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.io.FilenameUtils, java.util.*, java.io.File, java.lang.Exception&#8221; %&gt;<BR>&lt;% response.setContentType(&#8220;application\/vnd.wap.xhtml+xml&#8221;); %&gt;<BR><BR>&lt;html xmlns=&#8221;http:\/\/www.w3.org\/1999\/xhtml&#8221;&gt;<BR>&nbsp;&nbsp;&lt;head&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;File Upload Example&lt;\/title&gt;<BR>&nbsp;&nbsp;&lt;\/head&gt;<BR><BR>&nbsp;&nbsp;&lt;body&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;Data Received at the Server&lt;\/h1&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;hr\/&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<BR><BR>&lt;%<BR>if (ServletFileUpload.isMultipartContent(request)){<BR>&nbsp;&nbsp;ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());<BR>&nbsp;&nbsp;List fileItemsList = servletFileUpload.parseRequest(request);<BR><BR>&nbsp;&nbsp;String optionalFileName = &#8220;&#8221;;<BR>&nbsp;&nbsp;FileItem fileItem = null;<BR><BR>&nbsp;&nbsp;Iterator it = fileItemsList.iterator();<BR>&nbsp;&nbsp;while (it.hasNext()){<BR>&nbsp;&nbsp;&nbsp;&nbsp;FileItem fileItemTemp = (FileItem)it.next();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (fileItemTemp.isFormField()){<BR>%&gt;<BR><BR>&lt;b&gt;Name-value Pair Info:&lt;\/b&gt;&lt;br\/&gt;<BR>Field name: &lt;%= fileItemTemp.getFieldName() %&gt;&lt;br\/&gt;<BR>Field value: &lt;%= fileItemTemp.getString() %&gt;&lt;br\/&gt;&lt;br\/&gt;<BR><BR>&lt;%<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (fileItemTemp.getFieldName().equals(&#8220;filename&#8221;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optionalFileName = fileItemTemp.getString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileItem = fileItemTemp;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;if (fileItem!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;String fileName = fileItem.getName();<BR>%&gt;<BR><BR>&lt;b&gt;Uploaded File Info:&lt;\/b&gt;&lt;br\/&gt;<BR>Content type: &lt;%= fileItem.getContentType() %&gt;&lt;br\/&gt;<BR>Field name: &lt;%= fileItem.getFieldName() %&gt;&lt;br\/&gt;<BR>File name: &lt;%= fileName %&gt;&lt;br\/&gt;<BR>File size: &lt;%= fileItem.getSize() %&gt;&lt;br\/&gt;&lt;br\/&gt;<BR><BR>&lt;%<BR>&nbsp;&nbsp;&nbsp;&nbsp;\/* Save the uploaded file if its size is greater than 0. *\/<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (fileItem.getSize() &gt; 0){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (optionalFileName.trim().equals(&#8220;&#8221;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileName = FilenameUtils.getName(fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileName = optionalFileName;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String dirName = &#8220;\/file_uploads\/&#8221;;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File saveTo = new File(dirName + fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileItem.write(saveTo);<BR>%&gt;<BR><BR>&lt;b&gt;The uploaded file has been saved successfully.&lt;\/b&gt;<BR><BR>&lt;%<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception e){<BR>%&gt;<BR><BR>&lt;b&gt;An error occurred when we tried to save the uploaded file.&lt;\/b&gt;<BR><BR>&lt;%<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>}<BR>%&gt;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/p&gt;<BR>&nbsp;&nbsp;&lt;\/body&gt;<BR>&lt;\/html&gt;<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=mainText>The following screenshots show what you will see in the Nokia 6230 cell phone:<\/SPAN><\/P><br \/><P><BR><\/P><br \/><TABLE cellSpacing=0 cellPadding=4 width=\"100%\" border=0><br \/><COLGROUP><br \/><COL width=256><br \/><THEAD><br \/><TR><br \/><TD vAlign=top width=\"100%\"><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_1967dd40.png\" width=131 align=left border=0 name=image16><BR clear=left><BR><BR><\/P><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m2a245acb.png\" width=131 align=left border=0 name=image17><BR clear=left><BR><BR><\/P><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m49c4f872.png\" width=131 align=left border=0 name=image18><BR clear=left><BR><BR><\/P><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m734eb420.png\" width=131 align=left border=0 name=image19><BR clear=left><BR><BR><\/P><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m3ff8af64.png\" width=131 align=left border=0 name=image20><BR clear=left><BR><BR><\/P><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_db69705.png\" width=131 align=left border=0 name=image21><BR clear=left><SPAN class=mainText>Nokia 6230 cell phone<\/SPAN><\/P><\/TD><\/TR><\/THEAD><br \/><TBODY><\/TBODY><\/TABLE><br \/><P><BR><\/P><br \/><P><SPAN class=mainText>The above JSP script is very straightforward. Most of the code has been covered earlier. Below shows some lines of code that you may be unfamiliar with.<\/SPAN><\/P><br \/><P><SPAN class=mainText>The line:<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=codeText>&lt;% response.setContentType(&#8220;application\/vnd.wap.xhtml+xml&#8221;); %&gt;<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=mainText>is used to set the MIME type of the JSP document. &#8220;application\/vnd.wap.xhtml+xml&#8221; is the MIME type of XHTML MP.<\/SPAN><\/P><br \/><P><SPAN class=mainText>The line:<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=codeText>fileName = FilenameUtils.getName(fileName);<\/SPAN><\/P><SPAN class=codeText><br \/><P><SPAN class=mainText>is used to extract the file name from a path. (<I>FilenameUtils<\/I> is a class of the <I>org.apache.commons.io<\/I> package in the Apache Jakarta Commons IO library.) For example, both <I>FilenameUtils.getName(&#8220;\/files\/myFile.txt&#8221;)<\/I> and <I>FilenameUtils.getName(&#8220;myFile.txt&#8221;)<\/I> return the string &#8220;myFile.txt&#8221;. The above line of code is necessary in our JSP script since some browsers provide the full path of the uploaded file in the HTTP request and so after the execution of the following line, the <I>fileName<\/I> variable may contain a path but not a file name.<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=codeText>String fileName = fileItem.getName();<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=mainText>As XHTML MP is compatible with HTML\/XHTML, the resulting XHTML MP document generated by the JSP script can also be viewed on web browsers such as Microsoft Internet Explorer and Mozilla Firefox. The only thing you need to do is to remove the following line from the JSP script:<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=codeText>&lt;% response.setContentType(&#8220;application\/vnd.wap.xhtml+xml&#8221;); %&gt;<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><SPAN class=mainText>This is because unlike WAP 2.0 browsers on cell phones, Internet Explorer 6 and Mozilla Firefox 2.0 do not understand the MIME type of XHTML MP. Instead of displaying the XHTML MP document, they will pop up a dialog box asking you to select a program to open the document or save the document on disk.<\/SPAN><\/P><br \/><P><SPAN class=mainText>The following screenshots show the result on Mozilla Firefox 2.0:<\/SPAN><\/P><br \/><P><BR><\/P><br \/><P><br \/><TABLE cellSpacing=0 cellPadding=4 width=\"100%\" border=0><br \/><COLGROUP><br \/><COL width=256><br \/><THEAD><br \/><TR><br \/><TD vAlign=top width=\"100%\"><br \/><P><IMG height=460 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m645fb144.png\" width=478 align=left border=0 name=image22><BR clear=left><BR><BR><\/P><br \/><P><IMG height=460 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_81f4269.png\" width=478 align=left border=0 name=image23><BR clear=left><SPAN class=mainText>Mozilla Firefox 2.0<\/SPAN><\/P><\/TD><\/TR><\/THEAD><br \/><TBODY><\/TBODY><\/TABLE><\/SPAN><\/P><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below shows a JSP file upload script that is used to print out the name-value pair received from the earlier XHTML MP document and save the uploaded file to a certain location on the WAP server. Remember to change the action attribute of the &lt;form&gt; element in the earlier XHTML MP document from file_upload.php to &hellip; <a href=\"https:\/\/www.strongd.net\/?p=248\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Example JSP File Upload Script<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}