uploadify文件上传的使用
2015-04-15 17:51
204 查看
1.jsp代码
2.action和service
appname=$("#appname").val(); $("#uploadify").uploadify({ 'swf' : '<%=basePath%>/Backstage/Style/jQuery-uploadify/uploadify.swf', // 'script' : 'ProductAction!fileUpload.do',//后台处理的请求 'uploader': 'AppVersionAction!fileUpload.do?appname='+appname , 'cancelImg' : '<%=basePath%>/Backstage/include/easyvalidator/images/vtip_arrow.png', 'folder' : '/uplaod/temp',//您想将文件保存到的路径 'queueID' : 'fileQueue',//与下面的id对应 'fileObjName':'uploadify', 'queueSizeLimit' : 5, 'fileDesc' : '文件格式', 'fileTypeExts' : '*.apk;*.ipa;*.png', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc 'auto' : false, 'multi' : true, 'simUploadLimit' : 2, 'buttonText' : '选择文件', 'onQueueComplete' : function(file,data,response) { picFlag = true; } });
<input type="file" name="uploadify" id="uploadify" /> <input id="appname" type="text" value="<s:property value="version.appName"/>" name="version.appName" size="30" maxlength="32" data-options="required:true,missingMessage:'请输入版本号'" class="easyui-validatebox" /><font color="red"> *</font>
2.action和service
public void addVersion() { String appname= getRequest().getParameter("appname"); UploadImg upload = new UploadImg(); try { /** 商品基本属性 **/ version.setId(Constants.getUUID()); version.setCreateTime(Constants.getSimpleDateFormat3()); /** 将缓存中的图片放入版本目录中 **/ List<String> files = (List<String>) getSession().getAttribute("files"); String filesext=(String) getSession().getAttribute("fileExt"); String updateUrl=(String) getSession().getAttribute("fileName1"); if (files != null) { SimpleDateFormat yearFormat = new SimpleDateFormat("yyyyMMdd"); String folderName = Constants.versionFileSavePath + "/" + yearFormat.format(new Date()) ; String newFileName=appname+"."+filesext; for (int i = 0; i < files.size(); i++) { upload.moveAndSaveFile(getRequest(), "/Upload/temp" +files.get(i), "/Upload/"+folderName,newFileName); } version.setUpdateUrl(updateUrl); // commodityInfo.setImageDir("/Upload/" + folderName); } getSession().removeAttribute("files"); getSession().removeAttribute("fileName1"); this.wfAppVersionService.addObject(version); String json = JSON.toJSONString(version); json = json.replace("}", ",\"tips\":\"新增版本信息成功\"}"); getResponse().setContentType("text/html;charset=utf-8"); getResponse().getWriter().println(json); } catch (Exception ex) { ex.printStackTrace(); try { getResponse().setStatus(555); getResponse().getWriter().println("新增失败"); Logger.getLogger(AppVersionAction.class).info("版本控制执行新增操作异常"); } catch (IOException e) { e.printStackTrace(); } } }
public void copyAndUpateFile(HttpServletRequest request,String oldPath, String newPath, String newName) { try { int bytesum = 0; int byteread = 0; System.out.println("@@newPath: "+newPath); File oldfile = new File(request.getRealPath("/")+oldPath); if (oldfile.exists()) { // 文件存在时 //查看传入的目录是否存在 如果不存在就创建 if (newPath.split("/").length > 0) { String[] paths = newPath.split("/"); String path = request.getRealPath("/") ; for (int i = 0; i < paths.length; i++) { path += paths[i] + "/"; File saveDirFile = new File(path); if (!saveDirFile.exists()) { saveDirFile.mkdirs(); } } } File file=new File(request.getRealPath("/")+newPath+"/"+"cbt_"+newName); file.createNewFile(); InputStream inStream = new FileInputStream(request.getRealPath("/")+oldPath); // 读入原文件 FileOutputStream fs = new FileOutputStream(request.getRealPath("/")+newPath+"/"+"cbt_"+newName); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // 字节数 文件大小 fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } } /** * 将临时文件tmp移动并改变格式 * @Title: UploadImg * @param: @param request * @param: @param oldPath * @param: @param newPath * @param: @param newName * @return: * @throws: * @Description: TODO */ public void moveAndSaveFile(HttpServletRequest request,String oldPath, String newPath,String newName) { copyAndUpateFile(request,oldPath, newPath,newName); String fileExt = oldPath.substring(oldPath.lastIndexOf(".") + 1).toLowerCase(); System.out.println("oldPath: "+oldPath+"###NewPath: "+newPath+" fileExt: "+fileExt); delFile(request,oldPath); }
相关文章推荐
- 基于jQuery的uploadify(flash上传文件)控件 的使用
- 在Struts2中使用Uploadify组件上传文件
- 大文件上传Jquery 插件Uploadify-v2.1.4使用图解
- 在asp.net mvc中使用Uploadify上传文件
- 解决MVC下使用uploadify上传文件时HttpPostedFileBase为空的问题
- django使用uploadify上传文件
- 上传文件插件 Uploadify使用说明
- 上传文件插件 Uploadify使用说明 转
- 基于jquery的上传插件Uploadify 3.1.1在MVC3中的使用:上传大文件的IO Error问题
- 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传
- fileupload文件上传(配合Uploadify使用)
- Web开发中的文件上传组件uploadify的使用
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
- Web开发中的文件上传组件uploadify的使用
- django使用uploadify上传文件
- django使用uploadify上传文件
- asp.net使用uploadify上传文件不能超过4mb的解决方案
- 在asp.net mvc中使用Uploadify上传文件
- 在asp.net mvc中使用Uploadify上传文件
- 在asp.net mvc中使用Uploadify上传文件