1.UploadAction代码:多文件上传
- package com.hxz.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.struts2.ServletActionContext;
- import com.hxz.exception.NameException;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- public class UploadAction extends ActionSupport {
- private String title;
- private File[] upload;
- private String[] uploadContentType;
- private String[] uploadFileName;
- private String savePath;
- 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 getSavePath() throws Exception {
- return ServletActionContext.getRequest().getRealPath(savePath);
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String execute() throws Exception {
- File[] files = getUpload();
- for(int i=0;i<files.length;i++){
- try {
- InputStream is = new FileInputStream(files[i]);
- File file = new File(this.getSavePath(), this.getUploadFileName()[i]);
- OutputStream os = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = is.read(buffer)) > 0) {
- os.write(buffer, 0, len);
- }
- os.close();
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw new NameException("上传文件错误!");
- }
- }
- return "zhe";
- }
- }
2.upload.jsp
- <%@ page language="java" pageEncoding="GBK"%>
- <%@ taglib prefix ="s" uri="/struts-tags" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>Upload File</title>
- </head>
- <body>
- <form action="uploadMany.action" method="post" enctype="multipart/form-data">
- <s:fielderror/>
- ${requestScope.typeError} <br>
- 文件标题:<input type="text" name="title"/><br>
- 选择1文件:<input type="file" name="upload"/><br>
- 选择2文件:<input type="file" name="upload"/><br>
- 选择3文件:<input type="file" name="upload"/><br>
- <input type="submit" value="上传"/>
- </form>
- </body>
- </html>
3.strust.xml,采用fileUpload拦截器实现文件类型和大小验证
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.custom.i18n.resources" value="globalMessages"/>
- <constant name="struts.i18n.encoding" value="utf-8"/>
- <package name="lee" extends="json-default">
- <global-exception-mappings>
- <!-- 指Action抛出AuctionException异常时,转入名为exception的结果。 -->
- <exception-mapping exception="com.hxz.excepton.NameException" result="exception"/>
- <!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
- <exception-mapping exception="java.lang.Exception" result="exception"/>
- </global-exception-mappings>
- <action name="readName" class="com.hxz.action.NameAction">
- <result name="success">/testJstl.jsp</result>
- <result name="error">/error.jsp</result>
- </action>
- <action name="getNameById" class="com.hxz.action.NameAction" method="getNameById">
- <result name="success" type="json">/name.html</result>
- <result name="error">/error.jsp</result>
- </action>
- <action name="updateName" class="com.hxz.action.NameAction" method="updateName">
- <result name="success" >/name.html</result>
- <result name="error">/error.jsp</result>
- </action>
- <action name="upload" class="com.hxz.action.UploadAction">
- <interceptor-ref name="fileUpload">
- <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>
- <param name="maximumSize">2000</param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack"/>
- <param name="savePath">/upload</param>
- <result name="zhe">/upload/succ.jsp</result>
- <result name="input">/upload/upload.jsp</result>
- </action>
- </package>
- </struts>
4.成功页面succ.jsp
- <%@ page language="java" pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>succ.jsp</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- 文件标题:<s:property value="+title"/><br>
- <img src="<s:property value="uploadFileName[0]"/>" />
- <img src="<s:property value="uploadFileName[1]"/>" />
- <img src="<s:property value="uploadFileName[2]"/>" />
- </body>
- </html>