이 기사는 참조를 위해 struts2 프레임 워크의 파일 업로드를 구현하는 방법을 공유합니다. 특정 내용은 다음과 같습니다
struts2 구성 프로세스
(1) 프로젝트에 JAR 패키지를 추가하십시오
(2) web.xml에서 필터 (필터) 구성
<? xml version = "1.0"encoding = "utf-8"?> <web-app version = "2.5"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_2_50 <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecuteFilter </filter-class> </필터> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </필터 매핑> </web-app>
(3) 쓰기 struts.xml 구성 파일
<? xml version = "1.0"encoding = "utf-8"?> <Web-App version = "2.5"xmlns = "http://java.sun.com/xml/ns/javaee"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xsi : skemalocati on = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name> </display-name> <필터 > <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecuteFilter </filt ER-Class> </filter> <Filter-Mapping> <Filter-Name> struts2 </filter-name> <Url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(4) 행동 수업의 글쓰기
패키지 com.xmgc.sc.action; import java.io.file; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.ioexception; import org.apache.struts2.servletactionContext; 공개 클래스 myUploadAction {개인 문자열 제목; 비공개 파일 업로드; // 형식의 파일 유형의 이름은 동일한 개인 문자열 업로드 ContentType입니다. // 접두사는 위의 개인 문자열 업로드 uploadFileName이어야합니다. 공개 문자열 getTitle () {return title; } public void settitle (문자열 제목) {this.title = title; } 공개 파일 getUpload () {return ubload; } public void setUpload (파일 업로드) {this.upload = 업로드; } public String getUploadContentType () {return uploadContentType; } public void setUploadContentType (String uploadContentType) {this.uploadContentType = uploadContentType; } public String getUploadFilename () {return uploadFilename; } public void setUploadFileName (String uploadFilename) {this.uploadFilename = uploadFilename; } /* public string getSavePath () {// servletContext cxt = servletActionContext.getServletContext (); // String path = cxt.getRealPath ( "/"); // 이것은 경로를 가져옵니다 : http : // localhost : 8080/sc // 업로드 후, 그것은 e : /software/apache-tomcat-6.0.45/webapps/sc return savepath가됩니다. } public void setsavepath (String savepath) {//e:/efoftware/apache-tomcat-6.0.45/webapps/sc/myupload this.savepath = servletactionContext.getServletContext (). getRealPath ( "/myUpload"); }*/public string execute ()는 ioexception {system.out.println (title); // title system.out.println (uploadContentType); // 파일의 파일 유형을 업로드 할 파일의 파일 유형 (uploadfilename); // extension system (uploadfilename); String RealPath = servletActionContext.getServletContext (). getRealPath ( "/"); 문자열 path = realPath+"myUpload/"+uploadFilename; System.out.println (RealPath); System.out.println (Path); fileInputStream fis = 새로운 fileInputStream (업로드); fileoutputStream fos = 새 FileOutputStream (path); BYTE [] BYTES = NEW BYTE [1024]; // 1024 크기의 바이트 배열을 정의합니다 int len = -1; // 플래그 비트로 사용하기 위해 사용합니다 ((len = fis.read (bytes))> 0) {fos.write (bytes, 0, len); } return null; }}(5) JSP 페이지 작성
<%@ page contenttype = "text/html; charset = utf-8"%> <!-enctarpe = "multipart/form-data", 이것은 가장 중요한 구성-> <form action = "myupload.action"enctype = "multupart/form-data"method = "post"> 파일 이름 : <입력 유형 = "tept"/> <bl "/>>> <input type ="text " 이름 = "업로드"/> <br/> <입력 유형 = "제출"값 = "업로드"/> </form>
이 요약 후에는 Struts2 프레임 워크 아래에서 단일 파일을 업로드하는 것이 여전히 매우 간단하다고 생각합니다. 주소를 저장 한 다음 입력 및 출력 스트림을 통해이 주소에 작성하십시오. 대부분의 작업에서 Struts2는 우리를 위해 그것을했습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.