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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531485.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下使用消息队实现 ATM 自动取款机功能

文章分五部分&#xff1a;需求分析、项目所需知识点、思路讲解、代码实现、功能演示 本文内容较长&#xff0c;建议是按照我自己的思路给大家讲解的&#xff0c;如果有其他问题&#xff0c;欢迎评论区讨论 文章中的代码是在linux下编译实现的&#xff0c;注意自己的环境。 &…

200行代码实现视频人物实时去除

今天在GitHub上发现了一个好玩的代码&#xff0c;短短几百行代码就实现了从复杂的背景视频中去除人物&#xff0c;不得不说这位大佬比较厉害。 这个项目只需要在网络浏览器中使用JavaScript&#xff0c;用200多行TensorFlow.js代码&#xff0c;就可以实时让视频画面中的人物对…

codeforces C. Vanya and Scales

C. Vanya and ScalesVanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2(exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using …

菱形继承和虚继承、对象模型和虚基表

1.菱形继承&#xff08;钻石继承&#xff09;&#xff1a;两个子类继承同一父类&#xff0c;而又有子类同时继承这两个子类。例如B,C两个类同时继承A&#xff0c;但是又有一个D类同时继承B,C类。 2.菱形继承的对象模型 class A { public:int _a; };class B :public A { p…

c++虚函数和虚函数表

前言 &#xff08;1&#xff09;虚基表与虚函数表是两个完全不同的概念 虚基表用来解决继承的二义性(虚基类可以解决)。虚函数用来实现泛型编程&#xff0c;运行时多态。 &#xff08;2&#xff09;虚函数是在基类普通函数前加virtual关键字&#xff0c;是实现多态的基础 &a…

Struts.xml中Action的method与路径的三种匹配方法

原文 http://blog.csdn.net/woshixuye/article/details/7734482 首先我们有一个Action——UserAction public class UserAction extends ActionSupport { public String add() { return "add"; } public String modify() { return …

VS2017安装配置Qt

这篇文章作为qt的开发环境配置篇&#xff0c;记录如何在vs2017中安装qt的 所需软件下载链接如下&#xff1a; QT下载链接&#xff1a;QT visual studio下载链接&#xff1a;visual studio 这里推荐安装最新的&#xff0c;原因是vs2017不支持一些老版本的makefile文件生成&#…

Qt Creator 常用快捷键

掌握一些适用的快捷键&#xff0c;可以提高程序开发的效率。 快捷键功能F4在同名的头文件和源程序文件之间切换F2跟踪光标下的符号&#xff0c;若是变量&#xff0c;可跟踪到变量声明的地方&#xff1b;若是函数体或函数声 明&#xff0c;可在两者之间切换ShiftF2在函数的声明…

Hibernate学习之hibernate.cfg.xml

<?xml version1.0 encodingUTF-8?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- 正文开始 --> <h…

STM32位带区和位带别名区的浅谈

1.首先谈下为什么要使用位带&#xff1f; 在学习51单片机时就已经使用过位操作&#xff0c;比如使用sbit对单片机IO口的定义&#xff0c;但是STM32中并没有这类关键字&#xff0c;而是通过访问位带别名区来实现&#xff0c;即通过将每个比特位膨胀成一个32位字&#xff0c;当访…

Hibernate的数据查找,添加!

1.首先看一下测试数据库的物理模型 2.测试所需要的Hibernate的jar包 3.数据库的sql /**/ /* DBMS name: MySQL 5.0 */ /* Created on: 2015/7/3 23:17:57 */ /**/drop table if exists books;drop tab…

新手如何在Altium Designer中绘制电路板

好久没用AD画电路板了&#xff0c;这次电子实训让画个PCB板&#xff0c;借着这个机会写了一篇新手教程。 此教程所用的电路图是自动循迹小车&#xff0c;虽然元件比较简单&#xff0c;但是感觉还是很厉害的&#xff0c;一块看一下吧。 此教程仅适用于没有基础的同学 一、概述 …

Hibernate的数据删除,更改

其他未给出代码&#xff0c;请参考上一篇.... 一.数据的删除 方法1.从“多”的一方进行数据的删除 books.hbm.xml文件不变&#xff1a; <many-to-one name"publishers" column"publisherId" class"com.entry.Publishers" lazy"false&quo…

STM32的AFIO时钟什么时候开启?

问题描述 在使用STM32的USART2时发现AFIO时钟无论打不打开串口都能正常工作 带着这个问题网上搜集了一些资料&#xff0c;由于我对这块的理解并不是很深&#xff0c;如果有错误欢迎指正 首先为什么要开启时钟&#xff1f; 答&#xff1a;因为STM32几乎所有的外设都有独立的时…

Qt模仿QQ登录界面(一)

这两天研究qt&#xff0c;练习时做了个仿QQ登录界面&#xff0c;我这次实现的比较简单&#xff0c;先在这里记录一下&#xff0c;以后有空了会继续完善的。 &#xff08;一&#xff09;效果图 这里使用我的qq号测试的如图&#xff1a; &#xff08;二&#xff09;工程文件 &…

n维数组实现(可变参数表的使用)

首先先介绍一下可变参数表需要用到的宏&#xff1a; 头文件&#xff1a;#include<cstdarg> void va_start( va_list arg_ptr, prev_param ); type va_arg( va_list arg_ptr, type ); void va_end( va_list arg_ptr ); va_list:用来保存宏va_start、va_arg和va_end所需信息…

回流焊和波峰焊的区别

本文首先分别介绍回流焊和波峰焊的特点&#xff0c;然后对两者进行比较&#xff0c;欢迎评论补充哦~ 最近在实习看到了厂里面的回流焊的波峰焊&#xff0c;有点好奇就查了点资料&#xff0c;分享给同样爱学习的你。 一.回流焊 一般的表面贴装工艺分三步&#xff1a;印刷机施加…

三对角矩阵的压缩

三对角矩阵&#xff0c;从第二行开始选中的元素的个数都为3个。对于a[i,j]将要存储的位置k&#xff0c;首先前(i-1)行元素的个数是(i-2)*3 2(第一行元素的个数为2)&#xff0c;又a[i,j]属于第i行被选中元素的第j-i1个元素&#xff0c;所以k (i-2)*3 2 j-i1 2*ij-3 如果知道了…

LC和RC滤波电路分析

一、概述 整流电路的输出电压并不是纯粹的直流&#xff0c;从示波器观察整流电路的输出&#xff0c;与直流相差很大&#xff0c;波形中含有较大的脉动成分&#xff0c;称为纹波。为了获得比较理想的直流电压&#xff0c;需要利用具有储能作用的电抗性元件(如&#xff1a;电感、…

dev c++ Boost库的安装

dev c 的boost库的安装步骤 然后点击“check for updates”按钮 最后点击“Download selected”按钮&#xff0c;下载完成后安装.... 给dev添加boost库文件&#xff0c;找到之前安装的目录 #include<iostream> #include<string> #include<cstring> #include…