JSP upload File failed -
i going build simple jspfile handle upload files using servlet. when comes execution, shows following message
demo fail: org.apache.catalina.connector.requestfacade cannot cast org.apache.tomcat.util.http.fileupload.requestcontext
would please tell me how convert http request requestcontext ?
the below code jsp front-page
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title test upload</title> </head> <body> <form action="uploadfile" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" /> </form> </body> </html>
the below code servlet
import java.io.*; import java.net.url; import java.net.urlconnection; import java.net.urlencoder; import java.sql.*; import java.util.enumeration; import java.util.iterator; import java.util.list; import javax.servlet.*; import javax.servlet.http.*; import org.apache.tomcat.util.http.fileupload.fileitem; import org.apache.tomcat.util.http.fileupload.fileitemfactory; import org.apache.tomcat.util.http.fileupload.fileuploadexception; 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; /** * servlet implementation class getlogin */ public class uploadfile extends httpservlet { private static final long serialversionuid = 17864986468494864l; // location store file uploaded private static final string upload_directory = "upload"; // upload settings public uploadfile() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub //dopost(request, response); //throw new servletexception("get method used " + getclass( ).getname( )+": post method required."); request.getrequestdispatcher("/web-inf/upload.jsp").forward(request, response); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println("demo"); if (!servletfileupload.ismultipartcontent(request)) { // if not, stop here printwriter writer = response.getwriter(); writer.println("error: form must has enctype=multipart/form-data."); writer.flush(); return; } // configures upload settings diskfileitemfactory factory = new diskfileitemfactory(); // sets temporary location store files factory.setrepository(new file(system.getproperty("java.io.tmpdir"))); servletfileupload upload = new servletfileupload(factory); // constructs directory path store upload file // path relative application's directory string uploadpath = getservletcontext().getrealpath("")+ file.separator + upload_directory; // creates directory if not exist file uploaddir = new file(uploadpath); if (!uploaddir.exists()) { uploaddir.mkdir(); } try { // parses request's content extract file data system.out.println(uploadpath); list<fileitem> formitems = upload.parserequest((requestcontext)request); if (formitems != null && formitems.size() > 0) { // iterates on form's fields (fileitem item : formitems) { // processes fields not form fields if (!item.isformfield()) { string filename = new file(item.getname()).getname(); string filepath = uploadpath + file.separator + filename; file storefile = new file(filepath); // c:\tomcat\apache-tomcat-7.0.40\webapps\data\ // saves file on disk item.write(storefile); request.setattribute("message","upload has been done successfully!"); system.out.println("demo success"); } } } } catch (exception ex) { request.setattribute("message","there error: " + ex.getmessage()); system.out.println("demo fail: " + ex.getmessage() ); } } }
this error attemp on tomcat 7 , servlet 3.0 when getting parameters form enctype=multipart/form-data without using standard
@webservlet("/yourpattern") @multipartconfig public class uploadfile extends httpservlet { //...
then use part object
part filepart = request.getpart("file"); // retrieves <input type="file" name="file"> inputstream filecontent = filepart.getinputstream();
instead of
list<fileitem> formitems = upload.parserequest((requestcontext)request); if (formitems != null && formitems.size() > 0) { // iterates on form's fields (fileitem item : formitems) { if (!item.isformfield()) { //...
see other post further information.
Comments
Post a Comment