コードを見てください、コメントはすべて中にあります
まず、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-Class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value> classpath:applicationContext.xml </parm-value> </init-on-startup> <Servlet-Name> upload </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <説明>パラメーター配信の文字化けの問題を解決</described> <filtercodingutf8 </filter-name> <filter-class> org.filtter.cringereancework.filtter.chercterter.chramecterecterwork。 <param-name> encoding </param-name> <param-value> utf-8 </param-value> </init-param> </filter> <filter-mapping> <filter-name> charapleencodingutf8 </filter-name> <url-pattern>/*</url-pattern> </filt-mapping> </web-app>
以下は//src//applicationcontext.xmlにあります
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework/ http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!ベースパッケージで指定) - > <! - ここでは、私の記事を参照できますhttp://blog.csdn.net/jadyer/article/details/6038604-> <Context:component-scan base-package = "com.jadyer"/>> < exceptionResolver-> <MVC:Annotation-driven/> <! - springMVCはすべてのリクエストを傍受するために設定されているため、この構成を介して静的リソースファイルを読み取るときは読み取られません。場所= "/js/"/> <mvc:リソースマッピング= "/upload/**" location = "/upload/"/> <! - springmvcでファイルをアップロードするときは、マルチパートルソルバープロセッサを構成する場合、> <bean id = "multipartresolver"> <! maxUploadSize property is not for a single file, but for the sum of the capacity of all files--> <property name="maxUploadSize" value="800000"/> </bean> <!-- When SpringMVC exceeds the upload file limit, it will throw org.springframework.web.multipart.MaxUploadSizeExceededException --> <!-- This exception was thrown by springmvcアップロードされたファイル情報をチェックするとき、および現時点でコントローラーメソッドを入力していません - > <bean id = "exceputionResolver"> <プロパティ名= "exceptionmappings"> <props> <! - maxuploadsizeex cuceedexception例外に遭遇したとき、それは自動的に/web-inerror_filefopload.jspに自動的にジャンプします。 key = "org.springframework.web.multipart.maxuploadsizeexexexception"> error_fileupload </prop> </props> </property> </bean> <bean> <プロパティ名= "prefix" value = "/web-inf/jsp/"/>> <property name = "
ファイルコンテンツのアップロードが大きすぎる場合のプロンプトページ//web-inf//jsp//error_fileupload.jsp
<%@ページ言語= "java" pageencoding = "utf-8"%>
<h1>ファイルが大きすぎるので、再選択してください</h1>
以下は、ファイルindex.jspを選択するためのアップロードページです
<%@ page Language = "java" pageEncoding = "utf-8"%> <! - これは<スクリプトタイプ= "text/javascript" src = ".." // js/jquery-1.10.2.min.js "> </script> <script =" text/javaScript "" src = "<%= request.getContextPath()%>/js/ajaxfileupload.js"> </script> <script> <text/javascript "src =" <%= request.getContextpath()%>/js/ajaxfileupload.js " ajaxfileupload(){//ファイルのアップロード時に画像が表示され、ファイルのアップロードが完了して画像を非表示にします( "#loading")。 //ファイルアップロード操作のサーバーアドレス(パラメーターを渡すことができ、個人的にテストできます) DataType: 'Text'、//サーバーによって返される形式はJSONまたはXMLなどです。 // ajaxfileuploadは、サーバーが返信するテキストコンテンツに<pre>テキスト</pre>の接尾辞を追加します。 data = data.replace( "</pre>"、 ''); data = data.replace( "<pre>"、 ''); data = data.replace( "</pre>"、 ''); data = data.replace( "</pre>"、 ''); //この例では、アップロードファイルが完了した後、サーバーは前景[0`filepath]に戻ります(data.substring(0、1)== 0){// 0はアップロード後のファイルパスを超えることを意味します(アップロード後にファイルパスを超える)、1は障害を意味します(障害記述を超える)$( "IMG [ID = 'Uploadimage']" $( '#result')。html( "画像アップロード<br/>"); } else {$( '#result')。html( '画像アップロードが失敗しました、もう一度やり直してください!!'); }}、error:function(data、status、e){//処理関数$( '#result')。html( '画像のアップロードが失敗しました、再試行!!'); }});} </scrip> <div id = "result"> </div> <img id = "uploadimage" src = "http://www.firefox.com.cn/favicon.ico"> <入力タイプ= "ファイル" id = "myblogimage" name = "myfiles"/> <input opt = "" botun onclick = "ajaxfileupload()"/> <! - ajaxfileuploadの公式ウェブサイトの紹介:http://phpletter.com/our-projects/ajaxfileupload/introduction:jqueryプラグインAjaxfileuploadは、更新されたファイルを達成できます。多くのユーザーがいます。推奨する価値があります。JSを導入する順序(jQueryに依存します)で、ページにフォームはありません(ボタンがクリックされたときのAjaxFileupload()メソッドのみがトリガーされます)共通エラーとソリューションは次のとおりです)SyntaxError:Missing;ステートメントの前 - URLパスにアクセスできるかどうかを確認します$エラーは、上記の無効なエラープロンプトよりもはるかに便利です - >最後に、ファイルアップロードを処理するFileuploadController.java
パッケージcom.jadyer.controller; import java.io.file; import java.io.ioexception; import java.io.printwriter; import javax.servlet.httpservletrequest; import javax.servlet.http.httpsertrespons; import.pach.comons. springframework.stereotype.controller; Import org.springframework.web.bind.annotation.Requestmapping; Import org.springframework.web.bind.annotation.requestparam; Import org.springframework.web.wbc* 1) SpringMVCはCommons-Fileupload実装を使用します。最初にコンポーネントをプロジェクトに導入する必要があります * 2)SpringMVC構成ファイルのMultiPartresolverプロセッサを構成する必要があります(アップロードされたファイルの属性制限はここに追加できます) * 3)。 enctype = "multipart/form-data"および<input type = "file" name = "****"/>)*ps:ajaxfileupload.jsは更新されないアップロードを実現するために使用されるため、この例ではフォームは使用されません* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Spring-Context-3.2.4.Release.jar * Spring-Core-3.2.4.Release.jar * Spring-Expression-3.2.4.Release.jar * Spring-Jdbc-3.2.4.Release.jar * Spring-oxm-3.2.4.Release.jar * Spring-3.2.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4.4. Spring-Web-3.2.4.Release.jar * Spring-Web-3.2.4.Release.jar * Spring-Web-3.2.4.Release.jar * Spring-Webmvc-3.2.4.Release.jar * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ファイルをアップロードした後、フォアグラウンド[0`filepath]に戻ると、0はアップロードされた(アップロードされたファイルパスのシーケント)を意味します。 httpservletrequestリクエスト、httpservletresponse応答)throws ioexception {//ファイルsystem.out.println( "受信ユーザー[ + uname +"]ファイルアップロードリクエスト "); // Tomcatサーバーを使用している場合、ファイルは//%tomcat_home%にアップロードされます// webapps // yourwebproject // upload // folder // commons.io.fileutilsクラスはファイルアップロード操作を実装するために使用されます。アップロードが存在するかどうかを自動的に決定します。存在しない場合、文字列realpath = request.getSession()。getServletContext()。getRealPath( "/upload"); //フォアグラウンドのコンテンツ応答にデータ形式の応答を設定します。SetContentType( "Text/Plain; charset = utf-8"); //フォアグラウンドコンテンツに応答するprintwriterオブジェクトを設定しますprintwriter out = respons.getWriter(); //アップロードファイルの元の名前(つまり、アップロードする前のファイル名)文字列originalFileName = null; // 1つのファイルをアップロードするだけの場合、ファイルをMultiPartFileタイプで受信する必要があります。また、@RequestParam Annotationを明示的に指定する必要はありません。//複数のファイルをアップロードする場合は、ファイルを受信してファイルを受信し、@RequestParam Annotation // Front int the file int the inpolding intecly int infer in the file "// myfiles、それ以外の場合は、パラメーター内のmyfilesが(multipartfile myfile:myfiles)のすべてのアップロードされたファイルを取得できません{if(myfile.isempty()){out.print( "1`ファイルを選択してアップロードする"); out.flush(); nullを返します。 } else {originalfileName = myfile.getoriginalFileName(); System.out.println( "ファイル元名:" + originalFileName); System.out.println( "ファイル名:" + myfile.getName()); System.out.println( "ファイル長:" + myfile.getSize()); System.out.println( "ファイルタイプ:" + myfile.getContentType()); System.out.println( "================================================================================================ System.out.println( "========================================================================================================== ========================================================================================= ========================================================================================= ========================================================================================== {file [" + oferityfilename +"] e.printstacktrace(); [d:/develod/apache-tomcat-6.0.36/webapps/ajaxfileupload//upload/anger birds.jpg] //system.out.println(realpath + "//" + originalfilename) //system.out.println(request.getContextPath() + "/upload/" + originalFileName); // [RealPath + "//" + originalFileName]の推奨事項は推奨されません// OriginalFileName); out.flush(); nullを返します。 }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。