浏览 2289 次
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-02-23
最后修改:2010-03-17
接上篇 webserver使用的是resin3.1.9,首先在web.xml中配置invoker方式来处理请求 <?xml version="1.0" encoding="UTF-8"?> <web-app> <classpath id='WEB-INF/classes' source='WEB-INF/src' compile='false'/> <classpath id='WEB-INF/lib' library-dir='true'/> <servlet-mapping url-pattern='/servlet/*' servlet-name='invoker'/> </web-app>
然后写一个search.jsp,选择开始、结束日期后,将提交到UpdateServlet来处理 <form name="form" method="post" action="/servlet/com.swfml.update.action.UpdateServlet"> ...... </form> 然后来写UpdateServlet,根据不同的action执行不同的操作 public void doPost(HttpServletRequest request, HttpServletResponse response) { String defaultAction = ""; String url = actionMap(request, response, defaultAction); if ((url.length() > 0) && (!response.isCommitted())) { try { response.sendRedirect(URLDecoder.decode(url)); } catch (IOException e) { e.printStackTrace(); } } } private String actionMap(HttpServletRequest request, HttpServletResponse response, String defaultAction) { String action = defaultAction; String url = ""; if ((request.getParameter("action") != null) && (request.getParameter("action").length() > 0)) { action = request.getParameter("action").trim(); } if(action.equals("search")) { doSearch(request, response); } else if(action.equals("create")) { doCreate(request, response); } else if(action.equals("upload")){ doUpload(request, response); } else if(action.equals("unzip")){ doUnzip(request, response); } else if(action.equals("update")){ doUpdate(request, response); } return url; } 然后来处理检索 private void doSearch(HttpServletRequest request, HttpServletResponse response) { String startDate = request.getParameter("startDate"); String endDate = request.getParameter("endDate"); String searchDir = getServletContext().getRealPath("/"); searchDir = searchDir.substring(0, searchDir.length() - 1); ArrayList updateFileList = ProUpdate.searchFile(searchDir, startDate, endDate); request.setAttribute("updateFileList", updateFileList); request.getSession().setAttribute("updateFileList", updateFileList); request.setAttribute("updateMessage", ProUpdate.updateMessage); try { RequestDispatcher rd = request.getRequestDispatcher("/update/search.jsp"); rd.forward(request, response); } catch (Exception e) { e.printStackTrace(); } } 其它处理类似,包含的功能有: 文件检索、生成zip包,解压zip包、备份替换文件、servlet实现上传下载
生成zip包 /** * 将更新文件列表打包到输出文件 * @param outFile * @param updateFileList * @throws FileNotFoundException */ public static void createZip(String zipFile, ArrayList updateFileList) throws FileNotFoundException { String searchDir = getSearchDir(updateFileList); File outfile; if(zipFile.length() > 0) { outfile = new File(zipFile); } else { outfile = new File(updateFile); } ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(outfile)); byte[] b = new byte[1024]; int read = 0; StringBuffer strBuffer = new StringBuffer(); try { for(int i = updateFileList.size() - 1; i >=0; i --) { File file = (File)updateFileList.get(i); String filePath = ""; if(file.getPath().length() > searchDir.length()) { filePath = file.getPath().substring(searchDir.length() + 1) + (file.isDirectory() ? "/" : ""); } else { filePath = "/"; } filePath = filePath.replace('\\', '/'); if(!pathIsExit(filePath)) { outputStream.putNextEntry(new ZipEntry(filePath)); } if(file.isFile()) { strBuffer.append("/" + filePath + "\r\n"); FileInputStream inputStream = new FileInputStream(file); while((read = inputStream.read(b)) != -1) { outputStream.write(b, 0, read); } inputStream.close(); } } outputStream.putNextEntry(new ZipEntry(msgFileName)); outputStream.write(strBuffer.toString().getBytes()); outputStream.closeEntry(); printZipMessage(outfile.getPath()); } catch(Exception e) { e.printStackTrace(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } 解压zip包 /** * 解压文件到指定目录 * @param zipFilePath * @throws FileNotFoundException */ public static String unZip(String outputPath, String zipFilePath) throws FileNotFoundException { ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry; Date date = new Date(); if(outputPath.length() > 0) { outputDir = outputPath; } outputDir = outputDir + File.separator + timeDateFormat.format(date); File outputFile = new File(outputDir); outputFile.mkdirs(); String updateMesaage = outputDir + "!"; try { while((entry = inputStream.getNextEntry()) != null) { if(entry.isDirectory()) { File file = new File(outputDir + File.separator + entry.getName().substring(0, entry.getName().length() - 1)); file.mkdirs(); } else { File file = new File(outputDir + File.separator + entry.getName()); System.out.println(file.getPath()); if(!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); FileOutputStream outputStream = new FileOutputStream(file); byte[] b = new byte[1024]; int read = 0; while((read = inputStream.read(b)) != -1) { outputStream.write(b, 0, read); } outputStream.close(); if(file.getName().equals(msgFileName)) { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String s = ""; while((s = reader.readLine()) != null) { updateMesaage += s + "#"; } } } } inputStream.close(); } catch(Exception e) { e.printStackTrace(); } return updateMesaage; } 下载更新包 /** * 生成更新包 * @param request * @param response */ private void doCreate(HttpServletRequest request, HttpServletResponse response) { ArrayList updateFileList = new ArrayList(); String zipFile = getServletContext().getRealPath("/") + File.separator + "update.zip"; if(request.getSession().getAttribute("updateFileList") != null) { updateFileList = (ArrayList)request.getSession().getAttribute("updateFileList"); ProUpdate.createUpdateFile(zipFile, updateFileList); } response.setContentType("text/html"); try { response.sendRedirect("/update.zip"); } catch (IOException e) { e.printStackTrace(); } } 上传更新包 /** * 上传更新包 * @param request * @param response */ private void doUpload(HttpServletRequest request, HttpServletResponse response) { try { File updatefile = new File(getServletContext().getRealPath("/") + File.separator + "WEB-INF" + File.separator + "update" + File.separator + "update.zip"); File tempFile = new File(getServletContext().getRealPath("/") + File.separator + "WEB-INF" + File.separator + "update" + File.separator + "update.temp.txt"); if(!tempFile.exists()) { tempFile.createNewFile(); } RandomAccessFile raFile = new RandomAccessFile(tempFile, "rw"); ServletInputStream inputStream = request.getInputStream(); FileOutputStream outputStream = new FileOutputStream(updatefile); byte[] b = new byte[1024]; int read = 0; while((read = inputStream.read(b)) != -1) { raFile.write(b, 0, read); } inputStream.close(); raFile.close(); raFile = new RandomAccessFile(tempFile, "r"); String startFlag = raFile.readLine(); String endFlag = startFlag + "--"; String line = null; raFile.seek(0); long startIndex = 0; long endIndex = 0; while((line = raFile.readLine()) != null) { if(line.equals(startFlag) && !line.equals(endFlag)) { startIndex = raFile.getFilePointer() - (startFlag.length() + 2); } else if(line.equals(endFlag)){ endIndex = raFile.getFilePointer() - (endFlag.length() + 2); } } raFile.seek(0); raFile.seek(startIndex); raFile.readLine(); raFile.readLine(); raFile.readLine(); raFile.readLine(); while(raFile.getFilePointer() < endIndex) { outputStream.write(raFile.readByte()); } outputStream.close(); raFile.close(); if(tempFile.exists()) { tempFile.delete(); } request.setAttribute("updatefilepath", updatefile.getPath()); RequestDispatcher rd = request.getRequestDispatcher("/update/uploadResult.jsp"); rd.forward(request, response); } catch(Exception e) { e.printStackTrace(); } } 具体请参见代码,欢迎拍砖!
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-02-26
很不错的收藏了,项目中应该会很有用,楼主是这是历年项目经验啊。
|
|
返回顶楼 | |
发表时间:2010-02-26
项目中也需要类似的东西,感谢楼主啊。过滤的地方可以改进一下。
File[] files = rootDir.listFiles(getFileRegexFilter("\\.svn")); public static FilenameFilter getFileRegexFilter(String regex) { final String regex_ = regex; return new FilenameFilter() { public boolean accept(File file, String name) { boolean ret = name.matches(regex_); return !ret; } }; } |
|
返回顶楼 | |
发表时间:2010-03-01
whiteface999 写道 项目中也需要类似的东西,感谢楼主啊。过滤的地方可以改进一下。
File[] files = rootDir.listFiles(getFileRegexFilter("\\.svn")); public static FilenameFilter getFileRegexFilter(String regex) { final String regex_ = regex; return new FilenameFilter() { public boolean accept(File file, String name) { boolean ret = name.matches(regex_); return !ret; } }; } 恩,这个工具的核心就是检索文件,所以想提高效率也就是在这里优化 |
|
返回顶楼 | |
发表时间:2010-03-01
ahpo 写道 很不错的收藏了,项目中应该会很有用,楼主是这是历年项目经验啊。
惭愧,惭愧 |
|
返回顶楼 | |