当前位置: 首页 > news >正文

【Struts2学习笔记(9)】单文件上传和多文件上传


(1)单文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。

这两个文件可以从http://commons.apache.org/下载。(如果是直接使用Add Struts capacities 则可以不用这几个jar包)


第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
  <input  type="file" name="uploadImage">
</form>

第三步:在Action类中添加以下属性:

package cn.lc.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class HelloWorldAction {
	private File image;
	private String imageFileName;

	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String addUI() {
		return "success";
	}
	public String execute() throws Exception {

		// 获得路径
		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
		System.out.println(realpath);
		//判断文件不为空的时候才保存
		if (image != null) {
			File savefile = new File(new File(realpath), imageFileName);
			//获得稳健目录 如果不存在则创建
			if (!savefile.getParentFile().exists()) {
				savefile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(image, savefile);
			ActionContext.getContext().put("message", "上传成功");
		}
		return "success";
	}
}

第四步:上边的代码在上擦混大文件的时候会出现错误这是因为默认的大小只有几兆 如果需要上传稍大一些的文件则需要修改struts.xml默认的文件大小:

<struts>
  <!--   使用常量来设置上传文件的大小  大小为10兆左右-->
    <constant name="struts.multipart.maxSize" value="10701096"/>
 
	<package name="employee" namespace="/control/employee" extends="struts-default">
		<action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
			<result name="success">/WEB-INF/page/message.jsp</result>
		</action>
	</package>
</struts>

(2)多文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
  <input  type="file" name="uploadImages">
  <input  type="file" name="uploadImages">
</form>


第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

package cn.lc.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class HelloWorldAction {
	private File[] image;
	private String[] imageFileName;

	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public String[] getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String addUI() {
		return "success";
	}

	public String execute() throws Exception {

		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
		System.out.println(realpath);
		if (image != null) {
			File savedir = new File(realpath);
			if (!savedir.exists())
				savedir.mkdirs();
			for (int i = 0; i < image.length; i++) {
				File savefile = new File(savedir, imageFileName[i]);
				FileUtils.copyFile(image[i], savefile);
			}
			ActionContext.getContext().put("message", "上传成功");
		}
		return "success";
	}
}

同样的 也可以设置文件上传的大小限制!


注:转载请注明出处!



相关文章:

  • 如何更改Java默认的花括号对齐方式
  • GET和POST区别和用法
  • 【Struts2学习笔记(10)】自定义拦截器管理权限访问
  • 大型网站提速方案
  • 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
  • 分享一百多套开发视频教程的下载地址
  • java中Object类 源代码详解
  • 史上最全Java学习视频下载地址分享
  • MySql 查询一周内最近7天记录
  • 【Struts2学习笔记(12)】Struts2国际化
  • 【Struts2学习笔记(13)】Struts2中OGNL详解
  • 【Struts2学习笔记(14)】Struts2标签元素
  • SSH2框架搭建Jar下载地址
  • hibernate.cfg.xml文件的配置模板和不同数据库的配置参数
  • EasyUI - LinkButton 按钮控件
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • ES2017异步函数现已正式可用
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • js面向对象
  • mockjs让前端开发独立于后端
  • PHP CLI应用的调试原理
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 读懂package.json -- 依赖管理
  • 前端临床手札——文件上传
  • 原生js练习题---第五课
  • ​低代码平台的核心价值与优势
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • (1)(1.9) MSP (version 4.2)
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (六)激光线扫描-三维重建
  • (四) Graphivz 颜色选择
  • (四)JPA - JQPL 实现增删改查
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • ***通过什么方式***网吧
  • .“空心村”成因分析及解决对策122344
  • .Net CoreRabbitMQ消息存储可靠机制
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • .NET开源快速、强大、免费的电子表格组件
  • .NET企业级应用架构设计系列之结尾篇
  • .NET实现之(自动更新)
  • .net专家(张羿专栏)
  • [AX]AX2012 SSRS报表Drill through action
  • [BZOJ] 2044: 三维导弹拦截
  • [BZOJ2208][Jsoi2010]连通数
  • [BZOJ3223]文艺平衡树
  • [C++]priority_queue的介绍及模拟实现
  • [ExtJS5学习笔记]第三十节 sencha extjs 5表格gridpanel分组汇总
  • [LeetCode周赛复盘] 第 310 场周赛20220911
  • [Linux] 进程间通信基础