struts 多文件上传 annotation注解(零配置)+ ajaxfileupload + 异步 版本
【本文简介】
struts 多文件上传。基于”零配置“+"ajaxfileupload" 的一个简单例子。
【导入依赖jar包】
- jquery-1.7.2.js : http://jquery.com/download/
- ajaxfileupload.js:http://fileuploadajax.codeplex.com/releases/view/8061
【修改 ajaxfileupload.js 使其支持多文件】
打开js,找到:
var oldElement = jQuery('#' + fileElementId);
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
改成:
for(var i in fileElementId){
var oldElement = jQuery('#' + fileElementId[i]);
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
}
【文件夹结构】


【web.xml】
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
【struts.xml】
<!-- URL资源分隔符 -->
<constant name="struts.convention.action.name.separator" value="_" />
<!-- ****************************以下是文件上传的设置*********************************** -->
<!-- 指定国际化资源文件的baseName为messageResource -->
<!-- 设置该应用使用的解码集 -->
<constant name="struts.i18n.encoding" value="utf-8"/>
<!-- 上传的全部文件的最大限制-->
<constant name="struts.multipart.maxSize" value="1024102400"/>
<!-- 设置存放临时文件的文件夹 -->
<constant name="struts.multipart.saveDir" value="/tmp"></constant>
<!-- ****************************以上是文件上传的设置*********************************** -->
【JSP代码】
以上的web.xml配置导致下面的访问地址的方法名有要加:.action
以上的struts.xml配置 URL 资源分隔符导致下面的访问地址的 类名第一个字母的大写换成 ”_小写“
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload()
{
var file = ['file1','file2']; $.ajaxFileUpload
(
{
url:'upload_file_by_annotation!upload.action',//用于文件上传的服务器端请求地址
secureuri:false,//一般设置为false
fileElementId: file,// 文件id数组
dataType: 'JSON',//返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
alert("success");
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert("fail");
}
}
); return false; } </script> </head> <body>
<input type="file" id="file1" name="file" />
<input type="file" id="file2" name="file" />
<br />
<input type="button" value="上传" onclick="return ajaxFileUpload();"> </body>
</html>
【action代码】
注:其实这个说是annotation版本,但因为 没有特别要设置的,所以annotation也省了。
假如要跳转到其他页面,可自己加入annotation。简单annotaion使用例子可用参看上一篇文章:
《struts文件下载 annotation 注解版》http://www.cnblogs.com/xiaoMzjm/p/3879048.html
package com.modelsystem.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import org.apache.struts2.ServletActionContext; /**
* @描述 struts 多文件上传 annotation注解(零配置)+ ajaxfileupload + 异步 版本
* @作者 小M
* @博客 http://www.cnblogs.com/xiaoMzjm/
* @时间 2014/07/30
*/
public class UploadFileByAnnotationAction extends BaseAction { private static final long serialVersionUID = 1L; // 上传的文件,struts会自动帮我们填充至此,因为多文件,所以用List
private List<File> file; // 上传的文件的文件名,因为多文件,所以用List
private List<String> fileFileName; // 上传的文件的类型,因为多文件,所以用List
private List<String> fileContentType; 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 upload(){ // 文件所放的文件夹。, 有关路径问题,请参考另一篇博文:http://www.cnblogs.com/xiaoMzjm/p/3878758.html
String root = ServletActionContext.getServletContext().getRealPath("/")+"\\upload\\"; //循环上传的文件
for(int i = 0 ; i < file.size() ; i ++){ InputStream is = null ; OutputStream os = null;
try {
// 获取当前遍历到的文件,new 一个文件输入流,连接到该文件。
is = new FileInputStream(file.get(i)); // new 一个文件,连接到要存储的文件夹处。
File destFile = new File(root,this.getFileFileName().get(i)); // new 一个输出流,连接到要存储的文件处。
os = new FileOutputStream(destFile); // 字节流,规定可写入的字节数。
byte[] buffer = new byte[is.available()];
int length = 0 ; // 开始写入文件
while((length = is.read(buffer))>0){
os.write(buffer, 0, length);
} } catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return SUCCESS;
}
}
struts 多文件上传 annotation注解(零配置)+ ajaxfileupload + 异步 版本的更多相关文章
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- struts 多文件上传 xml 版本
[本文简介] 本文将介绍 以配置 struts.xml 的方式 实现 多文件上传的功能. [文件夹结构] [struts.xml] <?xml version="1.0" ...
- java框架篇---struts之文件上传和下载
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
- Struts中文件上传的一些规则...
1.action中定义规范 如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为 private File xxx; private String ...
- php curl文件上传兼容php5.0~5.6各版本
PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POS ...
- IIS7 大文件上传 asp.net iis配置
IS6下上传大文件没有问题,但是迁移到IIS7下面,上传大文件时,出现HTTP 404错误. IIS配置上传大小,webconfig <!-- 配置允许上传大小 --><httpRu ...
- 前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https
一.信息流小程序-GET请求案例 1.1服务端接口开发 一定要养成接口的意识,前端单打独斗出不来任何效果,必须有接口配合,写一个带有分页.关键词查询的接口: 分页接口:http://127.0.0.1 ...
- struts实现文件上传和下载。
先来实现上传. 写上传不管语言,都要先注意前端的form那儿有个细节. <form name="form1" method="POST" enctype= ...
- FTP文件上传以及获取ftp配置帮助类
帮助类: using QSProjectBase; using Reform.CommonLib; using System; using System.Collections.Generic; us ...
随机推荐
- RIP协议两个版本对不连续子网的支持情况实验
一.连续子网与不连续子网 我们经常见到说RIPv1不支持不连续子网,仅支持连续子网,那么什么是连续子网,什么是不连续子网呢? l 不连续子网:指在一个网络中,某几个连续由同一主网划分的子网在中间被多 ...
- 八大CMS内容管理系统推荐
cms系统哪个好 感谢 64320 的投递 时间:2015-03-05 来源:http://www.iisp.com/ztview/ID_16129.html?s=bios 耐思尼克 很多新手站长初次 ...
- phpmyadmin通过日志文件拿webshell
该方法非原创.只是给大家分享一下姿势.如果知道得就当复习了,不知道得就捣鼓捣鼓. 前提:条件是root用户. 思路:就是利用mysql的一个日志文件.这个日志文件每执行一个sql语句就会将其执行的保存 ...
- Unity—JsonFx序列化场景
场景数据类: /// <summary> /// 关卡数据 /// </summary> public class LevelData { //关卡名称 pub ...
- 折腾WordPress感想
以前在cnblogs上写博客没感觉什么,一旦要搭建自己的博客,我感觉好麻烦啊,具体就体现在一下方面: 1. 域名得要申请 2. 还要购买虚拟机 3. 自己搭建php,mysql,wordpress,a ...
- HashMap与TreeMap的区别?
HashMap与TreeMap的区别? 解答:HashMap通过hashcode对其内容进行快速查找,而TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用Tre ...
- Android音频文件浏览+音频播放
该Demo执行后,会显示全部你sd卡上的音乐文件列表, 并能够点击列表选择某一首歌曲进行播放. 执行效果: 点击download出现: 然后点击歌曲调用系统播放器播放. 源码: activity_au ...
- scrollHeight、clientHeight、offsetHeight、scrollTop等的定义以及图解
开发中经常遇到使用scrollHeight.scrollTop.clientHeight.offsetHeight等的情况,网上有众多关于这些属性的解释,但是并不全面和直观,现在将这些属性结合图例整理 ...
- uva 10494 - If We Were a Child Again 大数除法和取余
uva 10494 - If We Were a Child Again If We Were a Child Again Input: standard inputOutput: standard ...
- 利用Fiddler或Charles进行mock数据
使用场景:服务器数据不符合测试条件时,我们可以通过在本地创建虚拟数据来打到测试用例所描述的条件. fiddler使用方法 1.首先在本地创建txt数据:将抓到的response中的json数据拷贝到记 ...