<%@ 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的更多相关文章

  1. Springmvc+uploadify实现文件带进度条批量上传

    网上看了很多关于文件上传的帖子,众口不一,感觉有点乱,最近正好公司的项目里用到JQuery的uploadify控件做文件上传,所以整理下头绪,搞篇文档出来,供亲们分享. Uploadify控件的主要优 ...

  2. uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案

     由于技术选型的原因,在一个产品中,我选择了uploadify,选择它的原因是它有完善的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...

  3. SpringMVC+jquery.uploadify 上传文件

    前言 以前用Asp.net MVC+uploadify上传文件,最近学习SpringMVC,所以就用SpringMVC+uploadify做个上传文件的demo. 刚开始用form表单的方式提交,在C ...

  4. uploadify在火狐下上传不了的解决方式,java版(Spring+SpringMVC+MyBatis)具体解决方式

     因为技术选型的原因,在一个产品中.我选择了uploadify,选择它的原因是它有完好的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...

  5. SpringMVC与uploadify结合进行上传

    uploadify是一个第三方js插件,支持多文件上传,拥有较为强大的上传功能 1.uploadify实现 下载其flash版本 http://www.uploadify.com/  解压后将其内容区 ...

  6. springmvc 多数据源 SSM java redis shiro ehcache 头像裁剪

    获取下载地址   QQ 313596790  A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:31359679 ...

  7. java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis

    A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:313596790freemaker模版技术 ,0个代码不用写 ...

  8. springmvc整合mybatis框架源码 bootstrap

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址    ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...

  9. springmvc+mybatis+spring 整合源码项目

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等 ...

随机推荐

  1. Java封装

    面向对象的三大特征:封装.继承.多态.封装在书面上来说,他无非就是set和get方法罢了.为什么要用到封装,比如买主机如果没有机箱只有内存条.CPU.主板等等那些零件买回到家,你还得一个个的去装那些零 ...

  2. 阿里云专有网络与弹性公网IP

    阿里云服务器经典网络和专有网络究竟有什么区别? 在用户提交订单购买阿里云ECS云服务器时,会面临怎样选择网络类型的烦恼,阿里云服务器定制购买时,网络类型里的经典网络和专有网络(VPC)是什么含义,该怎 ...

  3. markdown语法测试

    斜体 粗体 百度 标题一 -------- 标题二 ======== 标题三 标题四 有序列表一 有序列表二 无序列表一 无序列表二 这是引用的文字 这是一句行内代码var=1 public clas ...

  4. 深入理解asp.net里的HttpModule机制

    刚工作的时候看<asp.net深入解析>,第一次知道HttpModule和HttpHandler.当时对我而言,它们不过就是两个新名词而已,仅仅知道工作原理但是理解的不深刻.随着经验的累积 ...

  5. c#中关键词out和ref的区别

    c#中关键词out和ref用来表明以传引用的方式传递参数. 区别如下: 如果方法的参数用out标记,表示方法被调用前不需初始化参数,方法内不能读取此参数的值,在方法返回前必须向此参数写入值: 如果方法 ...

  6. VMware安装Linux第一天

    今天上午下载了VMware-workstation_full_12.1.1.6932,它的Key是5A02H-AU243-TZJ49-GTC7K-3C61N,这些都是网络上搜罗到的. VMWare安装 ...

  7. 问题 “No mapping found for HTTP request with URI [/rbiz4/uploadFile.html]” 的解决

    从以前的SpringMVC项目简化一下做个例子,结果出现了下面的错误: No mapping found for HTTP request with URI [/rbiz4/uploadFile.ht ...

  8. 打开VS调试不进入开发的网站直接跳转到主页

    重启了熟悉有卸载IE11的,搞了好几个小时 最后把电脑管家里的锁定主页打开就好了! 很久之后  我再锁上  也没有这问题了

  9. win7 安装 memcached

    1. 下载 memcached-win32-1.4.4-14.zip,里面包含6个文件,将解压后的文件夹随便放在什么位置.如果需要win64版,下载 memcached-win64-1.4.4-14. ...

  10. iOS,第三方库使用

    1.ASIHttpRequest网络请求库 2.MBProgressHUD指示层库 3.Toast+UIView提示库 4.SDWebImage图片缓存库 5.MGSwipeTableCell单元格侧 ...