파일 업로드
multipart/form-data 로 설정해야합니다.<input type=“file”> 필드를 추가해야합니다.<s : form action = "testupload"enctipe = "multipart/form-data"> <s : aftiffield name = "username [0]"label = "user-1"> </s : textfield> <s : "photos"label = "photos"> </s : file> <s : "usern name [1]" "user-2"> label = "photos"> </s : file> <s : textfield name = "username [2]"label = "user-3"> </s : textfield> <s : 파일 이름 = "photos"label = "photo"> </s : 파일> <s : "제출"> </s : downs> </s : form>
공개 클래스 업로드 액션은 ActionSupport { @setter @getter private list <file> 사진을 확장합니다. @setter@getter private list <string> photoscontentType; @setter@getter private list <string> photosfilename; @setter@getter private list <string> username; public String testupload ()는 ioexception {system.out.println ( "사용자 이름 :"+username)을 던졌습니다. System.out.println ( "사진 :"+사진); System.out.println ( "Photosfilename :"+photosfilename); System.out.println ( "PhotoscontentType :"+photoscontentType); // 서버 루트 디렉토리의 업로드 파일로 파일을 전달하십시오. // 실제 경로 문자열을 가져옵니다. RealPath = servletContext.getRealPath ( "/Upload"); System.out.println (RealPath); 파일 업로드 파일 = 새 파일 (realPath); // 경로가 존재하는지 (! uploadfile.exists ()) {// uploadfile.mkdir ()를 작성하십시오. } for (int i = 0; i <photos.size (); i ++) {uuid uuid = uuid.randomuuid (); fileUtils.copyFile (photos.get (i), 새 파일 (RealPath + "/" + uuid + photosfilename.get (i))); } 반환 성공; }}1. 얼마나 많은 작은 문제를 처리해야합니까?
1. 파일 이름에는 중복 이름이 있습니다. 일반적으로 파일 이름 이전에 접두사로 UUID를 생성 할 수 있습니다.
2. 단일 파일의 크기를 제한합니다
3. 파일 유형을 제한합니다
4. 총 파일 크기를 제한합니다
2. FileUpload 인터셉터는이 속성 값을 설정하기 위해 struts2로 제공됩니다.
FileUpload 인터셉터에는 설정할 수있는 3 가지 속성이 있습니다.
참고 : org.apache.struts2에서 업로드 된 파일의 총 크기에 업로드 된 파일의 총 크기에는 제한이 있습니다. 상수를 사용 하여이 한계 struts.multipart.maxsize = 2097152를 수정할 수 있습니다
<constant name = "struts.devmode"value = "true"/> <!-전체 파일의 크기를 여기에서 변경합니다-> <constant name = "struts.multipart.maxsize"value = "2097152"/> <package name = "default"namespace = "/"extends = "Struts-default"> <Intercepsor "> <interceptor-Ref name = "defaultStack"> <!-단일 파일의 크기를 변경하면 Commons FileUpload 구성 요소는 기본적으로 업로드 된 파일을 2M-> <param name = "fileUpload.maxumSize"> 57,408 </param> <!-upload로 허용하는 파일 유형입니다. 이름 = "FileUpload.allowedTypes"> image/pjpeg, image/gif </param> <!-파일 업로드를위한 확장자-> <param name = "fileUpload.allowedextensions"> jpg, gif </param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interactor-ref 이름 = "myinterceptor"> </default-interceptor-Ref> <action name = "testupload"method = "testupload"> <result name = "success">/web-inf/views/success.jsp </result> <result name = "input">/upload.jsp </result> </action> </spackation>
1. 파일 업로드와 관련된 오류 메시지?
1. 파일 업로드와 관련된 오류 메시지는 struts-messages.properties 파일에 사전 정의되어 있습니다.
2. 파일의 조치에 해당하는 리소스 파일을 업로드하거나 i18n_zh_cn.properties International Resource 파일에서 오류 메시지를 재정의 할 수 있습니다.
struts.messages.error.file.too.large = 당신이 전달하는 파일은 너무 큰 struts.messages.error.content.type.not.allowed = 파일 유형 오류 struts.messages.error.file.extension.not.allowed = Extension Error struts.messages.upload.error.sizelxexexexexmentececeed = Extension Error Struts.messages.upload.error.sizelxexexexexexcementexexmentexected.
파일 다운로드
일부 응용 프로그램에서는 파일을 사용자의 브라우저로 동적으로 보내야 할 수도 있으며 프로그래밍시 파일의 이름과 저장 위치는 예측할 수 없습니다.
샘플 코드
<a href = "TestDownload"> 다운로드 </a>
public class downloadloadaction actionSupport {// 일반적으로 다음 매개 변수는 action @setter @getter private String contenttype에 제공됩니다. @setter@getter private long contentlength; @setter@getter private String contentDisPosition; @setter@getter private inputStream inputStream; public string testdownload ()는 filenotfoundexception, unsupportedencodingexception {// servletcontext servletcontext servletcontext를 얻습니다. servletactionContext.getServletContext (); // 파일의 경로를 가져옵니다. string realpath = servletcontext.getRealPath ( "/web-inf/file/ord you.mp3"); // 파일의 스트림을 가져옵니다. inputStream = new FileInputStream (realPath); // 파일 유형 설정 contentType = servletContext.getMimeType (realPath); // 파일의 길이를 가져옵니다. contentlength = 새 파일 (realPath) .length (); // 파일 이름을 설정 문자열 filename = "적어도 you.mp3"; filename = new String (filename.getBytes ( "gbk"), "iso8859-1"); contentDisPosition = "첨부 파일; filename ="+filename; 반환 성공; }}<!-파일 다운로드-> <action name = "testdownload"method = "testdownload"> <result type = "stream"> <!-파일 버퍼 크기-> <param name = "buffersize"> 2048 </param> </result> </action>
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.