1、概述
2、文件上传
singleUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload/doUpload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"> <input type="submit" value="上传"> </form> </body> </html>
struts.xml
<package name="upload" namespace="/upload" extends="struts-default"> <action name="doUpload" class="com.ljb.web.upload.UploadAction" method="doUpload"> <result>/singleSuccess.jsp</result> </action> </package>
UploadAction:
package com.ljb.web.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private File upload; private String uploadContentType; private String uploadFileName; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = 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 doUpload () throws IOException { System.out.println(upload); System.out.println(uploadContentType); System.out.println(uploadFileName); // 获取保存文件路径 String savePath = ServletActionContext.getServletContext().getRealPath("/upload/" + this.uploadFileName); System.out.println(savePath); FileInputStream fis = new FileInputStream(upload); FileOutputStream fos = new FileOutputStream(savePath); IOUtils.copy(fis, fos); fos.flush(); fos.close(); fis.close(); return SUCCESS; } }
singleSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String contextPath = request.getContextPath()+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 文件上传成功! <img alt="" src="<%=contextPath %>upload/<s:property value='uploadFileName'/>" /> </body> </html>
小结:
3、限制文件上传大小
第一种方式:(在struts中配置常量限制文件上传大小,默认2M)
<constant name="struts.multipart.maxSize" value="10000000"></constant>
第二种方式:设置拦截器参数
4、上传多个文件
样例:
multieFileUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function addFile () { var btn = document.getElementById("btn"); var newChild = document.createElement("input"); newChild.setAttribute("type", "file"); newChild.setAttribute("name", "upload"); btn.parentNode.appendChild(newChild); btn.parentNode.appendChild(document.createElement("br")); } </script> </head> <body> <form action="upload/multiFile" method="post" enctype="multipart/form-data"> <input type="file" name="upload"> <input type="button" value="继续添加" id="btn" onclick="addFile();"> <input type="submit" value="上传"><br> </form> </body> </html>
struts.xml
<package name="upload" namespace="/upload" extends="struts-default"> <action name="doUpload" class="com.ljb.web.upload.UploadAction" method="doUpload"> <result>/singleSuccess.jsp</result> <result name="input">/error.jsp</result> </action> <action name="multiFile" class="com.ljb.web.upload.MutipleFileUploadUsingListAction" method="doUpload"> <result>/multiSuccess.jsp</result> </action> </package>
mutipleFileUploadUsingListAction
package com.ljb.web.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class MutipleFileUploadUsingListAction extends ActionSupport { private List<File> upload; private List<String> uploadContentType; private List<String> uploadFileName; public String doUpload () throws IOException { System.out.println(upload.get(0)); for (int i = 0 ; i < upload.size() ; i++) { String savePath = ServletActionContext.getServletContext().getRealPath("/upload/" + this.uploadFileName.get(i)); FileInputStream fis = new FileInputStream(upload.get(i)); FileOutputStream fos = new FileOutputStream(savePath); IOUtils.copy(fis, fos); fos.flush(); fos.close(); fis.close(); } return SUCCESS; } public List<File> getUpload() { return upload; } public void setUpload(List<File> upload) { this.upload = upload; } public List<String> getUploadContentType() { return uploadContentType; } public void setUploadContentType(List<String> uploadContentType) { this.uploadContentType = uploadContentType; } public List<String> getUploadFileName() { return uploadFileName; } public void setUploadFileName(List<String> uploadFileName) { this.uploadFileName = uploadFileName; } }