이 기사는 참조를 위해 struts2에서 파일 다운로드 함수를 공유했습니다. 특정 내용은 다음과 같습니다
다음은 이미지 다운로드 기능을 구현하는 예입니다.
1. 프로젝트 구조
2. web.xml
<? xml version = "1.0"encoding = "utf-8"?> <web-app version = "3.0"xmlns = "http://java.sun.com/xml/xml/ns/javaee"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance. xsi : schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_3_0.xsd <filter-name> struts 2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class> </필터> <filter-mapping> <filter-name> struts 2 </filter-name> <url-pattern>/* <!-설정 환영 페이지-> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <!-세션 타임 아웃 정의, 단위 내 단위-> <session-config> <session-timeout> 30 </session-config> </web-app>
3. downloadaction.java
패키지 com.action; import java.io.inputstream; import org.apache.struts2.servletactionContext; com.opensymphony.xwork2.actionSupport 가져 오기; 공개 클래스 다운로드 ActionSupport {private static final long serialversionuid = 1L; // 파일 경로 개인 문자열 경로; // 경로 속성의 getter 메소드 public String getPath () {return Path; } // 경로 속성의 세터 메소드 public void setpath (문자열 경로) {this.path = path; } // return inputStream, 파일을 다운로드하는 키 메소드 public java.io.inputStream getDownloadFile () 예외 {inputStream in = servletActionContext.getServletContext (). getResourCeasStream (getResourCeasStream ()); 반환; } public string execute ()는 예외 {return success; }}4.Struts.xml
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype struts public "-// apache Software Foundation // dtd struts 구성 2.1 // en" ""http://struts.apache.org/dtds/struts-2.1.dtd "> <!-struts 2의 Configure Constants re- <! 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"> <!-파일 경로의 매개 변수를 설정하고 액션 클래스 파일에 전달하고 action 클래스 파일에 전달합니다-<param name = "path">/a.jpg </param>-> <!-파일 유형 정의 다운로드, 즉 "stream"-> <result name = "type ="type = "strame"> <speg jpeg. 이름 = "contentType"> image/jpeg </param> <!-다운로드 파일 처리 방법-> <param name = "contentDisPosition"> <!-첨부 파일은 첨부 파일을 의미합니다. 또는 인라인을 작성하십시오. 즉, 인라인을 의미합니다. 즉, 다운로드-> 첨부 파일; filename = "a.jpg"</param> <!-파일 출력 스트림 정의 다운로드-> <!-여기에 입력 이름 요소에 해당하는 값은 다운로드 파일입니다. 작업에서 해당 GetDownloadFile () 메소드-> <param name = "inputname"> downloadfile </param> <!-다운로드 버퍼 크기-> <param name = "buffersize"> 1024 </param> </result> </action> </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 전환 // en"> <html> <head> <base href = "<%= basepath%>"rel = "external nofollow"> <title> </title> </head> <body> <br <br/<body> <broffage <br <br/<body> <링크를 클릭하십시오. href = "download.action? path = < %="rel = "external nofollow"./download/a.jpg " %>"> 다운로드 </a> </a> </a> </a>
6. 파일 경로
다운로드 디렉토리는 프로젝트에서 미리 생성해야하며 그 안에는 a.jpg 파일이 있어야합니다. 그렇지 않으면 다운로드가 실패합니다.
7. 기능적 포털
프로젝트가 서버에 게시되면 브라우저를 사용하여 프로젝트에서 index.jsp에 액세스하고 다운로드 링크를 클릭하면 "다운로드"대화 상자가 나타납니다.