Write a servlet to upload file on server.
Answer / Basant Singh Yadav
To write a Servlet for file upload, you need to implement the doPost method and use apache commons FileUpload API. Here's a simple example:nn```javanimport java.io.*;nimport javax.servlet.*;nimport javax.servlet.http.*;nimport org.apache.commons.fileupload.*;nimport org.apache.commons.fileupload.disk;npublic class FileUploadServlet extends HttpServlet {n private String saveDir = "uploads/";n public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {n DiskFileUpload upload = new DiskFileUpload();n List items = upload.parseRequest(request);n Iterator iter = items.iterator();n FileItem item;n while (iter.hasNext()) {n item = (FileItem)iter.next();n if (!item.isFormField()) {n String path = saveDir + FileUtils.getFileName(item.getName());n File file = new File(path);n item.write(file);n }n }n }n}
| Is This Answer Correct ? | 0 Yes | 0 No |
What do you mean by cgi?
How an Image can be loaded in a Servlet ?
How a servlet is unloaded?
What is servlet and how it works?
Which event is fired at the time of session creation and destroy?
Explain the war file?
What is servlet and its use?
can i modify the data which are generated by the servlet
What are different types of SessionTracking?
Explain in detail about applet to servlet communication?
What are different ways for authentication of servlet?
What is URL Rewriting ?