Hai all,
I wrote a program to upload a file into specified directory and store the file path and username into database. for that i created a html page having browse option and text box i used commons-fileupload and commons-io APIs.
while i enterd username and upload file path
it uploaded successfully but in database table it gives null value for username and filepath was inserted successfully
is there any enctype="multipart/form-data" issue in case of giving null value for text field username
can any one help me
Thanks&Regards P.SriDivya
Please visit the following link:
Hi Everyone,
I wrote a program to upload an image file into specified directory. In my jsp page i take two input box one as text type and other one is file type as follows.
<input type="text" name="CategoryName"> <input type="file" name="txtImgName"> <form action="uploadservlet" enctype="multipart/form-data" method="POST"> <input type="text" name="CategoryName"> <input type="file" name="txtImgName"><br> <input type="Submit" value="Upload File"><br> </form> public class Commonsfileuploadservlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ private static final String TMP_DIR_PATH = "c:\\tmp"; private File tmpDir; private static final String DESTINATION_DIR_PATH = "/files"; private File destinationDir; @Override public void init(ServletConfig config) throws ServletException { super.init(config); tmpDir = new File(TMP_DIR_PATH); tmpDir.getName(); System.out.println("inside 0124 init function########## " + tmpDir.getName()); if (!tmpDir.isDirectory()) { System.out.println("inside 1111111111"); throw new ServletException(TMP_DIR_PATH + " is not a directory"); } else { System.out.println("tmpDir.isDirectory() is directory......"); } System.out.println("after 22222#########"); String realPath1 = getServletContext().getRealPath(DESTINATION_DIR_PATH); System.out.println("realpath 111111111 - > " + realPath1); String realPath = "d:\\tmp"; System.out.println("after 3333######### real path" + realPath); destinationDir = new File(realPath); System.out.println("after 444#########"); if (!destinationDir.isDirectory()) { System.out.println("insideeeeeeeee"); throw new ServletException(DESTINATION_DIR_PATH + " is not a directory"); } else { System.out.println("not directry again***********"); } System.out.println("after destination#########"); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String test=""; test=request.getParameter("temptest"); System.out.println("test---- > "+test); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB /* * Set the temporary directory to store the uploaded files of size above threshold. */ fileItemFactory.setRepository(tmpDir); ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); try { List items = uploadHandler.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString()); } else { //Handle Uploaded files. out.println("Field Name = " + item.getFieldName() + ", File Name = " + item.getName() + ", Content type = " + item.getContentType() + ", File Size = " + item.getSize()); String catadd = "s" + "16" + ".png"; // File file = new File(destinationDir, item.getName()); File file = new File(destinationDir, catadd); item.write(file); System.out.println("File write successfullyYYYYYY...0201.....###################"); } out.close(); } } catch (FileUploadException ex) { log("Error encountered while parsing the request", ex); } catch (Exception ex) { log("Error encountered while uploading file", ex); } } in above servlet i m not able to get "CategoryName" value of jsp input box.but when i remove <enctype="multipart/form-data"> like : <form action="Commonsfileuploadservlet" method="POST">
then i get the "CategoryName" value.can anyone please suggest me what thing i m missing?
thanks Ashwani
Hi evryone,
I wrote a program to upload an image file into specified directory. In my jsp page i take two input box one as text type and other one is file type as follows.
<input type="text" name="CategoryName"> <input type="file" name="txtImgName"> <form action="uploadservlet" enctype="multipart/form-data" method="POST"> <input type="text" name="CategoryName"> <input type="file" name="txtImgName"><br> <input type="Submit" value="Upload File"><br> </form> public class Commonsfileuploadservlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ private static final String TMP_DIR_PATH = "c:\\tmp"; private File tmpDir; private static final String DESTINATION_DIR_PATH = "/files"; private File destinationDir; @Override public void init(ServletConfig config) throws ServletException { super.init(config); tmpDir = new File(TMP_DIR_PATH); tmpDir.getName(); System.out.println("inside 0124 init function########## " + tmpDir.getName()); if (!tmpDir.isDirectory()) { System.out.println("inside 1111111111"); throw new ServletException(TMP_DIR_PATH + " is not a directory"); } else { System.out.println("tmpDir.isDirectory() is directory......"); } System.out.println("after 22222#########"); String realPath1 = getServletContext().getRealPath(DESTINATION_DIR_PATH); System.out.println("realpath 111111111 - > " + realPath1); String realPath = "d:\\tmp"; System.out.println("after 3333######### real path" + realPath); destinationDir = new File(realPath); System.out.println("after 444#########"); if (!destinationDir.isDirectory()) { System.out.println("insideeeeeeeee"); throw new ServletException(DESTINATION_DIR_PATH + " is not a directory"); } else { System.out.println("not directry again***********"); } System.out.println("after destination#########"); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String test=""; test=request.getParameter("temptest"); System.out.println("test---- > "+test); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB /* * Set the temporary directory to store the uploaded files of size above threshold. */ fileItemFactory.setRepository(tmpDir); ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); try { List items = uploadHandler.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString()); } else { //Handle Uploaded files. out.println("Field Name = " + item.getFieldName() + ", File Name = " + item.getName() + ", Content type = " + item.getContentType() + ", File Size = " + item.getSize()); String catadd = "s" + "16" + ".png"; // File file = new File(destinationDir, item.getName()); File file = new File(destinationDir, catadd); item.write(file); System.out.println("File write successfullyYYYYYY...0201.....###################"); } out.close(); } } catch (FileUploadException ex) { log("Error encountered while parsing the request", ex); } catch (Exception ex) { log("Error encountered while uploading file", ex); } } in above servlet i m not able to get "CategoryName" value of jsp input box.but when i remove <enctype="multipart/form-data"> like : <form action="Commonsfileuploadservlet" method="POST">
then i get the "CategoryName" value.can anyone please suggest me what thing i m missing?
thanks Ashwani
Ads