<%@ 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. IOS第七天(2:UiTableView 加上数据分离)

    ****加上数据分离 #import "HMViewController.h" #import "HMStudent.h" @interface HMViewC ...

  2. Sqlserver2008和Oracle分页语句

    SqlServer 分页语句 select StuID ,StuNo,StuName,Age,Sex, ClassName ClassName from (select *, row_number() ...

  3. bootstrap全局CSS样式学习

    参考http://v3.bootcss.com/css/,根据自己的记忆进行的复述,加深记忆. 首先介绍bootstrap全局CSS样式 只通过使用bootstrap.css,即可获得统一的样式设置. ...

  4. ExtJs 使用点滴 十三 在FormPanel 嵌入按钮

    Ext.onReady(function () { //初始化标签中的Ext:Qtip属性. Ext.QuickTips.init(); Ext.form.Field.prototype.msgTar ...

  5. Java学习-048-插件应用之 Find Bugs

    FindBugs 是一个静态分析工具,它可以检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题,使用 FindBugs 可以在不实际运行程序的情况对软件进行分析.使用时最好将字节 ...

  6. angularJs之template指令

    template: 如果我们只需要在ng-view 中插入简单的HTML 内容,则使用该参数: .when('/computers',{template:'这是电脑分类页面'}) templateUr ...

  7. Python学习【第二篇】Python入门

    Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...

  8. 转:"在已损坏了程序内部状态的XXX.exe 中发生了缓冲区溢出"的一种可能原因

    我的问题跟原作者的问题差不多.头文件和DLL不匹配导致的. 原文链接:http://blog.csdn.net/u012494876/article/details/39030887 今天软件突然出现 ...

  9. [HTML5]移动开发不同手机弹出数字键盘问题

    这里还是先那么先交代一下遇到的问题.其实无论是tel还是number都不是完美的:type="tel"优点是iOS和Android的键盘表现都差不多缺点是那些字母好多余,虽然我没有 ...

  10. Leetcode: Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...