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
- Struts2_文件上传_Demo
- Struts2_单文件上传
- Struts2_多文件上传
- struts2_文件上传
- Struts2_文件上传
- Struts2_事物管理&文件上传
- Struts2_事物管理&文件上传
- 42. Struts2_文件的上传
- 26.struts2_文件的上传
- Lion.Web.UpLoadModule 1.0_Demo提供大文件上传并显示上传进度条
- 11级_Java_曹建波 03.02 Struts2_事物管理&文件上传
- JavaWeb开发知识总结(七)-(struts2_文件上传_Ajax)
- Struts2_文件上传关于限制文件名、类型及大小
- Lion.Web.UpLoadModule 1.0_Demo提供大文件上传并显示
- struts2_文件上传下载
- Struts2_文件下载
- struts2_文件下载
- Struts2_文件下载
- Spark-1.6.0中的Sort Based Shuffle源码解读
- LINUX下mysql区分大小写的问题
- 长期接受碎片化信息,会有什么后果?
- 深入浅出 - Android系统移植与平台开发(五)- 定制手机模拟器ROM
- c++第六次作业
- Struts2_文件上传_Demo
- hibernate tool
- 彻底理解Git
- mac下ctags illegal的问题
- 远程桌面连接支持网络级身份验证修复脚本 vbs
- 跟我一起学C++之C++介绍(笔记)
- 几道常问的题
- 欢迎使用CSDN-markdown编辑器
- Android 自定义组合控件