项目文件部署功能

将更新文件夹压缩为zip格式上传,解析更新。

大致步骤为:

1、前台上传文件;

2、上传文件至服务器;

3、验证上传版本信息;

4、更新文件;

前台html&js代码:

<form name="cForm" action=""  enctype="multipart/form-data"  method="post">
<input name="file" id="file" style="width: 400px" type="file" />
<input type="button" class="btn" value="数据上传" onclick="dopass(this.form)" />
</form>

<iframe name="uploadFrame" height="0px" width="0px" style="display: none;"></iframe>
function dopass(form) {
 
	if (form.file.value == '') {
		alert('请选择需要升级的文件!');
		return false;
	}

	if ((form.file.value.toLocaleUpperCase()).indexOf('.ZIP') <= 0) {
		alert('上传文件只能为 zip 文件!');
		return false;
	}
	 
	if (confirm('将执行系统升级,确定要升级当前系统吗?')) {
 	          document.cForm.action =  "do_deploy";
	          document.cForm.target = "uploadFrame";
	          document.cForm.submit();
	} else {
		return false;
	}
}

在Controller层获取文件-->上传服务器-->部署文件:

@RequestMapping("/do_deploy")
        public void doDeploy(HttpServletRequest request,HttpServletResponse response) {
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
			OutData od = deployService.uploadDeployFile(multipartRequest);
			//od为上传文件反馈
                       if(od.getCode() == -1) {
				logger.warn(od.getMsg());
				render(response, "<script>alert('" + od.getMsg() + "');</script>", "text/html;charset=UTF-8");
				return;
			}
			String downPath = od.getMsg();//上传文件所在路径
			String rootPath = multipartRequest.getSession().getServletContext().getRealPath("/");/获取根目录
			File zip = new File(downPath);
			//获取版本文件进行验证
                        File versionFile = deployService.getVersionFile(zip, "version.xml", rootPath);
			od = deployService.versionCheck(versionFile, rootPath);
			if(od.getCode() == -1) {
				logger.warn(od.getMsg());
				render(response, "<script>alert('" + od.getMsg() + "');</script>", "text/html;charset=UTF-8");
				return;
			}
			 //文件部署
                        od = deployService.deployProject(zip, rootPath);
			if(od.getCode() == -1) {
				logger.warn(od.getMsg());
				render(response, "<script>parent.alertMessage('" + od.getMsg() + "');</script>", "text/html;charset=UTF-8");
				return;
			}
			render(response, "<script>parent.alertMessage('部署成功!');parent.grid0.load();</script>", "text/html;charset=UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("内部出错,出错原因:" + e.getMessage());
			render(response, "<script>parent.alertMessage('内部出错!出错代码:" + e.getMessage() + "');parent.grid0.load();</script>", "text/html;charset=UTF-8");
		}
	}
 public  void render(HttpServletResponse response, String text,
   String contentType) {
 
 try {
  
     response.setHeader("Pragma", "No-cache");
 
     response.setHeader("Cache-Control", "no-cache");

     response.setDateHeader("Expires", 0);

     response.setContentType(contentType);
 
     response.getWriter().write(text);

     response.getWriter().close();

  } catch (IOException e) {
 
  e.printStackTrace();
  
}


 }

}

 deployService代码:

public OutData deployProject(File file, String rootPath) {
		OutData od = new OutData();
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(file, "GBK");
			ZipEntry ze = null;
			Enumeration enumeration = zipFile.getEntries(); 
			Map<String, List<String>> fileLimit = this.getDeployFilesDetails();
			String filesInfo="";
			while (enumeration.hasMoreElements()) {
				ze = (ZipEntry) enumeration.nextElement();
			String entryName = ze.getName();
		      // File.separator系统分隔符 
                        String path  = rootPath +File.separator + entryName;
				logger.info("部署文件:" + path);
				if (ze.isDirectory()) {
					File decompressDirFile = new File(path);
					if (!decompressDirFile.exists()) {
						decompressDirFile.mkdirs();
					}
				}else {
					createFile(path, zipFile, ze);
					filesInfo+="||"+path;
				}
				
				od.setMsg("部署文件:"+filesInfo);
			}

		} catch (IOException e) {
			od.setCode(-1);
			od.setMsg(e.getMessage());
		} finally {
			if(zipFile != null)
				try {
					zipFile.close();
				} catch (IOException e) {
					zipFile =null;
				}
		}

		return od;
	}
 private File createFile(String path, ZipFile zipFile, ZipEntry ze) throws IOException{
  
         File decompressFile = new File(path);
 
         if(!decompressFile.getParentFile().exists())
            decompressFile.getParentFile().mkdirs();
 
         if(!decompressFile.getParentFile().exists()) 
            decompressFile.createNewFile();
 
         OutputStream outputStream = new FileOutputStream(decompressFile);
 
         InputStream inputStream = zipFile.getInputStream(ze); 
       
  int length = 0;
 
         byte b[] = new byte[1024]; 

         while ((length = inputStream.read(b)) > 0) {
  
            outputStream.write(b, 0, length);
 
            outputStream.flush();
 
          }
  
           inputStream.close();
 
           outputStream.close();
 
           return decompressFile;
 
 }

猜你喜欢

转载自b090023.iteye.com/blog/2316147