この記事では、参照用のStruts2フレームワークのファイルアップロードを実装する方法を共有しています。特定のコンテンツは次のとおりです
struts2構成プロセス
(1)プロジェクトにJARパッケージを追加します
(2)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 http://java.sun.com/xml/ns/ns/javaee/web-app_55.xsd"> display-name> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.stretspareandexecutefilter </filter-class> </filter> <フィルターマッピング> <filter-name> struts2 </filternam> <urtern> </url-mappn>
(3)struts.xml構成ファイルの書き込み
<?xmlバージョン= "1.0" encoding = "utf-8"?> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocati on = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name> </display-name> <フィルター> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.streutsecutefilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filternam
(4)アクションクラスの執筆
パッケージcom.xmgc.sc.アクション; Import java.io.file; Import java.io.fileinputStream; Import java.io.fileoutputStream; Import java.io.ioexception; Import org.apache.struts2.servletactionContext;パブリッククラスmyuploadaction {private string title;プライベートファイルアップロード; //フォームフォームのファイルファイルタイプの名前は同じプライベート文字列uploadContentType; //上記のアップロードプライベート文字列uploadFileNameでなければなりません。 public string getTitle(){return title; } public void settitle(string title){this.title = title; } public file getupload(){return upload; } public void setupload(file upload){this.upload = upload; } public string getuploadContentType(){return uploadContentType; } public void setuploadContentType(String uploadContentType){this.uploadContentType = uploadContentType; } public string getuploadfileName(){return uploadFileName; } public void setuploadfileName(String uploadFileName){this.uploadfileName = uploadFileName; } /* public string getSavePath(){// servletcontext cxt = servletactionContext.getServletContext(); // string path = cxt.getRealPath( "/"); //これはパスを取得します:http:// localhost:8080/sc //アップロード後、e:/software/apache-tomcat-6.0.45/webapps/sc return savepathになります。 } public void setSavePath(String SavePath){//e:/software/apache-tomcat-6.0.45/webapps/sc/myupload this.savepath = servletactionContext.getServletContext()。getRealPath( "/myupload"); }*/public string execute()throws ioException {system.out.println(title); // title system.out.println(uploadcontenttype); //アップロードされるファイルのファイルタイプ。 string realpath = servletactionContext.getServletContext()。getRealPath( "/"); string path = realpath+"myupload/"+uploadFileName; System.out.println(RealPath); system.out.println(path); fileInputStream fis = new FileInputStream(upload); fileoutputStream fos = new fileoutputStream(path); byte [] bytes = new byte [1024]; // 1024サイズのバイト配列を定義します。 } nullを返します。 }}(5)JSPページの書き込み
<%@ page contentType = "text/html; charset = utf-8"%> <! - enctype = "multipart/form-data"、これは最も重要な構成です - > <form action = "myupload.action" enctype = "multipart/form-data" method = "post"> <input " type = "file" name = "upload"/> <br/> <input type = "submit" value = "upload"/> </form>
この要約の後、Struts2フレームワークの下に単一のファイルをアップロードすることはまだ非常に簡単だと感じています。アドレスを保存するだけで、入力ストリームと出力ストリームを介してこのアドレスに書き込みます。ほとんどの作業では、Struts2が私たちのためにそれをしました。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。