1.jsp代碼
1 appname=$("#appname").val(); 2 $("#uploadify").uploadify({ 3 'swf' : '<%=basePath%>/Backstage/Style/jQuery-uploadify/uploadify.swf', 4 // 'scr1 <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:'請輸入版本號'" class="easyui-validatebox" /><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 }
新聞熱點
疑難解答