struts2 파일 다운로드 함수의 구현 코드는 다음과 같습니다.
액션 파일
공개 클래스 다운로드로드 액션은 ActionSupport를 확장합니다 { / ** * * / private static final long serialversionuid = 5879762231742395104L; 개인 문자열 filename; // 사용자가 요청한 파일 이름 개인 문자열 inputpath; // 리소스를 다운로드하는 경로 (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; } return "download_success"; } /** 입력 스트림 리소스 받기* / public inputStream getInputStream () 예외 {문자열 path = inputpath + file.separatorchar + new String (filename.getBytes ( "ISO-8859-1"), "UTF-8"); return servletactionContext.getServletContext (). getResourCeasStream (Path); } /** 다운로드 할 때 파일의 기본 파일 이름을 가져옵니다. try {downloadfilename = urlencoder.encode (downloadfilename, "iso-8859-1"); } catch (UnsupportedEncodingException e) {e.getMessage (); e.printstacktrace (); } return downloadFileName; }}JSP 파일 :
<li> <a href = "<%= path%>/download_downloadtaction_downloadfile.action? filename = dwr.jar"> dwr.jar 파일 </a> </li> <li> <a href = "<%= path%>/download_downloadaction_downloadfile.action? filename = struts2 Tutter. </li>
struts.xml :
<action name = "download _*_*"method = "{2}"> <param name = "inputpath">/upload </param> <!-결과 유형은 스트림으로 설정-> <result name = "download_success"type = "stream"> <!-mime type-> <param name = "contenttype"> application </param> <! 입력 스트림 리소스를 얻으십시오 (getInputStream 메소드는 동작에서 정의되어 있으며 리턴 유형은 inputStream입니다)-> <param name = "inputName"> inputStream </param> <!-파일 이름을 동적으로 가져 오려면 파일을 설정합니다 (getDownloadFilename 메소드를 정의)-> 이름 = "contentDisPosition"> 첨부 파일; filename = "$ {downloadfilename}"</param> <!-버퍼 크기를 설정-> <param name = "buffersize"> 2048 </param> </result> </action>위의 내용은 편집자가 소개 한 파일 다운로드 함수 코드 공유 (중국어로 트랜스 코딩)의 구현의 관련 내용입니다. 나는 그것이 모두에게 도움이되기를 바랍니다!