File upload
multipart/form-data<input type=“file”> field needs to be added.<s:form action="testUpload" enctype="multipart/form-data"> <s:textfield name="userName[0]" label="user-1"></s:textfield> <s:file name="photos" label="photos"></s:file> <s:textfield name="userName[1]" label="user-2"></s:textfield> <s:file name="photos" label="photos"></s:file> <s:textfield name="userName[2]" label="user-3"></s:textfield> <s:file name="photos" label="photo"></s:file> <s:submit value="submit"></s:submit></s:form>
public class UploadAction extends ActionSupport{ @Setter@Getter private List<File> photos; @Setter@Getter private List<String> photosContentType; @Setter@Getter private List<String> photosFileName; @Setter@Getter private List<String> userName; public String testUpload() throws IOException { System.out.println("userName: "+userName); System.out.println("photos: "+photos); System.out.println("photosFileName: "+photosFileName); System.out.println("photosContentType: "+photosContentType); // Pass the file to the upload file in the server root directory// Get ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); // Get the real path String realPath = servletContext.getRealPath("/upload"); System.out.println(realPath); File uploadFile = new File(realPath); //Judge whether the path exists if (!uploadFile.exists()){ //Create uploadFile.mkdir(); } for (int i = 0; i < photos.size(); i++) { UUID uuid = UUID.randomUUID(); FileUtils.copyFile(photos.get(i), new File(realPath + "/" + uuid + photosFileName.get(i))); } return SUCCESS; }}1. How many small problems should be dealt with?
1. File name has a duplicate name. Generally, a UUID can be generated before the file name as a prefix.
2. Limit the size of a single file
3. Restrict file types
4. Limit the total file size
2. The FileUpload interceptor is provided in Struts2 to set these attribute values.
The FileUpload interceptor has 3 properties that can be set.
Note: There is a limit on the total size of uploaded files in default.properties under org.apache.struts2. You can use constants to modify this limit struts.multipart.maxSize=2097152
<constant name="struts.devMode" value="true"/> <!-- Change the size of the total file here --> <constant name="struts.multipart.maxSize" value="2097152"/> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor-stack name="myInterceptor"> <interceptor-ref name="defaultStack"> <!-- Change the size of a single file, the Commons FileUpload component accepts uploaded files by default is 2M --> <param name="fileUpload.maximumSize">57,408</param> <!-- File type allowed to upload --> <param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param> <!-- Extensions for uploading files --> <param name="fileUpload.allowedExtensions">jpg,gif</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="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></package>
1. Error messages related to uploading files?
1. Error messages related to file upload are predefined in the struts-messages.properties file.
2. You can upload the resource file corresponding to the Action in the file or redefine the error message in the i18n_zh_CN.properties international resource file
struts.messages.error.file.too.large=The file you passes is too large struts.messages.error.content.type.not.allowed=File type error struts.messages.error.file.extension.not.allowed=Extension error struts.messages.upload.error.SizeLimitExceededException=Total file size exceeds the upper limit
File download
In some applications, it may be necessary to send a file to the user's browser dynamically, and the name and storage location of the file are unpredictable when programming
Sample code
<a href="testDownLoad">Download</a>
public class DownloadLoadAction extends ActionSupport{ //Usually the following parameters will be provided in the Action @Setter@Getter private String contentType; @Setter@Getter private long contentLength; @Setter@Getter private String contentDisposition; @Setter@Getter private InputStream inputStream; public String testDownLoad() throws FileNotFoundException, UnsupportedEncodingException { //Get ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); //Get the path of the file String realPath = servletContext.getRealPath("/WEB-INF/file/at least you.mp3"); //Get the stream of the file inputStream = new FileInputStream(realPath); //Set the file type contentType = servletContext.getMimeType(realPath); //Get the length of the file contentLength = new File(realPath).length(); //Set the file name String fileName = "At least you.mp3"; fileName = new String(fileName.getBytes("gbk"),"iso8859-1"); contentDisposition = "attachment;filename="+fileName; return SUCCESS; }}<!-- File Download--><action name="testDownLoad" method="testDownLoad"> <result type="stream"> <!-- File Buffer Size--> <param name="bufferSize">2048</param> </result></action>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.