jsp html with multiple input including input type=file |
jsp html with multiple input including input type=file |
Why |
Feb 25 2010, 01:47 AM
Post
#1
|
Group: Members Posts: 1 Joined: 25-February 10 Member No.: 11,209 |
I am a newbee,
Input form has multiple input types, multiple radio button groups, and file type, The file needs to be uploaded along with file type and contents. The called jsp receives the radio button groups values , if the form is NOT set with ENCTYPE="multipart/form-data". but for a file to be upload , enc type/content type needs to be set with ENCTYPE="multipart/form-data". Is it possible to have code like this, if so whats wrong with my snippet? My code is as follows - <HTML> <BODY> <FORM ENCTYPE="multipart/form-data" METHOD=POST ACTION="single_upload_page.jsp"> <br><br><br> <center><table border="3" > <tr><center><td colspan="4" ><p align="center"><B>Upload File From User</B><center></td></tr> <tr><td><b>File Contents:</b></td> <td><input type="radio" NAME=fileContent value="Terms" /> TERMS</td> <td><input type="radio" NAME=fileContent value="Relations" /> Relations</td> <td><input type="radio" NAME=fileContent value="Both" /> Both</td> </tr> <tr><td><b>FileType:</b></td> <td><input type="radio" NAME=fileType value="csv" /> csv<br /></td> <td><input type="radio" NAME=fileType value="xml" /> xml<br /> <td><input type="radio" NAME=fileType value="owl/rdf" /> owl/rdf<br /></tr> <tr><td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="F1" TYPE="file" NAME=fileName></td> <td colspan="2"><p align="center"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr> <table> </center> </FORM> </BODY> </HTML> single_upload_page.jsp <%@ page import="java.io.*" %> <% //to get the content type information from JSP Request Header String fileType = request.getParameter("fileType"); String fileContent = request.getParameter("fileContent"); System.out.println(" FileType " + fileType + " fileContent " + fileContent ); String contentType = request.getContentType(); //here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0 if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename=\"") + 10); System.out.println(" File Name " + saveFile); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; //extracting the index of file pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; // creating a new file with the same name and writing the content in new file FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); } %><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b> <% out.println(saveFile); %></td></tr></table> <BODY> <a href = "java script:history.back(-2)">Back to previous page</a> </BODY> <% } %> |
Lo-Fi Version | Time is now: 23rd April 2024 - 05:02 AM |