第30讲 .struts2多文件上传

1在项目中,HeadFirstStruts2chapter08,新建filesUpload.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="upLoad" method="post" enctype="multipart/form-data">
多文件:
<input type="file" name="cruise"/><br/>
<input type="file" name="cruise"/><br/>
<input type="file" name="cruise"/><br/>
<input type="submit" value="提交文件"/><br/>
</form>
</body>
</html>
2新建FilesUploadAction,
package com.cruise.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FilesUploadAction extends ActionSupport{

    private File[] cruise;
    private String[] cruiseFileName;
    private String[] cruiseContentType;
    
    public File[] getCruise() {
       return cruise;
    }

    public void setCruise(File[] cruise) {
       this.cruise = cruise;
    }

    public String[] getCruiseFileName() {
       return cruiseFileName;
    }

    public void setCruiseFileName(String[] cruiseFileName) {
       this.cruiseFileName = cruiseFileName;
    }

    public String[] getCruiseContentType() {
       return cruiseContentType;
    }

    public void setCruiseContentType(String[] cruiseContentType) {
       this.cruiseContentType = cruiseContentType;
    }

    @Override
    public String execute() throws Exception {

       for(int i=0; i<cruise.length;i++){
           String filePath="C:/Users/pengc/Desktop/"+cruiseFileName[i];
           File saveFile = new File(filePath);
           FileUtils.copyFile(cruise[i], saveFile);
       }
       return SUCCESS;
    }
}
3配置struts.xml文件,
<?xml version="1.0" encoding="UTF-8" ?>
<!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.multipart.maxSize" value="20000000"></constant>
    <package name="manager" extends="struts-default">
       <action name="filesUpLoad" class="com.cruise.action.FilesUploadAction">
           <result name="success">/success.jsp</result>
           <result name="input">/filesUpload.jsp</result>
       </action> 
    </package>
</struts>
4测试
http://localhost:8080/HeadFirstStruts2chapter08/filesUpload.jsp

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83928791
今日推荐