struts2+uploadify多文件上传
来源:互联网 发布:数字媒体技术软件 编辑:程序博客网 时间:2024/05/01 23:58
直接上代码:
action类:
package action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.HashMap;import java.util.Map;import java.util.UUID;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport {private File img_file;private String img_fileContentType;// 格式同上"fileName"+ContentTypeprivate String img_fileFileName;// 格式同上"fileName"+FileNameprivate String savePath;// 文件上传后保存的路径private Map<String, Object> dataMap = new HashMap<String, Object>();@Override/*** * 上传文件 */public String execute() throws Exception {File dir = new File(getSavePath());System.out.println("真正的路径:" + dir.getAbsolutePath());// 判断是否存在路径if (!dir.exists()) {dir.mkdirs();}// 当前上传的文件File file = getImg_file();System.out.println("文件:" + file);// 获得后缀String ext = getImg_fileFileName().substring(getImg_fileFileName().lastIndexOf("."));System.out.println("后缀名:" + ext);String newFileName = UUID.randomUUID() + ext;System.out.println("新的文件名:" + newFileName);FileOutputStream fos = new FileOutputStream(getSavePath() + "//"+ newFileName);FileInputStream fis = new FileInputStream(getImg_file());byte[] buffers = new byte[1024];int len = 0;while ((len = fis.read(buffers)) != -1) {fos.write(buffers, 0, len);}fos.close();fis.close();// String uploadFileName = getImg_fileFileName();dataMap.put("filename", newFileName);return SUCCESS;}public File getImg_file() {return img_file;}public void setImg_file(File img_file) {this.img_file = img_file;}public String getImg_fileContentType() {return img_fileContentType;}public void setImg_fileContentType(String img_fileContentType) {this.img_fileContentType = img_fileContentType;}public String getImg_fileFileName() {return img_fileFileName;}public void setImg_fileFileName(String img_fileFileName) {this.img_fileFileName = img_fileFileName;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}public Map<String, Object> getDataMap() {return dataMap;}public void setDataMap(Map<String, Object> dataMap) {this.dataMap = dataMap;}}
struts.xml配置:
<constant name="struts.multipart.maxSize" value="10240000000" /><package name="default" namespace="" extends="json-default"><action name="uploadAction" class="action.FileUploadAction"><param name="savePath">C:\devtools\server\apache-tomcat-7.0.57\webapps\upload\weiuserfile</param><result type="json"><param name="root">dataMap</param></result></action></package>
jsp代码:
<%@ 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 'index.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">--><link rel="stylesheet" type="text/css" href="js/uploadify.css"><script type="text/javascript" src="js/jquery-1.10.2.min.js"></script><script type="text/javascript" src="js/jquery.uploadify.min.js"></script><script type="text/javascript">$(function(){$("#img_file").uploadify({ auto:false,//是否自动上传 height: 30, buttonText:'选择图片', cancelImage:'<%=basePath%>js/uploadify-cancel.png', swf : '<%=basePath%>js/uploadify.swf', // expressInstall:'js/uploadify/expressInstall.swf', uploader : '<%=basePath%>uploadAction.action', //后台处理上传文件的action width : 120,'multi' : true, //设置为true将允许多文件上传 'filesSelected' : '4',queueID : 'uploadfileQueue',fileObjName : 'img_file', //与后台Action中file属性一样/*formData:{ //附带值 'userid':'111','username':'tom', 'rnd':'111'}, */fileTypeDesc : '上传文件支持的文件格式:jpg,jpge,gif,png',fileTypeExts : '*.jpg;*.jpge;*.gif;*.png',//*.jpg;*.jpge;*.gif;*.pngqueueSizeLimit : 4,//只能一次上传4张图片 // simUploadLimit:1,//一次可以上传1个文件fileSizeLimit : '20971520',//上传文件最大值 单位为字节 2M//返回一个错误,选择文件的时候触发onSelectError : function(file, errorCode, errorMsg) {switch (errorCode) {case -100:alert("上传的文件数量已经超出系统限制的4个文件!");break;case -110:alert("文件 [" + file.name+ "] 大小超出系统限制的2M大小!");break;case -120:alert("文件 [" + file.name + "] 大小异常!");break;case -130:alert("文件 [" + file.name + "] 类型不正确!");break;}}, ////上传到服务器,服务器返回相应信息到data里onUploadSuccess : function(file, data, response) {var objs = eval('(' + data + ')');alert(objs);alert(objs.filename);var phtml = "<span><img style='width:150;height:150' src='<%=basePath%>weiuserfile/"+objs.filename+"'></span>";alert(phtml);if ($("#imgs span").length == 0) {$("#imgs").html(phtml);} else {$("#imgs span:last").after(phtml);}},onSelect : function(file) {//alert(file.name); },//removeCompleted:true,//上传的文件进度条是否消失requeueErrors : false,// removeTimeout:2,//进度条消失的时间,默认为3progressData : "percentage",//显示上传的百分比onUploadError : function(file, errorCode, errorMsg,errorString, swfuploadifyQueue) { //这里是取消的时候发生 // $("#dialog-message").html(errorString); }});});//上传文件function myUpload() {$("#img_file").uploadify('upload', '*');}</script></head><body><form action="" method="post"><input type="file" name="img_file" id="img_file"><div id="uploadfileQueue"></div><div id="imgs"></div><div id="dialog-message"></div></form><p><a href="javascript:void(0);" onclick="myUpload()">上传</a> <ahref="javascript:$('#img_file').uploadify('cancel')">取消上传</a></p></body></html>
需要的jar包:
需要的js:
下载连接:http://pan.baidu.com/s/1mgkOHN6
0 0
- struts2 uploadify 多文件上传
- struts2+uploadify多文件上传
- uploadify 文件上传 struts2
- uploadify+Struts2实现多文件上传
- uploadify多文件上传结合struts2
- Struts2 + uploadify 多文件上传完整实例!
- JQuery上传控件uploadify结合Struts2多文件上传
- uploadify多文件上传
- uploadify多文件上传
- uploadify多文件上传
- Uploadify多文件上传
- uploadify多文件上传
- 使用struts2和uploadify实现多文件上传
- uploadify+ struts2 上传多个文件带进度条
- struts2+jquery(uploadify)文件上传
- jquery uploadify 3.2 基于struts2 上传文件
- struts2,Jquery uploadify异步上传文件
- uploadify 多文件上传例子
- 从四分钟到两秒——谈谈客户端性能优化的一些最佳实践
- ping不通某ip, 但向它发arp请求居然有响应?--- 谈谈一个奇葩非问题的定位过程
- AIS和雷达信息融合(1)坐标转换
- 程序员可以在周末做的8件事
- Eclipse 安装插件
- struts2+uploadify多文件上传
- Linux压缩&解压命令详解
- cocos2dx3.2 某些UI白屏或使用ClippingNode造成部分手机白屏
- 我的2014点点滴
- STM32F103学习笔记(四) 串口实验
- linux vsftpd服务器安装和配置
- BeanUtils包的使用
- 黑马程序员——第一个iOS程序
- android总结整理----(2)自定义ListView(SimpleAdapter实现)