JSPとSerlvetを使用して、参照用の最も簡単なアップロードを実現します。特定のコンテンツは次のとおりです
1。ページindex.jsp
<%@ページ言語= "java" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> index.jsp </title> <meta http-equiv = "pragma" content = "no-cache"> content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <br> Name: <input type="text" name="uname"/> <br> Upload file:<input type="file" name="pic"/> <br> <input type="submit" value = "submit" "> </input> </form> </body> </html>
2。アクションがサブレットのアップロードにジャンプするため、Web.xml、web.xmlで構成する必要があります。
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instcance" xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet> <servlet-name> uploadaction </servlet-name> <Servlet-Class> com.pop.action.smartuploadaction </servlet-class> </servlet> <servlet-mapping> <servlet-name> uploadaction </servlet-name> <url-pattern>/upload/*</url-pattern> </servet-mapping> <welcome-file> <welcome-list>
3.アクションファイルへのマップ、smartuploadaction.java:
パッケージcom.pop.action; java.io.ioexceptionをインポートします。 javax.servlet.servletexceptionをインポートします。 javax.servlet.http.httpservletをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 javax.servlet.jsp.jspfactoryをインポートします。 javax.servlet.jsp.pagecontextをインポートします。 com.soft4j.httpupload4j.requestをインポートします。 com.soft4j.httpupload4j.smartupload; com.soft4j.httpupload4j.smartuploadexception;パブリッククラスSmartuploadactionはhttpservletを拡張します{private static final long serialversionuid = -861055375032925108l; @Override Protected void dopost(httpservletrequest req、httpservletresponse rep)servletexception、ioexception {req.setcharacterencoding( "utf-8"); rest.setcharacterencoding( "utf-8"); smartupload su = new smartupload(); // MultiPart/Form-Dataの送信により、Reqは使用できないため、Smartuploadによって生成された要求が使用されますreqest = su.getRequest(); // pageContextオブジェクトを取得pagecontext pagecontext = jspactory.getDefaultFactory().getPageContext(this、req、resp、null、true、8192、true); su.Initialize(pagecontext); try {su.upload(); //このプロジェクトのアップロードディレクトリにアップロードsu.save( "upload"); } catch(smartuploadexception e){e.printstacktrace(); } // smartuploadによって生成されたreqestオブジェクトを使用して、ページ文字列uname = reqest.getParameter( "uname")で渡されたパラメーターを取得します。 System.out.println(uname); }}最終的な説明:使用されるコンポーネントパッケージはSmartupload.zipです。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。