Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
I want to upload a file. The requirement is , the user can browse a file and once file is selected , there are 4 check box for different users. Depending on the checkbox selected , on clicking submit the file should go and reside in a particular folder.
Kindly help

Here is my code
HTML
                     <%@ page import="java.io.*" %>


<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>  
<% //  for uploading the file we used Encrypt type of multipart/
//form-data and input of file type to browse and submit the file %>
  <BODY> <FORM  name="sop" ENCTYPE="multipart/form-data" ACTION="uploadPOD_ho.jsp" METHOD=POST>
                                <br><br><br>
                  <center><table border="2">
                    <tr><center><td colspan="2"><p align="<br" mode="hold" />"center">UPLOADING THE FILE</td></center></tr>
                    <tr><td>Choose the file To Upload:
</td>
                    <td><INPUT NAME="F1" TYPE="file"></td></tr>
                                <tr><td colspan="2"><p align="center">
                                Viewed By:
                                
                                </p></td></tr>
                                <tr>
                                <td colspan="2" style="color:blue">
                Head Office:-<input  type="checkbox" name="id" value="HO"  >
                  State Head:-<input type="checkbox" name="id" value="SH" >
                                Area Manager :-<input type="checkbox" name="id" value="AM"  >
                                Retail Outlet:-<input  type="checkbox" name="id" value="RO" >
                
                                </td></tr>
                                                                                <tr><td colspan="2">
<p align="center"> <input type="submit" value="Upload File"></p></td></tr>
             <table>
     </table></table></center> 
                 
     </FORM>
</BODY>
</HTML>
<%
                //to get the content type information from JSP Request Header
                
                
                String contentType = request.getContentType();
                String saveFile="";
                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
                                
                                saveFile = file.substring(file.indexOf("filename=\"") + 10);
                                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;
                                //out.println(""+boundaryLocation);
                                int startPos = ((file.substring(0, pos)).getBytes()).length;
                                //out.println(""+startPos);
                                int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
                                //out.println(""+endPos);
                                // creating a new file with the same name and writing the content in new file
                                String fname=saveFile.toLowerCase();
                                
                                //String path="C:\\Fuelsrv\";
                                String select[]=request.getParameterValues("id");
                   if(select!=null)
                                {
                                //out.println(""+request.getParameterValues("id"));
                                File dest=new File("C:\\FuelsrvHO");
                                if(!dest.exists())
                                                dest.mkdir();
                                FileOutputStream fileOut = new FileOutputStream("C:\\FuelsrvHO\\"+saveFile);
                                fileOut.write(dataBytes, startPos, (endPos - startPos));
                                fileOut.flush();
                                fileOut.close();
                                %>
    <br><table border="2"><tr><td style="color:green">You have successfully
     upload 
    </td></tr></table>
                <%
                                }


                else
                {
                                %>
<br><table border="2"><tr><td style="color:red">You can
upload only Web SSR Data in excel format</td></tr></table>
                                <%
                }</br></br></br></br></br>
Posted
Updated 27-Oct-12 1:16am
v2
Comments
Sandeep Mewara 27-Oct-12 14:25pm    
What help you need? Issue/error?

1 solution

 
Share this answer
 
Comments
ursatish 1-Nov-12 0:14am    
Brother i am able to upload the file.....but i want something like....before uploading i have to tick one out of 4 checkbox....and then click submit....so according to the checkbox the file should go and sit in a particular folder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900