write jsp code
<form method="post" action="upload" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />
write servlet code
first include jar file orielly.cos.jar
then import this
import java.util.Enumeration;
import com.oreilly.servlet.MultipartRequest;
public class upload extends HttpServlet {
private String webTempPath;
@Override
public void init()
{
webTempPath = getServletContext().getRealPath("/") + "data";
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet upload at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new ServerException("Get method used with" + getClass().getName() + ":Post method required");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
MultipartRequest mpr = new MultipartRequest(request, webTempPath,10*1024*1024);
Enumeration enumr = mpr.getFileNames();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Server upload</title>");
out.println("</head>");
out.println("<body>");
for(int i=1;enumr.hasMoreElements();i++)
out.println("The name of the uploaded file" + i + "is" + mpr.getFilesystemName((String)enumr.nextElement()) + "<br><br>");
out.println("</body>");
out.println("</html>");
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}