1. Struts2 환경을 구축하십시오
MyEclipse에서 프로젝트를 마우스 오른쪽 버튼으로 클릭하십시오 -> MyeClipse-> 프로젝트 패싯 -> Apache Struts2를 설치하십시오.
직접 빌드하려면 struts2 패키지를 다운로드하고 struts.xml 구성 파일을 작성해야합니다.
web.xml 파일 구성은 다음과 같습니다.
<filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsprepareAndexecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-panttern> </필터 매핑>
2. 파일 업로드
1. 프론트 데스크 페이지 :
페이지 업로드 :
<body> <form action = "upload.action"method = "post"encType = "multipart/form-data"> <input type = "file"name = "upload"/> <input type = "제출"value = "제출"/> <br> $ {result} </form> </body>입력 이름 속성은 배경 이름 지정과 일치합니다.
실패한 페이지 업로드 :
<body> <h2> 업로드 실패 </h2> <s : fielderror> </s : fielderror> </body>
필요:
<%@ taglib uri = "/struts-tags"prefix = "s"%>
2. 백엔드 조치
기본 속성 업로드, 업로드 ContentType, uploadFilename.
패키지 com.yf.action; import java.io.file; import org.apache.commons.io.fileutils; import org.apache.struts2.servletactionContext; import com.opensymphony.xwork2.actionSupport; public class uploadaction acctionsupport {개인 파일 업로드; 비공개 문자열 업로드 컨텐츠 타입; 개인 문자열 uploadFilename; 개인 문자열 결과; 공개 파일 getUoLoad () {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 getResult () {return result; } public void setResult (문자열 결과) {this.result = 결과; } @override public string execute ()는 예외를 {string path = servletactionContext.getServletContext (). getRealPath ( "/images"); 파일 = 새 파일 (Path); if (! file.exists ()) {file.mkdir (); } system.out.println (업로드); fileUtils.copyFile (업로드, 새 파일 (파일, uploadFilename)); 결과 = "성공적으로 업로드"; 반환 성공; }}3.Struts.xml 파일 구성
업로드 된 파일의 유형 및 크기를 제한하도록 작업을 구성하고 인터셉터를 구성하십시오.
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype struts public "-// apache Software Foundation // dtd struts configuration 2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd"> value = "false"/> <constant name = "struts.devmode"value = "true"/> <constant name = "struts.multipart.savedir"value = "/tmp"/> <constant name = "struts.custom.i18n.resources"value = "app"> <action name = "upload"> <cults>/index.jsp </result> <result name = "input">/error.jsp </result> <!-업로드 된 파일의 유형과 크기를 제한하도록 인터셉터 구성-> <interceptor-ref name = "fileUpload"> <parame name = "allucttypes"> image/bmp, image/x-png, im name = "maximsize"> 2m </param> </interceptor-ref> <interceptor-ref name = "defaultStack"> </interceptor-ref> </action> </package> </struts>
4. 새 속성 파일을 만듭니다
파일 업로드 실패 정보가 전경에 표시되며 처리 할 때 오류 메시지가 표시됩니다.
파일 내용은 다음과 같습니다.
struts.messages.error.file.too/large =/u4e0a/u4f20/u6587/u4ef6/u592a/u5927/u4e86/uff01struts.m essages.error.content.type.not.allowed =/u4e0a/u4f20/u6587/u4ef6/u7c7b/u578b/u4e0d/u7b26/uff01
그것은 추가하는 것입니다 :
이름 : struts.messages.error.file.too/large
값 : 업로드 파일이 너무 큽니다!
이름 : struts.messages.error.content.type.not.allowed
값 : 업로드 된 파일 유형이 일치하지 않습니다!
작업 결과는 다음과 같습니다.
JPG 이미지 선택, 실행 후 크기가 2m를 초과하지 않습니다.
비 Picture 파일을 선택하십시오.
배치로 파일을 업로드하고 배경 업로드를 변경하고 업로드하고 UPLOADTENTTYPE 및 UPLOADFILENAME를 목록에 변경하고 루프에서 업로드 파일을 읽고 하드 디스크에 저장하고 프론트 데스크에 입력을 추가하면 이름 속성이 동일합니다.
위는 편집기가 소개 한 struts2 파일 업로드 기능의 예제입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨주세요. 편집자는 제 시간에 모든 사람에게 답장 할 것입니다!