<%@ 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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)