struts配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <!-- 上传文件的总开关,配置上传文件的大小,默认值是2M -->
- <constant name="struts.multipart.maxSize" value="7097152"></constant>
- <package name="upload" namespace="/upload" extends="struts-default">
- <action name="uploadAction_*" class="cn.itcast.upload.UploadAction" method="{1}">
- <interceptor-ref name="defaultStack">
- <!-- 设置上传文件的参数 这里属于第二开关-->
- <!-- 设置上传文件的大小,struts2运行时,会调用FileUploadInterceptor中的setMaximumSize(6097152)方法-->
- <param name="fileUpload.maximumSize">1097152</param>
- <!-- 设置上传文件的允许类型,struts2运行时,会调用FileUploadInterceptor中的setAllowedTypes(...)方法-->
- <param name="fileUpload.allowedTypes">application/vnd.ms-excel,application/vnd.ms-powerpoint</param>
- <!-- 设置上传文件的允许扩展名,struts2运行时,会调用FileUploadInterceptor中的setAllowedExtensions(,,,)方法-->
- <param name="fileUpload.allowedExtensions">txt,ppt</param>
- </interceptor-ref>
- <result name="success">/upload/success.jsp</result>
- <!-- input:固定写法,当上传出错时,要转向input所指向的页面 -->
- <result name="input">/upload/error.jsp</result>
- </action>
- <action name="uploadsAction_*" class="cn.itcast.upload.UploadsAction" method="{1}">
- <result name="success">/upload/success.jsp</result>
- <!-- input:固定写法,当上传出错时,要转向input所指向的页面 -->
- <result name="input">/upload/error.jsp</result>
- </action>
- </package>
- </struts>
单文件上传action:
- package cn.itcast.upload;
- import java.io.File;
- import java.io.IOException;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- public class UploadAction extends ActionSupport {
- //<input type="file" name="uploadImage">,保存上传文件
- private File uploadImage;
- //文件的类型,定义格式:jsp页面上传的文件名+ContentType,这里是uploadImage+ContentType=uploadImageContentType
- private String uploadImageContentType;
- //文件的名字:定义格式jsp页面上传的文件名+FileName,这里是uploadImage+FileName=uploadImageFileName
- private String uploadImageFileName;
- public String saveFile(){
- System.out.println("xxxxxxxxxx");
- ServletContext sc=ServletActionContext.getServletContext();
- String realpath=sc.getRealPath("/pic");
- File file=new File(realpath,uploadImageFileName);
- try {
- FileUtils.copyFile(uploadImage, file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "success";
- }
- public File getUploadImage() {
- return uploadImage;
- }
- public void setUploadImage(File uploadImage) {
- this.uploadImage = uploadImage;
- }
- public String getUploadImageContentType() {
- return uploadImageContentType;
- }
- public void setUploadImageContentType(String uploadImageContentType) {
- this.uploadImageContentType = uploadImageContentType;
- }
- public String getUploadImageFileName() {
- return uploadImageFileName;
- }
- public void setUploadImageFileName(String uploadImageFileName) {
- this.uploadImageFileName = uploadImageFileName;
- }
- }
多文件上传action:
- package cn.itcast.upload;
- import java.io.File;
- import java.io.IOException;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- public class UploadsAction extends ActionSupport {
- //<input type="file" name="uploadImage">,保存上传文件
- private File[] uploadImages;
- //文件的类型,定义格式:jsp页面上传的文件名+ContentType,这里是uploadImage+ContentType=uploadImageContentType
- private String[] uploadImagesContentType;
- //文件的名字:定义格式jsp页面上传的文件名+FileName,这里是uploadImage+FileName=uploadImageFileName
- private String[] uploadImagesFileName;
- public String saveFiles(){
- ServletContext sc=ServletActionContext.getServletContext();
- String realpath=sc.getRealPath("/pic");
- try {
- if(uploadImagesContentType!=null&&uploadImagesContentType.length>0){
- for(int i=0;i<uploadImagesContentType.length;i++){
- File file=new File(realpath,uploadImagesFileName[i]);
- FileUtils.copyFile(uploadImages[i], file);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "success";
- }
- public File[] getUploadImages() {
- return uploadImages;
- }
- public void setUploadImages(File[] uploadImages) {
- this.uploadImages = uploadImages;
- }
- public String[] getUploadImagesContentType() {
- return uploadImagesContentType;
- }
- public void setUploadImagesContentType(String[] uploadImagesContentType) {
- this.uploadImagesContentType = uploadImagesContentType;
- }
- public String[] getUploadImagesFileName() {
- return uploadImagesFileName;
- }
- public void setUploadImagesFileName(String[] uploadImagesFileName) {
- this.uploadImagesFileName = uploadImagesFileName;
- }
- }