One, Struts2 file upload:
Second, the size of the configuration file and the type of files allowed to upload:
Three, large file upload:
If you do not configure the size of the upload file, struts2 default allows uploading files up to 2M; 2097152Byte; example implementation:
Com.cy.action.FileUploadAction.java:
Packagecom.cy.action;ImportJava.io.File;Importorg.apache.commons.io.FileUtils;ImportCom.opensymphony.xwork2.ActionSupport; Public classFileuploadactionextendsactionsupport{Private Static Final LongSerialversionuid = 1L; PrivateFile struts;//file, corresponding to the name of input type=file in fileupload.jsp PrivateString Strutsfilename;//file name PrivateString Strutscontenttype;//File Type PublicFile getstruts () {returnstruts; } Public voidsetstruts (File struts) { This. Struts =struts; } PublicString Getstrutsfilename () {returnStrutsfilename; } Public voidsetstrutsfilename (String strutsfilename) { This. Strutsfilename =Strutsfilename; } PublicString Getstrutscontenttype () {returnStrutscontenttype; } Public voidSetstrutscontenttype (String strutscontenttype) { This. Strutscontenttype =Strutscontenttype; } @Override PublicString Execute ()throwsException {System.out.println ("File name:" +strutsfilename); System.out.println ("File type:" +Strutscontenttype); String FilePath= "e:/" +Strutsfilename; File SaveFile=NewFile (FilePath); Fileutils.copyfile (struts, savefile); returnSUCCESS; } }
Struts.xml:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.or G/dtds/struts-2.0.dtd "><Struts> <!--configuration allows uploading files up to 20000000Byte, approx. 20Mb - <constantname= "struts.multipart.maxSize"value= "20000000"></constant> < Packagename= "Manage"extends= "Struts-default"> <Actionname= "Upload"class= "Com.cy.action.FileUploadAction"> <resultname= "Input">fileupload.jsp</result> <resultname= "Success">success.jsp</result> <!--Configure the file types allowed to upload the file size allowed to upload, maximum 81101byte,= 79.2KB - <Interceptor-refname= "FileUpload"> <paramname="Allowedtypes" >Image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param> <paramname="MaximumSize" >81101</param> </Interceptor-ref> <Interceptor-refname= "Defaultstack"></Interceptor-ref> </Action> </ Package> </Struts>
FILEUPLOAD.JSP:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@taglib prefix="s"URI="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body> <!--Failure message is returned when a file upload error is displayed - <S:fielderror></S:fielderror> <formAction= "Upload"Method= "POST"enctype= "Multipart/form-data">Select File:<inputtype= "File"name= "Struts" /><BR/> <inputtype= "Submit"value= "Submit" /> </form></Body></HTML>
SUCCESS.JSP:
< Body > File Upload Successful! </body>
View Code
Test results:
1) Configure the types of files that are allowed to be uploaded, and the file size;
2) configuration of large file upload, the above example is configured to about 20M;
If you do not configure a large file upload, upload more than 2M will be an error:
------------
Struts2 Learning (struts2) file upload and download (1)