码迷,mamicode.com
首页 > 其他好文 > 详细

kindeditor 4 指定生成文件的时间日期/动态获取My97的时间

时间:2018-11-15 20:16:42      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:扩展名   tco   key   indexof   format   sts   初始化   style   path   

最近后台要求要指定上传附件的时间日期,编辑器是kindeditor,仔细研究后发现可以借助clickToolbar事件,传递时间到后台,但是中途发现传递的时间总是初始化my97的时间,改变后的时间只有在提交时才能生效

于是又研究了下My97,终于解决

 1     <tr class="info">
 2         <td class="text-right">自定义发布时间</td>
 3         <td>
 4             <fmt:formatDate value="<%=new Date()%>" pattern="yyyy-MM-dd HH:mm:ss" var="fmttime"></fmt:formatDate>
 5             <div class="">
 6                 <label for="time" class="col-md-2 control-label">    
 7                 <input type="text" class="form-control" name="releaseTime" value="${fmttime}"  id="time" onClick="WdatePicker({dateFmt:‘yyyy-MM-dd HH:mm:ss‘,onpicked:function(dp){ setTime(dp) }})" disabled>
 8                 </label>
 9             </div>
10         </td>
11     </tr>

js

 1 //需要动态设置my97的值到输入框中否则无法获取最新改变的时间
 2 function setTime(dp) {
 3     $("#time").attr("value",dp.cal.getNewDateStr(‘yyyy-MM-dd HH:mm:ss‘));
 4 }
 5 
 6 KindEditor.ready(function(k) {
 7     window.editor = k.create("textarea[name=‘content‘]",options);     
 8     window.editor.clickToolbar("insertfile",function() {
 9         var releaseTime =$("#time").attr("value");
10           $.ajax({
11             url:"${pageContext.request.contextPath}/kindeditor/jsp/upload_json.jsp?releaseTime=" + releaseTime,
12             dataType:"text",
13                type:"GET",
14                success:function() {
15                    
16                }
17             
18         });
19  
20      })
21 });

upload_json

  1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2 <%@ page import="java.util.*,java.io.*" %>
  3 <%@ page import="java.text.SimpleDateFormat" %>
  4 <%@ page import="org.apache.commons.fileupload.*" %>
  5 <%@ page import="org.apache.commons.fileupload.disk.*" %>
  6 <%@ page import="org.apache.commons.fileupload.servlet.*" %>
  7 <%@ page import="org.json.simple.*" %>
  8 <%
  9 String releaseTime = request.getParameter("releaseTime");
 10 if(releaseTime != null) {
 11     request.getSession().setAttribute("releaseTime",releaseTime);
 12 }
 13 //用于创建文件目录和文件名
 14 String fileTime =(String)request.getSession().getAttribute("releaseTime");
 15 
 16 //文件保存目录路径
 17 String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
 18 
 19 //文件保存目录URL
 20 String saveUrl  = request.getContextPath() + "/attached/";
 21 
 22 //定义允许上传的文件扩展名
 23 HashMap<String, String> extMap = new HashMap<String, String>();
 24 extMap.put("image", "gif,jpg,jpeg,png,bmp");
 25 extMap.put("flash", "swf,flv");
 26 extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
 27 extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,zip,rar,gz,bz2,swf");
 28 
 29 //最大文件大小 1GB
 30 long maxSize = 1073741824;
 31 
 32 response.setContentType("text/html; charset=UTF-8");
 33 
 34 if(!ServletFileUpload.isMultipartContent(request)){
 35     out.println(getError("请选择文件。"));
 36     return;
 37 }
 38 //检查目录
 39 File uploadDir = new File(savePath);
 40 if(!uploadDir.isDirectory()){
 41     out.println(getError("上传目录不存在。"));
 42     return;
 43 }
 44 //检查目录写权限
 45 if(!uploadDir.canWrite()){
 46     out.println(getError("上传目录没有写权限。"));
 47     return;
 48 }
 49 
 50 String dirName = request.getParameter("dir");
 51 if (dirName == null) {
 52     dirName = "image";
 53 }
 54 if(!extMap.containsKey(dirName)){
 55     out.println(getError("目录名不正确。"));
 56     return;
 57 }
 58 //创建文件夹
 59 savePath += dirName + "/";
 60 saveUrl += dirName + "/";
 61 File saveDirFile = new File(savePath);
 62 if (!saveDirFile.exists()) {
 63     saveDirFile.mkdirs();
 64 }
 65 //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 66 //时间日期设置
 67 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意这种格式的时间含有空格无法生成文件名
 68 Calendar calendar = Calendar.getInstance();
 69 calendar.setTime(df.parse(fileTime));
 70 
 71 String month = String.valueOf(calendar.get(Calendar.MONTH)+1);
 72 if(month.length()<2) {
 73     month = "0" + month;
 74 }
 75 String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
 76 if(day.length() < 2) {
 77     day = "0"+day;
 78 }
 79 String ymd = String.valueOf(calendar.get(Calendar.YEAR))+month + day;
 80 
 81 
 82 savePath += ymd + "/";
 83 saveUrl += ymd + "/";
 84 File dirFile = new File(savePath);
 85 if (!dirFile.exists()) {
 86     dirFile.mkdirs();
 87 }
 88 
 89 FileItemFactory factory = new DiskFileItemFactory();
 90 ServletFileUpload upload = new ServletFileUpload(factory);
 91 upload.setHeaderEncoding("UTF-8");
 92 List items = upload.parseRequest(request);
 93 Iterator itr = items.iterator();
 94 while (itr.hasNext()) {
 95     FileItem item = (FileItem) itr.next();
 96     String fileName = item.getName();
 97     String oldFileName = fileName;
 98     long fileSize = item.getSize();
 99     if (!item.isFormField()) {
100         //检查文件大小
101         if(item.getSize() > maxSize){
102             out.println(getError("上传文件大小超过限制。"));
103             return;
104         }
105         //检查扩展名
106         String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
107         if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
108             out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));
109             return;
110         }
111 
112     
113         Date date = df.parse(fileTime);
114         
115         
116         calendar.setTime(date);
117         
118         
119         String newFileName = String.valueOf(calendar.get(Calendar.YEAR))
120                                 + month
121                                 + day
122                                 + String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))
123                                 + String.valueOf(calendar.get(Calendar.MINUTE)) 
124                                 + String.valueOf(calendar.get(Calendar.SECOND))
125                                 + "_" + new Random().nextInt(1000) + "." + fileExt;
126         try{
127             File uploadedFile = new File(savePath, newFileName);
128             item.write(uploadedFile);
129         }catch(Exception e){
130             out.println(getError("上传文件失败。"));
131             return;
132         }
133 
134         
135         JSONObject obj = new JSONObject();
136         obj.put("error", 0);
137         obj.put("url",saveUrl + newFileName);//
138         if(extMap.get("file").contains(newFileName.substring(newFileName.lastIndexOf(".")+1))){
139             request.getSession().setAttribute("attachment",true);
140         }
141     //    obj.put("url",oldFileName);//回显原文件名,回显图片的原文件名时会导致显示有问题
142         out.println(obj.toJSONString());
143     }
144 }
145 %>
146 <%!
147 private String getError(String message) {
148     JSONObject obj = new JSONObject();
149     obj.put("error", 1);
150     obj.put("message", message);
151     return obj.toJSONString();
152 }
153 %>

 

kindeditor 4 指定生成文件的时间日期/动态获取My97的时间

标签:扩展名   tco   key   indexof   format   sts   初始化   style   path   

原文地址:https://www.cnblogs.com/tele-share/p/9965727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!