1.jsp代码
1 appname=$("#appname").val(); 2 $("#uploadify").uploadify({ 3 'swf' : '<%=basePath%>/Backstage/Style/jQuery-uploadify/uploadify.swf', 4 // 'script' : 'ProductAction!fileUpload.do',//后台处理的请求 5 'uploader': 'AppVersionAction!fileUpload.do?appname='+appname , 6 'cancelImg' : '<%=basePath%>/Backstage/include/easyvalidator/images/vtip_arrow.png', 7 'folder' : '/uplaod/temp',//您想将文件保存到的路径 8 'queueID' : 'fileQueue',//与下面的id对应 9 'fileObjName':'uploadify',10 'queueSizeLimit' : 5,11 'fileDesc' : '文件格式',12 'fileTypeExts' : '*.apk;*.ipa;*.png', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc13 'auto' : false,14 'multi' : true,15 'simUploadLimit' : 2,16 'buttonText' : '选择文件',17 'onQueueComplete' : function(file,data,response) { 18 picFlag = true; 19 } 20 });
1 <input type="file" name="uploadify" id="uploadify" />2 <input id="appname" type="text" value="<s:property value="version.appName"/>" name="version.appName" size="30" maxlength="32" data-options="required:true,missingMessage:'请输入版本号'" /><font color="red"> *</font>
2.action和service
1 public void addVersion() { 2 String appname= getRequest().getParameter("appname"); 3 UploadImg upload = new UploadImg(); 4 try { 5 /** 商品基本属性 **/ 6 version.setId(Constants.getUUID()); 7 version.setCreateTime(Constants.getSimpleDateFormat3()); 8 9 /** 将缓存中的图片放入版本目录中 **/10 List<String> files = (List<String>) getSession().getAttribute("files");11 String filesext=(String) getSession().getAttribute("fileExt");12 String updateUrl=(String) getSession().getAttribute("fileName1");13 if (files != null) {14 SimpleDateFormat yearFormat = new SimpleDateFormat("yyyyMMdd");15 String folderName = Constants.versionFileSavePath + "/" + yearFormat.format(new Date()) ;16 String newFileName=appname+"."+filesext;17 for (int i = 0; i < files.size(); i++) {18 upload.moveAndSaveFile(getRequest(), "/Upload/temp" +files.get(i), "/Upload/"+folderName,newFileName);19 }20 version.setUpdateUrl(updateUrl);21 // commodityInfo.setImageDir("/Upload/" + folderName);22 }23 getSession().removeAttribute("files");24 getSession().removeAttribute("fileName1");25 this.wfAppVersionService.addObject(version);26 String json = JSON.toJSONString(version);27 json = json.replace("}", ",\"tips\":\"新增版本信息成功\"}");28 getResponse().setContentType("text/html;charset=utf-8");29 getResponse().getWriter().println(json);30 } catch (Exception ex) {31 ex.printStackTrace();32 try {33 getResponse().setStatus(555);34 getResponse().getWriter().println("新增失败");35 Logger.getLogger(AppVersionAction.class).info("版本控制执行新增操作异常");36 } catch (IOException e) {37 e.printStackTrace();38 }39 }40 }
1 public void copyAndUpateFile(HttpServletRequest request,String oldPath, String newPath, String newName) { 2 try { 3 int bytesum = 0; 4 int byteread = 0; 5 System.out.println("@@newPath: "+newPath); 6 File oldfile = new File(request.getRealPath("/")+oldPath); 7 if (oldfile.exists()) { // 文件存在时 8 //查看传入的目录是否存在 如果不存在就创建 9 if (newPath.split("/").length > 0) {10 String[] paths = newPath.split("/");11 String path = request.getRealPath("/") ;12 for (int i = 0; i < paths.length; i++) {13 path += paths[i] + "/";14 File saveDirFile = new File(path);15 if (!saveDirFile.exists()) {16 saveDirFile.mkdirs();17 }18 }19 }20 File file=new File(request.getRealPath("/")+newPath+"/"+"cbt_"+newName);21 file.createNewFile();22 InputStream inStream = new FileInputStream(request.getRealPath("/")+oldPath); // 读入原文件23 FileOutputStream fs = new FileOutputStream(request.getRealPath("/")+newPath+"/"+"cbt_"+newName);24 byte[] buffer = new byte[1444];25 while ((byteread = inStream.read(buffer)) != -1) {26 bytesum += byteread; // 字节数 文件大小 27 fs.write(buffer, 0, byteread);28 }29 inStream.close();30 }31 } catch (Exception e) {32 System.out.println("复制单个文件操作出错");33 e.printStackTrace();34 35 }36 }37 38 /**39 * 将临时文件tmp移动并改变格式40 * @Title: UploadImg41 * @param: @param request42 * @param: @param oldPath43 * @param: @param newPath44 * @param: @param newName45 * @return: 46 * @throws:47 * @Description: TODO48 */49 public void moveAndSaveFile(HttpServletRequest request,String oldPath, String newPath,String newName) {50 copyAndUpateFile(request,oldPath, newPath,newName);51 String fileExt = oldPath.substring(oldPath.lastIndexOf(".") + 1).toLowerCase();52 System.out.println("oldPath: "+oldPath+"###NewPath: "+newPath+" fileExt: "+fileExt);53 delFile(request,oldPath);54 55 }
原标题:uploadify文件上传的使用
关键词:上传