<%@ 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接入图灵机器人,实现与机器人聊天

    很多人都玩过微信,其中就有与机器人聊天的功能:

  2. Android课程---计算器的实现

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  3. 杭电ACM 1013 Digital Root

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){char s[100000];int ...

  4. Larbin初试

    前阵子找工作的时候经常会看到epoll多路复用的知识点,无奈自己一点都不懂.慌忙之际也只能去了解个大概.所以最近闲下来之后想要基于epoll机制实现一个比较有用的东西,刚好最近又想爬些东西,希望这次能 ...

  5. PHP运算符===和==的区别 小细节

    区别: ===是恒等计算符   同时检查表达式的值与类型 ==是比较运算符号  不会检查条件式的表达式的类型 举例 if (strstr($new_url,'http://') === false) ...

  6. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  7. Convert and Cast for Date and Money format.

    SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(10), '') The below script removes the TAB(Horozonta ...

  8. Openstack搭建相关问题

    1.Openstack添加新磁盘到根分区 初始化分区sdb为物理卷pv pvcreate /dev/sdb  //初始化 pvdisplay  //显示物理卷信息 vgextend centos /d ...

  9. vim - multiple windows

    https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

  10. DNS消息格式

    一,简介 空谈误国,要让一大堆抽象的DNS概念落地,还是需要了解DNS消息格式的,本文会尽量详细地介绍DNS消息格式的每一个字段. 也可以移步rfc1035了解. 二,概览 DNS消息主要由五部分组成 ...