Form Processing Problem

A forum to discuss Java programming related issues and concerns.
no avatar
Posts: 1
Joined: Thu Apr 18, 2013 2:21 pm

Form Processing Problem

by lusandas007 » Thu Apr 18, 2013 2:26 pm

I am trying to create a Circular Page. This is the Code where the circular is updated and asks for Circular Reference number and date.

Code: Select all
<%@ page import="*" %>
        //to get the content type information from JSP Request Header
        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 =, totalBytesRead,formDataLength);
                        totalBytesRead += byteRead;
                                        String file = new String(dataBytes);
                //for saving the file name
                String saveFile = file.substring(file.indexOf("filename=\"") + 10);
                saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

                File f=new File("C:/Program Files/Apache Software Foundation/Apache Tomcat 7.0.34/bin/UploadedFiles/"+saveFile); //the changed code->1 

                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(f);

                //FileOutputStream fileOut = new FileOutputStream(saveFile);
                fileOut.write(dataBytes, startPos, (endPos - startPos));
                                %><Br><table border="2"><tr><td><b>You have successfully  upload the file by the name of:</b>
                                            <!--<br><a href="viewFiles.jsp">View Files</a>-->
                                            **<br><br><br><form action="viewFiles.jsp" method="POST">
Circular Reference#: <input type="text" name="first_name">
<br />
Circular Date: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
                <% out.println(saveFile); %></td></tr></table>

and below is the code where it will view the inputs taken like Circular Reference and date viewFiles.jsp

Code: Select all
<%@ page import="*"%>
<tr><th>Circular Name</th><th>Download File</th>
File f = new File("C:/Program Files/Apache Software Foundation/Apache Tomcat 7.0.34/bin/UploadedFiles/");
        File[] files = f.listFiles();
        for(int i=0;i<files.length;i++){
            String name=files[i].getName();
            String path=files[i].getPath();
<tr><td><%= request.getParameter("first_name")%></td><td><%= request.getParameter("last_name")%></td><td><%=name%></td><td><a href="download.jsp?f=<%=path%>">Download File</a></td></tr>

The problem while displaying according to the code.. If we upload 3 files for all the files it will display the same circular date and reference as the variable is not unique for each inputs taken. Kindly help me, to take unique inputs from forms and the inputs will be saved when I open viewFiles.jsp. So that I can enter new Circular Reference and Date for other files uploaded and save it, so that it can be displayed in columns like

Circular reference Circular Date Files Download Files

Return to JAVA Lobby