Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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( "Upload" ); out.println("

"); out.println("
"); out.println(""); out.print("
"); 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 items = upload.parseRequest( request ); for( FileItem item : items ) { // If the item is not a form field - meaning it's an uploaded // file, we save it to the target dir if( !item.isFormField() ) { // item.getName() will return the full path of the uploaded // file, e.g. "C:/My Documents/files/test.txt", but we only // want the file name part, which is why we first create a // File object, then use File.getName() to get the file // name. String fileName = (new File( item.getName() )).getName(); File file = new File( fileDir, fileName ); item.write( file ); ++count; } }

} catch( Exception e ) { throw new IOException( e ); }

response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "Upload" ); 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions