uploadify springMVC
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/> <script src="${ctx}/jslib/uploadify/jquery.uploadify.js" type="text/javascript" charset="utf-8"></script> <link href="${ctx}/jslib/uploadify/uploadify.css" rel="stylesheet" type="text/css" /> <script type="text/javascript">
$(function() { //添加界面的附件管理
$("#file_upload").uploadify({
'height' : 27,
'width' : 80,
'buttonText' : '添加附件',
'queueID': 'fileQueue', //队列的ID
'queueSizeLimit':5,
'swf' : '${ctx}/jslib/uploadify/uploadify.swf',
'uploader' : '${ctx}/fileUpload/uploadFile',
'auto' : false,
'fileTypeExts' : '*.rar',
'fileObjName' : 'file',
'multi' : true,
'removeCompleted': false, //是否完成后移除序列,默认为true
'fileSizeLimit': '100MB', //单个文件大小,0为无限制,可接受KB,MB,GB等单位的字符串值
'onUploadSuccess':function(file, data, response){
var vv = data.split('|');
var bb = $("input[name=attachmentIds]").attr('value');
bb +="," + vv[0];
$("input[name=attachmentIds]").attr('value',bb); bb = $("input[name=attachmentNames]").attr('value');
bb +="," + vv[1];
$("input[name=attachmentNames]").attr('value',bb);
//$.messager.alert("提示", "上传完毕!");
}
}); $('#documentId').combotree({
url : '${ctx}/dictionarytype/tree?code=documenttype',
parentField : 'pid',
panelHeight : 'auto',
onBeforeExpand : function(node, param) {
$('#documentId').combotree("tree").tree("options").url = "${ctx}/dictionarytype/trees?type=0&pId="
+ node.id; }
});
$('#documentAddForm').form({
url : '${ctx}/document/add',
onSubmit : function() {
progressLoad(); var isValid = $(this).form('validate');
if (!isValid) {
progressClose();
}else{
var bb = $("input[name=attachmentIds]").attr('value');
var num = $("#file_upload").data('uploadify').queueData.queueLength;
if(bb=="" && num>0){
progressClose();
$.messager.show({
title : '提示',
msg : '<div class="light-info"><div class="light-tip icon-tip"></div><div>'
+ "您你附件还没上传,请先上传附件!"
+ '</div></div>',
showType : 'show'
});
return false;
}
}
return isValid;
},
success : function(result) {
progressClose();
result = $.parseJSON(result);
if (result.success) {
parent.$.modalDialog.openner_dataGrid.datagrid('reload');//
$.messager
.show({
title : '提示',
msg : '<div class="light-info"><div class="light-tip icon-tip"></div><div>'
+ result.msg
+ '</div></div>',
showType : 'show'
});
parent.$.modalDialog.handler.dialog('close');
}else{
parent.$.messager.alert('提示', result.msg, 'warning');
}
}
}); }); function init(){
var attachments = $("input[name=attachmentIds]").attr('value');
$("input[name=attachmentIds]").attr('value',"");
$('#file_upload').uploadify('cancel', '*');
if(attachments!=""){
$.post('${ctx}/fileUpload/deleteFile', {
attachments : attachments
}, function(result) { }, 'json');
}
}
</script>
<div style="padding: 3px;">
<form id="documentAddForm" method="post">
<table class="grid">
<tr>
<td>标题</td>
<td colspan="3"><input name="title" type="text" placeholder="请输入字典编码" class="easyui-validatebox" data-options="required:true" style="width: 355px; height: 29px;" ></td>
</tr>
<tr>
<td>排序</td>
<td><input name="seq" value="0" class="easyui-numberspinner" style="width: 140px; height: 29px;" required="required" data-options="editable:false"></td>
<td>类别</td>
<td><select id="documentId" name="documentId" style="width: 140px; height: 29px;" data-options="required:true"></select></td>
</tr>
<tr>
<td>备注</td>
<td colspan="3"><textarea name="content" rows="5" cols="50" ></textarea></td>
</tr>
<tr>
<td>文档附件</td>
<td colspan="3">
<input name="attachmentIds" id="attachmentIds" type="hidden" class="easyui-validatebox" >
<input name="attachmentNames" id="attachmentNames" type="hidden" class="easyui-validatebox" >
<input type="file" name="file_upload" id="file_upload"/>
<a class="easyui-linkbutton" href="javascript:$('#file_upload').uploadify('upload', '*')">上传文件</a> | <a class="easyui-linkbutton" href="javascript:init()">取消上传</a>
</td> </tr>
</table>
<div id="fileQueue" class="fileQueue"></div>
</form>
package east.mvc.controller.file;
package east.mvc.controller.file; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import east.mvc.controller.base.BaseController;
import east.mvc.framework.constant.GlobalConstant;
import east.mvc.model.base.Graph;
import east.mvc.model.sys.Tattachment;
import east.mvc.pageModel.base.Grid;
import east.mvc.pageModel.base.PageFilter;
import east.mvc.pageModel.base.SessionInfo;
import east.mvc.pageModel.sys.MapHistory;
import east.mvc.service.basic.GraphServiceI;
import east.mvc.service.sys.AttachmentServiceI;
import east.mvc.service.sys.MapHistoryServiceI;
import east.mvc.utils.StringUtil; @Controller
@RequestMapping("/fileUpload")
public class FileUploadController extends BaseController {
private HttpServletRequest request = null; @Autowired
private AttachmentServiceI attachmentService; @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.request = request;
String responseStr = "";
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取前台传值
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
// String ctxPath =
// request.getSession().getServletContext().getRealPath("/")+ "\\" +
// "images\\";
String configPath = File.separator + "upload" + File.separator;
// String vv = getPhysicalPath(configPath);
// String vv2 = getPhysicalPathByRoot(configPath);
String ctxPath = request.getSession().getServletContext().getRealPath("/");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(new Date());
configPath += year + File.separator;
sdf = new SimpleDateFormat("MM");
String month = sdf.format(new Date());
configPath += month + File.separator; ctxPath += configPath;
// 创建文件夹
File file = new File(ctxPath);
if (!file.exists()) {
file.mkdirs();
}
String fileName = null;
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
// 上传文件名
// System.out.println("key: " + entity.getKey());
MultipartFile mf = entity.getValue();
fileName = mf.getOriginalFilename(); String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
responseStr = configPath + newFileName + "|" + fileName;
File uploadFile = new File(ctxPath + newFileName);
try {
FileCopyUtils.copy(mf.getBytes(), uploadFile);
} catch (IOException e) {
responseStr = "上传失败";
e.printStackTrace();
} }
response.setHeader("Content-type", "text/html;charset=UTF-8");
// 这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
response.setCharacterEncoding("UTF-8");
response.getWriter().write(responseStr);
return null;
} @RequestMapping("/deleteFile")
@ResponseBody
public void getAreaIds(HttpServletRequest request, String attachments) {
if (!attachments.equals("")) {
String ctxPath = request.getSession().getServletContext().getRealPath("/");
attachments = attachments.substring(1);
String[] value = attachments.split(",");
for (int i = 0; i < value.length; i++) {
File file = new File(ctxPath + "//" + value[i]);
file.delete();
}
}
} @RequestMapping("/downloadFile")
@ResponseBody
public void downloadFile(HttpServletRequest request, HttpServletResponse response, String attachmentId) throws Exception{
if (!attachmentId.equals("")) {
Tattachment attachment = attachmentService.gets(attachmentId);
if (attachment != null) {
String fileName = attachment.getFileName();
String filePath = attachment.getFilePath();
BufferedInputStream in = null;
BufferedOutputStream out = null;
request.setCharacterEncoding("UTF-8");
String rootpath = request.getSession().getServletContext().getRealPath("/");
try { File f = new File(rootpath + "//" + filePath);
response.setContentType("application/x-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setHeader("Content-Length", String.valueOf(f.length()));
in = new BufferedInputStream(new FileInputStream(f));
out = new BufferedOutputStream(response.getOutputStream());
byte[] data = new byte[1024];
int len = 0;
while (-1 != (len = in.read(data, 0, data.length))) {
out.write(data, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
} /**
* 根据传入的虚拟路径获取物理路径
*
* @param path
* @return
*/
private String getPhysicalPath(String path) {
String servletPath = this.request.getServletPath();
String realPath = this.request.getSession().getServletContext().getRealPath(servletPath);
return new File(realPath).getParent() + "/" + path;
} /**
* 根据传入的虚拟路径获取物理路径(相对应根路径)
*
* @param path
* @return
* @author fengjx
* @date 2014年3月10日
*/
private String getPhysicalPathByRoot(String path) {
String realPath = this.request.getSession().getServletContext().getRealPath("/");
return new File(realPath).getPath() + "/" + path;
}
}
http://files.cnblogs.com/files/xsmhero/uploadify.rar
uploadify springMVC的更多相关文章
- Springmvc+uploadify实现文件带进度条批量上传
网上看了很多关于文件上传的帖子,众口不一,感觉有点乱,最近正好公司的项目里用到JQuery的uploadify控件做文件上传,所以整理下头绪,搞篇文档出来,供亲们分享. Uploadify控件的主要优 ...
- uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案
由于技术选型的原因,在一个产品中,我选择了uploadify,选择它的原因是它有完善的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...
- SpringMVC+jquery.uploadify 上传文件
前言 以前用Asp.net MVC+uploadify上传文件,最近学习SpringMVC,所以就用SpringMVC+uploadify做个上传文件的demo. 刚开始用form表单的方式提交,在C ...
- uploadify在火狐下上传不了的解决方式,java版(Spring+SpringMVC+MyBatis)具体解决方式
因为技术选型的原因,在一个产品中.我选择了uploadify,选择它的原因是它有完好的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...
- SpringMVC与uploadify结合进行上传
uploadify是一个第三方js插件,支持多文件上传,拥有较为强大的上传功能 1.uploadify实现 下载其flash版本 http://www.uploadify.com/ 解压后将其内容区 ...
- springmvc 多数据源 SSM java redis shiro ehcache 头像裁剪
获取下载地址 QQ 313596790 A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单; 技术:31359679 ...
- java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis
A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单; 技术:313596790freemaker模版技术 ,0个代码不用写 ...
- springmvc整合mybatis框架源码 bootstrap
A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址 ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...
- springmvc+mybatis+spring 整合源码项目
A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等 ...
随机推荐
- Xcode 添加类前缀
按照如下图所示操作后,接下来创建的类就会带有MN的前缀;如果想更换前缀,则替换MN即可!
- VS 调试
Vs 单步调试 在vs中的单步调试: 调试重要的几个键: F9在当前光标所在的行下断点,如果当前行已经有断点,则取消断点. F5调试状态运行程序,程序执行到有断点的地方会停下来. F10单步执行程序. ...
- IOS第12天(2,UINavigationController导航控制器)
****HMAppDelegate.m @implementation HMAppDelegate - (BOOL)application:(UIApplication *)application d ...
- 【iCore3 双核心板_FPGA】例程十二:Modelsim仿真实验
实验指导书及代码包下载: http://pan.baidu.com/s/1bnQEldl iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- C#异步编程简单的运用
当一个方法中有很多复杂的操作的时候就可以使用异步编程. 假如说这一个方法中有很多复杂的操作,把每一个复杂的操作放到一个异步方法中. 原来程序需要这些方法,上一个执行完成之后,才能执行下一个操作. 但是 ...
- DOM9大节点
ELEMENT_NODE 1 元素节点 常用 ATTRIBUTE_NODE 2 属性节点 常用 TEXT_NODE 3 文本节点 常用 CDATA_SECTION_NODE 4 CDATA区段 E ...
- win7系统如何安装SQL Server 2005
Microsoft Windows 7 旗舰版(32位) SQL Server 2005 简体中文开发板 方法/步骤 1 首先为了保证数据库的顺利安装,先启用IIS服务吧!Win7比XP好的一点是 ...
- Java jdbc 连接oracle
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.S ...
- 微信的redirect_uri参数错误原因分析
我们可以根据微信的开发者文档 http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html 网页授权获取用户验证这一章来 ...
- laravel----------------自动生成模型,控制器,视图的操作步骤。
首先要阅读这篇文章,你一定已经掌握了composer 和 artisan . 第一步,打开https://packagist.org/这个网址,在搜索框内输入way/generators 如图 第二步 ...