Struts2_文件上传_Demo

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";
	}

	@Override
	public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值