Der Implementierungscode der Download -Funktion "Struts2" lautet wie folgt:
Aktionsdatei
öffentliche Klasse DownloadLoadAction erweitert actionupport { / ** * * / private statische endgültige lange Serialversionuid = 5879762231742395104L; private String -Dateiname; // Der vom Benutzer private String inputPath angeforderte Dateiname; // Der Pfad zum Herunterladen der Ressource (in der Struts -Konfigurationsdatei festgelegt) public void setInputPath (String InputPath) {this.inputPath = inputPath; } public String getInputPath () {return InputPath; } public void setFileName (String Dateiname) {this.FileName = Dateiname; } public String getFileName () {return Dateiname; } public String downloadFile () löst Ausnahme aus {ServletContext context = servletActionContext.getServletContext (); String downloadDir = context.getRealPath ("/upload"); String downloadFile = context.getRealPath (InputPath); // Benutzer daran hindern, unsichere Ressourcen anzufordern, wenn (! DownloadFile.Startswith (DownloadDir)) {return null; } return "download_success"; } /** Eingabestress-Ressource erhalten* / public InputStream getInputStream () löst Ausnahme aus {String path = inputPath + file.seParatorchar + new String (Dateiname.getBytes ("ISO-8859-1"), "UTF-8"); return ServletActionContext.getServletContext (). getResourceAsStream (Pfad); } /** Erhalten Sie den Standarddateinamen der Datei beim Herunterladen* / public String getdownloadFileName () {String downloadFileName = Dateiname; try {downloadFileName = urlencoder.encode (DownloadFileName, "ISO-8859-1"); } catch (unportedenCodingException e) {e.getMessage (); E. printstacktrace (); } return downloadFileName; }}JSP -Datei:
<li> <a href = "<%= path%>/download_downloadAction_downloadFile.Action? Dateiname = dwr.jar"> dwr.jar -Datei unter </a> </li> <li> <a href = "<%= Path%>/download_downAtion.action.action.action.Prent. </a> </li>
Struts.xml:
<action name = "Download _*_*" method = "{2}"> <param name = "inputPath">/Upload </param> <!-Ergebnistyp auf Stream-> <resultenname = "download_success" type = "stream"> <!-mime type-> <param name Eingabestream-Ressource (die GetInputStream-Methode wird in der Aktion definiert, und der Rückgabetyp ist inputStream)-> <param name = "InputName"> InputStream </param> <!-Setzen Sie die Datei mit Anhängen, um den Dateinamen dynamisch den Dateinamen zu erhalten (Definieren Sie die GetDownloadFilen-Methode in Aktion). Die Puffergröße-> <param name = "buffersize"> 2048 </param> </result> </action>Das obige ist der relevante Inhalt der Struts2 -Implementierung der Datei -Download -Funktionscodefreigabe (Dateiname Transcodierung auf Chinesisch), die Ihnen vom Editor vorgestellt wurden. Ich hoffe, es wird für alle hilfreich sein!