Struts2_文件上传_Demo

来源:互联网 发布:万得for mac 编辑:程序博客网 时间:2024/05/19 20:20
package org.com.test.action;import java.io.File;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.Date;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.io.FileUtils;import org.com.test.dao.MessageDao;import org.com.test.model.Message;import org.com.test.model.Point;import com.opensymphony.xwork2.ModelDriven;public class MessageAction implements ModelDriven<Message>{private Point point;private MessageDao messageDao = new MessageDao();private Message msg;private Date endDate;/** * 如果传的是单个文件,应该这样子。 * private File photo;//这个名称跟form表单中的name一样;   private String photoFileName;//文件名称   private String photoContentType;//文件类型 *//** * 如果上传多个文件就用数组 */private File[] photo;//这个名称跟form表单中的name一样;private String[] photoFileName;//文件名称private String[] photoContentType;//文件类型public String[] getPhotoFileName() {return photoFileName;}public void setPhotoFileName(String[] photoFileName) {this.photoFileName = photoFileName;}public String[] getPhotoContentType() {return photoContentType;}public void setPhotoContentType(String[] photoContentType) {this.photoContentType = photoContentType;}public File[] getPhoto() {return photo;}public void setPhoto(File[] photo) {this.photo = photo;}public MessageDao getMessageDao() {return messageDao;}public void setMessageDao(MessageDao messageDao) {this.messageDao = messageDao;}public Date getEndDate() {return endDate;}public void setEndDate(Date endDate) {this.endDate = endDate;}public Point getPoint() {return point;}public void setPoint(Point point) {this.point = point;}public Message getMsg() {return msg;}public void setMsg(Message msg) {this.msg = msg;}public String addInput(){return "success";}public String add(){return "success";}public String updateInput(){try {/** * 此时栈中第0个root是Model,而Model是中的Message空的。 */Message m = messageDao.load();BeanUtils.copyProperties(msg, m);} catch (IllegalAccessException | InvocationTargetException e) {e.printStackTrace();}//msg = messageDao.load();return "success";}@Overridepublic Message getModel() {if (msg==null) {msg = new Message();}return msg;}public String fileInput(){return "success";}public String file(){/*System.out.println(photo.getName());System.out.println(photoFileName+";        "+photoContentType);*/try {/** * commons io 包中的方法;将form表单提交上来的源文件,复制到一个新的文件中。 *///FileUtils.copyFile(photo, new File("d:/test/"+photoFileName));for (int i = 0; i < photo.length; i++) {File f = photo[i];String fileName = photoFileName[i];FileUtils.copyFile(f, new File("d:/test/"+fileName));}} catch (IOException e) {e.printStackTrace();}return "success";}}

<%@ 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><h1>action fileInput</h1><s:debug/><!-- 第一种方法 --><!-- <form action="Message_add" method="post">id:<input type="text" name="msg.id">title:<input type="text" name="msg.title">content:<input type="text" name="msg.content"><input type="submit"><hr></form> --><!-- 第二种方法 --><form action="Message_file" method="post" enctype="multipart/form-data">title:<input type="text" name="title">flie:<input type="file" name="photo">flie:<input type="file" name="photo"><input type="submit"></form></body></html>

struts.multipart.saveDir=struts.multipart.maxSize=2097152     (默认的上传文件你大小的配置)

0 0
原创粉丝点击