-------------------------------------------------------------------------------- Hi Actually i have downloaded a JAR file. I have also placed the jar file in jspbook/WEB-INF/lib/ folder . I am trying file upload through a HTML file which has following code. actually i am accessing servlet FileUploadCommons though this HTML file and using an API for file upload present in that JAR file.
<html> <head> <title>Example HTML Form</title> </head> <body> <p>Select a file to upload or <a href="/jspbook/files/">browse currently uploaded files.</a></p> <form action="http://127.0.0.1/jspbook/FileUploadCommons" method="post" enctype="multipart/form-data"> File: <input type="file" name="file"><br> <input value="Upload File" type="submit"> </form> </body> </html>
The source code for servlet FileUploadCommons is as follows ::::
response.setContentType("text/html"); PrintWriter out = response.getWriter();
out.println("<html>"); out.print("File upload success. <a href=\"/jspbook/files/"); out.print("\">Click here to browse through all uploaded "); out.println("files.</a><br>");
ServletContext sc = getServletContext(); String path = sc.getRealPath("/files"); org.apache.commons.fileupload.FileUpload fu = new org.apache.commons.fileupload.FileUpload(); fu.setSizeMax(-1); fu.setRepositoryPath(path); try { List l = fu.parseRequest(request); Iterator i = l.iterator(); while (i.hasNext()) { FileItem fi = (FileItem)i.next(); // trim out full path info if it is included String filename = fi.getName(); int slash = filename.lastIndexOf("\\"); if (slash != -1) { filename = filename.substring(slash + 1); } // write the file to the 'files' directory fi.write(path+"/"+filename); } } catch (Exception e) { throw new ServletException(e); }