この記事は、参照のためにStruts2の下のファイルのダウンロード機能を共有しています。特定のコンテンツは次のとおりです
画像のダウンロード関数を実装する例は次のとおりです。
1。プロジェクト構造
2。Web.xml
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "3.0" 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_0.xsd"> <filter-name> struts 2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.streutsecutefilter </filter-class> </filter> <フィルターマッピング> <filter-name> struts 2 </filterna>ページ - > <Welcome-File-List> <Welcome-File> index.jsp </welcome-file> </welcome-file-list> <! - セッションタイムアウト定義、単位のユニット - > <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> </web-app>
3.downloadaction.java
パッケージcom.action; java.io.inputStreamをインポートします。 Import org.apache.struts2.servletactionContext; com.opensymphony.xwork2.actionsupportをインポートします。パブリッククラスのダウンロードextends ActionSupport {private static final long serialversionuid = 1l; //ファイルパスプライベート文字列パス。 // getter method of path属性public string getpath(){return path; } //パスのセッターメソッド属性public void setpath(string path){this.path = path; } // inputStreamを返し、ファイルをダウンロードするための重要な方法public java.io.inputStream getDownLoadFile()スロー例外{inputstream in = servletactionContext.getSourcontext()。戻ります; } public string execute()throws Exception {return success; }}4.struts.xml
<?xml version = "1.0" encoding = "utf-8"?> <!doctype struts public " - // apacheソフトウェア財団// dtd struts構成2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd" name = "struts.i18n.encoding" value = "utf-8" /> <! - アップロードされたファイルの最大容量を構成し、struts2はデフォルトで2mです。ユニットは1b、1kb = 1024b、1m = 1024kb、1m = 1024*1024b - > <constant name = "struts.multipart.maxsize" value = "1048576" /> <! - このアプリケーションでパッケージを構成し、Struts-Defaultパッケージを継承します - > extends = "struts-default"> <action name = "download"> <! - ファイルパスのパラメーターを設定し、アクションクラスファイルに渡します - > <! - <param name = "path =" path ">/download/a.jpg </param> - > <! - ダウンロードファイルタイプ定義、つまり、「ストリーム」として定義されています。 name = "contentType"> image/jpeg </param> <! - ファイル処理方法をダウンロード - > <param name = "contentDisposition"> <! - 添付ファイルは添付ファイル、つまりダウンロード中に開かれ、ファイル名はダウンロード中に表示されるファイル名を表します。またはインラインを書きます。インラインを意味します。つまり、ダウンロード - >添付ファイル、filename = "a.jpg" </param> <! - ダウンロードファイル出力ストリーム定義 - > <! - 入力名に対応する値をダウンロードファイルです。アクションでは、対応するgetDownLoadFile()メソッド - > <param name = "inputname"> downloadfile </param> <! - ダウンロードバッファーサイズ - > <param name = "buffersize"> 1024 </param> </action> </package> </struts>
5.index.jsp
<%@ page Language = "java" import = "java.util。*" 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%>" rel = "external nofollow"> <title> home </title> </head> <br/<br/<br/> href = "download.action?path = <%=" rel = "external nofollow" ./ download/a.jpg "%>">ダウンロード</a> </center> </body> </html>
6。ファイルパス
ダウンロードディレクトリはプロジェクトで事前に作成する必要があり、その中にa.jpgファイルがあるはずです。そうしないと、ダウンロードが失敗します。
7。機能ポータル
プロジェクトがサーバーに公開されたら、ブラウザを使用してプロジェクトのindex.jspにアクセスし、[ダウンロード]リンクをクリックすると、[ダウンロード]ダイアログボックスがポップアップします。