//首先是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";
} @Override
public 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>

structs2之多文件上传的更多相关文章

  1. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  2. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

  3. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  4. 小兔Java教程 - 三分钟学会Java文件上传

    今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...

  5. ,net core mvc 文件上传

    工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...

  6. Web开发安全之文件上传安全

    很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...

  7. AutoIt实现Webdriver自动化测试文件上传

    在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...

  8. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  9. .JavaWeb文件上传和FileUpload组件使用

    .JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...

随机推荐

  1. 一款免费支持PDF、word、excel、PPT、jpeg之间互转线上软件

    偶然发现的一款免费支持PDF.word.excel.PPT.jpeg之间互转,支持合并pdf.加密解密PDF的线上软件,首先声明,不是广告党,我自己试用过,确实是目前我用过最好用的,如果有朋友有更好的 ...

  2. SQL分页常用的两个存储过程

    在做数据绑定时,我们常常会遇到分页事件,下面记录一下分页常用到SQL的两个存储过程 ①/****分页数据总数****//****** 对象:  StoredProcedure [dbo].[GetRe ...

  3. Java学习日记之 Java-其他类型

    OK , 就绪, 第一篇博客开撸 ~ 先听了毕姥爷激情澎湃的其他类型 ,从这里开始入手吧 : (一)System类  System类代表系统类,系统的属性和方法都放在该类.System类是一个fina ...

  4. iOS 7 UI Transition – Porting View Controller Layouts from iOS 6

    http://www.mobinett.com/2013/08/19/ios7-ui-transition-porting-view-controller-layouts-ios6/

  5. Swap Swap,即交换分区

    Swap,即交换区,除了安装Linux的时候,有多少人关心过它呢?其实,Swap的调整对Linux服务器,特别是Web服务器的性能至关重要.通过调整Swap,有时可以越过系统性能瓶颈,节省系统升级费用 ...

  6. 我的c漏洞

    传入指针参数 #include<stdio.h> #include<stdlib.h> void READ(int *a) { scanf("%d",a); ...

  7. UNET学习笔记3 - 网络系统的概念

    服务器和 HOST 在Unity游戏里,一个游戏一般有一个服务器和多个客户端组成,但也可以没有服务器,用某一个客户端来同时做服务器用,这种就叫Host 在Host上的客户端叫Local Client, ...

  8. 从MySQL5.7.6开始,安装MySQL提示“请键入 NET HELPMSG 3534 以获得更多的帮助”的解决办法

    今天安装MySQL提示如下错误: ----------------------------------------------------------------------------------- ...

  9. Manifesto of the Communist Party

    A spectre is haunting Europe – the spectre of communism. All the powers of old Europe have entered i ...

  10. Xamarin开发IOS笔记:切换输入法时输入框被遮住

    在进行IOS开发的过程中,出现类似微信朋友圈的交互界面,当用户遇到感兴趣的内容可以进行评论.为了方便评论输入,当出现评论输入框的时候自动将评论输入框移动至键盘的上方,这样方便边输入边查看. 当用户隐藏 ...