1、上传ZIP文件
public class BaseAction extends ActionSupport { private static final long serialVersionUID = 1L; private HttpServletRequest request; private HttpServletResponse response; private File file; private String fileFileName; private String fileContentType; private String fileType; private String fileSize; protected void uploadFile(String floder)throws Exception{ InputStream is = null; OutputStream os = null; try{ if(file != null){ String[] fnType = fileFileName.split("\\."); if(fileType.indexOf(fnType[1].toUpperCase())!=-1){ String realPath = ServletActionContext.getServletContext().getRealPath(floder); //getRequest().get File fileR = new File(realPath); if(!fileR.exists()){ fileR.mkdirs(); } fileR = new File(realPath,fileFileName); if(!fileR.exists()){ fileR.createNewFile(); } os = new FileOutputStream(fileR); is = new FileInputStream(file); byte[] b = new byte[1024*100]; int length = 0; boolean tag = false; StringBuilder sbC = new StringBuilder(); while((length=is.read(b))!=-1){ if(length>Integer.parseInt(fileSize)){ tag = true; getRequest().setAttribute("fileS", "fileSize"); fileR.delete(); break; } os.write(b,0,length); sbC.append(new String(b,0,length,"utf-8")); } if(tag==false){ getRequest().setAttribute("content", sbC.toString()); getRequest().setAttribute("filePath", floder+"/"+fileFileName); } }else{ getRequest().setAttribute("fileT", "fileType"); } } }catch(Exception e){ e.printStackTrace(); throw e; }finally{ if(is != null){ is.close(); } if(os != null){ os.close(); } } } public HttpServletRequest getRequest() { return request = ServletActionContext.getRequest(); } public void setRequest(HttpServletRequest request) { this.request = request; } public HttpServletResponse getResponse() { return response = ServletActionContext.getResponse(); } public void setResponse(HttpServletResponse response) { this.response = response; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileSize() { return fileSize; } public void setFileSize(String fileSize) { this.fileSize = fileSize; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } }
2、读取ZIP文件,并开始解压文件,读取文件内容
代码中向旗添加的为主要程序
public class CrawlerMgrConfAction extends BaseAction{ private static final long serialVersionUID = 1L; private CrawlerMgrConfService crawlerMgrConfService; private List<CrawlerMgrConf> listCrawlerMgrCon; private CrawlerMgrConf crawlerMgrConf; private OpponentMgrService opponentMgrService; private String alterInfo =""; protected Log log = LogFactory.getLog(this.getClass()); public OpponentMgrService getOpponentMgrService() { return opponentMgrService; } public void setOpponentMgrService(OpponentMgrService opponentMgrService) { this.opponentMgrService = opponentMgrService; } private List<Site> listPisTSite; public String toCrawlerMgrConfList()throws Exception{ try{ listCrawlerMgrCon = crawlerMgrConfService.getListCrawlerMgrConfByObj(crawlerMgrConf); listPisTSite = opponentMgrService.getSiteList(null); Map<Integer, Site> mapPisTSite = new HashMap<Integer, Site>(); for(Site pts :listPisTSite){ mapPisTSite.put(pts.getSiteId().intValue(), pts); } getRequest().setAttribute("mapPisTSite", mapPisTSite); return "crawlerMgrConfList"; }catch(Exception e){ e.printStackTrace(); throw e; } } public String toCrawlerMgrConfAdd()throws Exception{ try{ listPisTSite = opponentMgrService.getSiteList(null); return "crawlerMgrConfAdd"; }catch(Exception e){ e.printStackTrace(); throw e; } } /** * 向旗添加 * 实现点击后跳转到批量上传的页面 * @return * @throws Exception */ public String toCrawlerMgrConfBatchSave()throws Exception{ try{ listPisTSite = opponentMgrService.getSiteList(null); return "crawlerMgrConfBatchSave"; }catch(Exception e){ e.printStackTrace(); throw e; } } /** * 向旗添加 * 实现批量增加配置文件或者批量修改配置文件,能够对一个ZIP包 * 里的文件实现对数据库的更新和增加操作 * @return * @throws Exception */ public String doCrawlerMgrConfBatchSave()throws Exception{ crawlerMgrConf = new CrawlerMgrConf(); //实现ZIP文件的上传到upload_file_zip文件夹 String folder = "upload_file_zip"; String realPath = ServletActionContext.getServletContext().getRealPath("upload_file_zip"); try{ File myFilePath = null ; if(this.getFile()!=null){ this.uploadFile(folder); log.info("得到的ZIP文件的路径"+realPath+"\\"+this.getFileFileName()); //对存储的ZIP文件进行解压缩 ZipFile zipFile = new ZipFile(realPath+"\\"+this.getFileFileName()); Enumeration emu =zipFile.entries(); //存储上传的文件名 String xmlAddFilesNames = ""; String[] xmlFilearr ; while(emu.hasMoreElements()){ ZipEntry entry = (ZipEntry)emu.nextElement(); String fileName = entry.getName(); String fileNath = realPath+"\\"+entry.getName(); if(entry.isDirectory()){ myFilePath = new File(realPath+"\\"+entry.getName()); if(!myFilePath.exists()){ myFilePath.mkdirs(); } continue; } BufferedInputStream bis1 = new BufferedInputStream(zipFile.getInputStream(entry)); File fileins = new File(realPath+"\\"+entry.getName()); if(!fileins.exists()){ fileins.createNewFile(); } FileOutputStream fos = new FileOutputStream(fileins); BufferedOutputStream bos = new BufferedOutputStream(fos,2048); int count ; byte data[] = new byte[2048]; StringBuilder sbC = new StringBuilder(); while((count = bis1.read(data,0,2048))!=-1){ // bos.write(data,0,count); sbC.append(new String(data,0,2048,"utf-8")); } xmlAddFilesNames = fileName; xmlFilearr = xmlAddFilesNames.split("\\."); //通过文件名判断属于哪个网站 listPisTSite = opponentMgrService.getSiteList(null); int i = 0; for(int j=0;j<listPisTSite.size();j++){ if(xmlAddFilesNames.indexOf(listPisTSite.get(j).getShortName())>-1){ crawlerMgrConf.setSiteId(listPisTSite.get(j).getSiteId()); i=1; } } if(i==0){ alterInfo ="根据上传文件判断文件名与网站的缩写不一致或有不存在的对手网站"+xmlAddFilesNames; return "crawlerMgrConfBatchSave"; } if(!xmlFilearr[1].equalsIgnoreCase("xml")){ alterInfo ="包里存在文件不符合格式"+xmlAddFilesNames; return "crawlerMgrConfBatchSave"; }else{ crawlerMgrConf.setFilePath(fileNath); crawlerMgrConf.setCreateTime(new Date()); crawlerMgrConf.setUpdateTime(new Date()); crawlerMgrConf.setContent(sbC.toString()); crawlerMgrConf.setType(xmlFilearr[0].split("/")[1]); crawlerMgrConfService.insertCrawlerMgrConf(crawlerMgrConf);} bos.flush(); bos.close(); bis1.close(); } zipFile.close(); } return "crawlerMgrConfList"; }catch(Exception e){ e.printStackTrace(); throw e; } } public String doCrawlerMgrConfSave()throws Exception{ try{ if(crawlerMgrConf!=null && this.getFile()!=null){ //WEB-INF/classes/crawler_conf this.uploadFile("upload_file"); if(!this.getFileFileName().split("\\.")[0].equals(crawlerMgrConf.getType())){ alterInfo ="文件名必须和配置名一样"; listPisTSite = opponentMgrService.getSiteList(null); return "crawlerMgrConfAdd"; } Object fileT = getRequest().getAttribute("fileT"); Object fileS = getRequest().getAttribute("fileS"); if(fileT!=null || fileS!=null){ return toCrawlerMgrConfAdd(); } String filePath = (String)getRequest().getAttribute("filePath"); if(UtilFunction.isNotEmpty(filePath)){ crawlerMgrConf.setFilePath(filePath); crawlerMgrConf.setCreateTime(new Date()); crawlerMgrConf.setUpdateTime(new Date()); crawlerMgrConf.setContent((String)getRequest().getAttribute("content")); crawlerMgrConfService.insertCrawlerMgrConf(crawlerMgrConf); } } return "crawlerMgrConfList"; }catch(Exception e){ e.printStackTrace(); throw e; } } public String toCrawlerMgrConfModify()throws Exception{ try{ if(crawlerMgrConf!=null && UtilFunction.isNotEmpty(crawlerMgrConf.getType()) && crawlerMgrConf.getSiteId()!=null){ listPisTSite = opponentMgrService.getSiteList(null); crawlerMgrConf = crawlerMgrConfService.getCrawlerMgrConfByType(crawlerMgrConf); } return "crawlerMgrConfAdd"; }catch(Exception e){ e.printStackTrace(); throw e; } } public String doCrawlerMgrConfDelete()throws Exception{ try{ if(crawlerMgrConf!=null && UtilFunction.isNotEmpty(crawlerMgrConf.getType())){ crawlerMgrConfService.deleteCrawlerMgrConfByType(crawlerMgrConf); getRequest().setAttribute("sucess", "sucess"); } listCrawlerMgrCon = crawlerMgrConfService.getListCrawlerMgrConfByObj(crawlerMgrConf); return "crawlerMgrConfList"; }catch(Exception e){ e.printStackTrace(); throw e; } } public CrawlerMgrConfService getCrawlerMgrConfService() { return crawlerMgrConfService; } public void setCrawlerMgrConfService(CrawlerMgrConfService crawlerMgrConfService) { this.crawlerMgrConfService = crawlerMgrConfService; } public List<CrawlerMgrConf> getListCrawlerMgrCon() { return listCrawlerMgrCon; } public void setListCrawlerMgrCon(List<CrawlerMgrConf> listCrawlerMgrCon) { this.listCrawlerMgrCon = listCrawlerMgrCon; } public CrawlerMgrConf getCrawlerMgrConf() { return crawlerMgrConf; } public void setCrawlerMgrConf(CrawlerMgrConf crawlerMgrConf) { this.crawlerMgrConf = crawlerMgrConf; } public List<Site> getListPisTSite() { return listPisTSite; } public void setListPisTSite(List<Site> listPisTSite) { this.listPisTSite = listPisTSite; } public String getAlterInfo() { return alterInfo; } public void setAlterInfo(String alterInfo) { this.alterInfo = alterInfo; } }
如果在linux下发布,则需要更改路径/为File.separator