struts2ファイルのダウンロード関数の実装コードは次のとおりです。
アクションファイル
public class downloadloadaction extends actionsupport { / ** * * / private static final long serialversionuid = 5879762231742395104l;プライベート文字列filename; //ユーザーが要求したファイル名プライベート文字列入力; //リソースをダウンロードするパス(struts構成ファイルに設定)public void setinputpath(string inputpath){this.inputpath = inputpath; } public string getInputPath(){return inputpath; } public void setFileName(string filename){this.filename = filename; } public string getFileName(){return filename; } public string downloadFile()スロー例外{servletcontext context = servletactionContext.getServletContext();文字列downloaddir = context.getRealPath( "/upload");文字列downloadfile = context.getRealPath(inputPath); //ユーザーが危険なリソースを要求しないようにしますif(!downloadfile.startswith(downloaddir)){return null; } "download_success"を返します。 } /**入力ストリームリソースを取得* / public inputstream getInputStream()スロー例外{string path = inputpath + file.separatorchar + new String(filename.getBytes( "ISO-8859-1")、 "utf-8"); return servletactioncontext.getServletContext()。getResourceasStream(PATH); } /**ダウンロードするときにファイルのデフォルトファイル名を取得* / public string getDownLoadFileName(){String DownloadFileName = fileName; try {downloadfilename = urlencoder.encode(downloadfilename、 "iso-8859-1"); } catch(unsupportedencodingexception e){e.getMessage(); e.printstacktrace(); } downloadFileNameを返します。 }}JSPファイル:
<li> <a href = "<%= path%>/download_downloadaction_downloadfile.action?filename = dwr.jar"> dwr.jarファイル</a> </li> <li> <%= path%>/download_downloadaction_downloadfile.action = firuts2チュートリアル。 </a> </li>
struts.xml:
<アクションname = "download _*_*" method = "{2}"> <param name = "inputpath">/upload </param> <! - resultタイプセットになります - > <result name = "download_success" type = "stream"> <! - mime type-> <param name = "contenttype"> application入力ストリームリソース(getInputStreamメソッドはアクションで定義され、リターンタイプはinputstreamです) - > <param name = "inputname"> inputstream </param> <! - ファイルを動的に取得するために添付ファイルを設定します(getdownloadfilenameメソッドをアクションのgetdownfileNameメソッドを定義します) - > <param name = "contentdistion"バッファサイズを設定 - > <param name = "buffersize"> 2048 </param> </result> </action>上記は、編集者によって紹介されたファイルダウンロード機能コード共有(中国語のファイル名トランスコーディング)のstruts2実装の関連コンテンツです。私はそれが誰にでも役立つことを願っています!