Question
So my professor provided us with code to upload a file, however, I am getting an error on my machine (through eclipse) on line 71
So my professor provided us with code to upload a file, however, I am getting an error on my machine (through eclipse) on line 71 of this code:
package hw;
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List;
import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem; import org.apache.tomcat.util.http.fileupload.RequestContext; import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory; import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
@WebServlet("/upload") public class upload extends HttpServlet { private static final long serialVersionUID = 1L;
public upload() { super(); }
public void init(ServletConfig config) throws ServletException { }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used) ServletContext servletContext = this.getServletConfig() .getServletContext(); File repository = (File) servletContext .getAttribute( "javax.servlet.context.tempdir" ); factory.setRepository( repository );
// Create a new file upload handler ServletFileUpload upload = new ServletFileUpload( factory );
// Count how many files are uploaded int count = 0; // The directory we want to save the uploaded files to. String fileDir = getServletContext().getRealPath( "/WEB-INF/files" );
// Parse the request try { List
} catch( Exception e ) { throw new IOException( e ); }
response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "
" + count + " file(s) uploaded to " + fileDir ); out.println( "" ); } }
the error resides within the doPost of the try catch:
I would like a fix for this because my professor is pulling the "works fine on my machine" excuse.
This is the error when clicked prompted in eclipse "The method parseRequest(RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)"
An alternative servlet for uploading is also welcome.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started