<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Upload File</title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <td>Select File : </td> <td><input name="file" type="file"/> </td> </tr> <tr> <td>Enter Filename : </td> <td><input type="text" name="photoname" size="20"/> </td> </tr> </table> <p/> <input type="submit" value="Upload File"/> </form> </body> </html>
package servlets; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet(name="UploadServlet", urlPatterns={"/upload"}) // specify urlPattern for servlet @MultipartConfig // specifies servlet takes multipart/form-data public class UploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // get access to file that is uploaded from client Part p1 = request.getPart("file"); InputStream is = p1.getInputStream(); // read filename which is sent as a part Part p2 = request.getPart("photoname"); Scanner s = new Scanner(p2.getInputStream()); String filename = s.nextLine(); // read filename from stream // get filename to use on the server String outputfile = this.getServletContext().getRealPath(filename); // get path on the server FileOutputStream os = new FileOutputStream (outputfile); // write bytes taken from uploaded file to target file int ch = is.read(); while (ch != -1) { os.write(ch); ch = is.read(); } os.close(); out.println("<h3>File uploaded successfully!</h3>"); } catch(Exception ex) { out.println("Exception -->" + ex.getMessage()); } finally { out.close(); } } // end of doPost() } // end of UploadServlet
http://localhost:8080/javaee6demo/upload.jsp