structs2之多文件上传
//首先是Action部分import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import java.util.List;import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext;import com.hzml.dao.DistributeDao; import com.opensymphony.xwork2.ActionSupport;public class CompanyAndDistributeAction extends ActionSupport{ //和form表单对应的name属性private DistributeDao distributeDao;private String developName;private String email;private String phone;private String timeID;private String money;private String taskDescribe; //多文件上传,form中的每个文件的name属性必须一样,在这里我的name="file"private List<File> file; // 上传的文件private List<String> fileFileName; // 文件名称private List<String> fileContentType; // 文件类型private String savePath;public DistributeDao getDistributeDao() {return distributeDao;}public void setDistributeDao(DistributeDao distributeDao) {this.distributeDao = distributeDao;}public String getDevelopName() {return developName;}public void setDevelopName(String developName) {this.developName = developName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getTimeID() {return timeID;}public void setTimeID(String timeID) {this.timeID = timeID;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;}public String getTaskDescribe() {return taskDescribe;}public void setTaskDescribe(String taskDescribe) {this.taskDescribe = taskDescribe;}public List<File> getFile() {return file;}public void setFile(List<File> file) {this.file = file;}public List<String> getFileFileName() {return fileFileName;}public void setFileFileName(List<String> fileFileName) {this.fileFileName = fileFileName;}public List<String> getFileContentType() {return fileContentType;}public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}public String getSavePath() {return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}private void init() throws UnsupportedEncodingException{ServletActionContext.getRequest().setCharacterEncoding("UTF-8");ServletContext context = ServletActionContext.getServletContext(); }public String saveTask () throws Exception{init();// 取得需要上传的文件数组List<File> files = getFile();if (files != null && files.size() > 0) {for (int i = 0; i < files.size(); i++) {FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));FileInputStream fis = new FileInputStream(files.get(i));byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fis.close();fos.close();}}return "saveTask";}@Overridepublic String execute() throws Exception{return "success";} }
//jsp代码部分<form action="companyAndDistributeAction" method="post" enctype="multipart/form-data" name="contactForm" id="contactForm"><br/><div><label>发布者:<span>*</span></label><input type="text" name="developName" id="developName" /></div><div><label>邮箱:<span>*</span></label><input type="text" name="email" id="email" /></div><div><label>电话:</label><input type="text" name="phone" id="phone" /></div><div><label>任务完成时间:<span>*</span></label><input type="text" name="timeID" id="timeID" /></div><div><label>任务费用:<span>*</span></label><input type="text" name="money" id="money" /></div><div><label>任务描述:</label><textarea name="taskDescribe" rows="10" cols="20" id="taskDescribe"></textarea></div><div><label>任务说明文档:<span>*</span></label><input type="file" name="file" id="file" /></div><a class="button" href="#" style="float:right;" onclick="$('#contactForm')[0].submit(); return false;" id="send"><span>发布任务</span></a></form>
//structs2配置文件部分<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><!-- 将Action的创建交给spring来管理 --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.i18n.encoding" value="utf-8"/><package namespace="/" name="struts2" extends="struts-default"> <!-- package中的标签必须按照如下顺序配置result-types,interceptors,default-interceptor-ref,default-action-ref,default-class-ref,global-results,global-exception-mappings,action*(就是所有的action放到最后)--><!-- 自定义拦截器 ,如果有拦截器,必须放在package标签内的第一位--><interceptors><!-- 在这里可以添加自己的拦截器<interceptor name="myInterceptor" class="自定义pojo类"></interceptor>--><interceptor-stack name="myInterceptorStack"><!-- <interceptor-ref name="myInterceptor"></interceptor-ref>--><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors> <global-results><result></result></global-results> <action name="companyAndDistributeAction" class="companyAndDistributeAction" method="saveTask"><!-- 要创建/hzmlFile文件夹,否则会报找不到文件 --><param name="savePath">/hzmlFile</param><result name="saveTask">/ActionIntroduction.jsp</result> </action><!-- 访问主页 --><action name="visitMainPage" class="login" method="visitMainPage"><result name="visitMainPage">/index.jsp</result> </action><!-- 用户登录 --><action name="userLogin" class="login" method="userLogin"><result name="success">/ActionIntroduction.jsp</result> </action></package> </struts>
转载于:https://www.cnblogs.com/hujunzheng/p/4574449.html
structs2之多文件上传相关推荐
- Structs2实现文件上传功能
Struts2本身并没提供上传的组件,我们可以通过调用上传框架来实现文件的上传. 一.配置上传解析器 首先要配置项目的框架,也就是倒导入"struts2-core-2.2.1.jar&quo ...
- struts2 文件上传与下载 (初始文件上传的底层技术)——struts2第七讲
2019独角兽企业重金招聘Python工程师标准>>> 文件上传 struts2 注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初 ...
- springboot设置文件上传大小(tomcat默认1M)
application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...
- 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录
将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...
- Springboot 多文件上传
其实多个文件和单个文件上传是一样的,可以使用同一个Controller 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileuplo ...
- django文件上传
Django在处理文件上传时,文件数据被打包封装在request.FILES中. 一.简单上传 首先,写一个form模型,它必须包含一个FileField: # forms.py from djang ...
- spring boot 文件上传工具类(bug 已修改)
以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...
- 模拟文件上传(一):手动文件上传
关于上传文件,首先我的第一个案例是一个文本文件的上传,简单容易上手! 首先我们上传文件肯定就属于实体内容部分了:所以不能过GET方式请求了,要通过POST方式请求: 因为: 1.get方式是URL传值 ...
- Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)
相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...
最新文章
- Appium学习笔记2_Android获取元素篇
- Kernel 社区 开发准备工作mutt 邮件使用
- Kubernetes日志分析利器:Elassandra部署使用指南
- Python+OpenCV:仿射变换和透射变换
- [Python] 维度交换函数:transpose(m,n,r)和permute(m,n,r)
- Oracle SQL性能调整注意事项
- Matlab遗传算法
- 基于R语言的现代贝叶斯统计学(INLA下的贝叶斯回归、多层贝叶斯回归、生存分析、随机游走模型、广义可加模型、极端数据的贝叶斯分析等)
- 遍历QListWidget 所有item
- promise、axios 理解
- 马化腾和朱啸虎互怼之后,摩拜ofo合并可能性基本为零
- Contiki学习笔记——Cooja启动失败
- 光学遥感影像的几何校正
- VC驿站一个不错的学习编程的地方
- 2019年度星标大奖重磅揭晓 百度APP携手ZOL“智见未来”
- Error creating document instance. Cause: org.xml.sax.SAXParseException报错解决
- 牛客网“程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)”错误的可能原因
- php获取12个月前的日期,PHP 1901年12月13日之前的日期的32位日期解析
- bestcoder #77 xiaoxin juju needs help
- 深度学习笔记-遥感影像转为tensor前的检查及线性拉伸
热门文章
- usleep延时0.毫秒_【进阶】用swoole实现订单的延时处理(自动取消,还原库存等)...
- mysql对本地文件的读取_Mysql 任意读取客户端文件
- KAFKA 同步和异步消息的发送(开发实战)
- linux 发送http请求方式
- 企业实战(Jenkins+GitLab+SonarQube)_05_Jenkins创建管理员用户
- 十六进制、RGB 与 VBA颜色值对照表
- linux php生产环境搭建,linux php 环境搭建
- qt中调整弹出框的位置
- tcp的简单使用实例一
- java调用js查询mongo_MongoDB增删查改操作示例【基于JavaScript Shell】