📄 lp_readwritefile.java
字号:
package com.power.pipeengine;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;/** * Read/Write Any File * Creation date: 2002/01/15 * @author: vicksong */public class LP_ReadWriteFile extends HttpServlet { int BLOCK = 1024*1024; public LP_ReadWriteFile() { super(); initialize(); } /** * Process incoming HTTP GET requests * * @param request Object that encapsulates the request to the servlet * @param response Object that encapsulates the response from the servlet */ public void doGet( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); response.setStatus(HttpServletResponse.SC_OK); String cmd = request.getParameter("cmd"); String FileName = request.getParameter("FileName"); /**** check Parameters ****/ if (cmd == null || FileName == null) { out.println("**Error: Reading Missing parameters."); System.out.println("**Error: Reading Missing parameters."); return; } /**** check Legal "cmd" ****/ if (!cmd.equals("ReadModel")) { out.println("**Error: Invalid 'cmd' parameter -->" + cmd); System.out.println("**Error: Invalid 'cmd' parameter -->" + cmd); return; } /**** get Content from File ****/ BufferedInputStream bis = null; try { System.out.println("##Start File Reading"); //check existing File file = new File(FileName); if (!file.exists()) throw new IOException("file '"+ file.getPath() + "' not exists."); System.out.println("Reading File --> " + FileName); //getting Bytes bis = new BufferedInputStream(new FileInputStream(file)); int total = bis.available(); byte b[] = new byte[BLOCK]; int count = 1; int len = 0; while ((len=bis.read(b)) != -1) { out.print(new String(b,0,len)); System.out.println(count+++"M block bytes read out."); } System.out.println(total + " bytes Read OK."); out.close(); } catch (IOException e) { System.out.println("**Error: " + e.toString()); out.println("**Error: " + e.toString()); } finally { if (bis != null) bis.close(); bis = null; System.out.println("End File Reading"); } } /** * Process incoming HTTP POST requests * * @param request Object that encapsulates the request to the servlet * @param response Object that encapsulates the response from the servlet */ public void doPost( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); response.setStatus(HttpServletResponse.SC_OK); /**** Write File ****/ String cmd = ""; String FileName = ""; boolean isAppend = false; BufferedInputStream bin = null; BufferedOutputStream bout = null; try { System.out.println("##Start File Writing"); Hashtable data = HttpUtils.parseQueryString(request.getQueryString()); Enumeration enum = data.keys(); /**** get Parameters ****/ while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); System.out.println("Parameter name:" + name); String[] values = (String[]) data.get(name); if (name.equals("FileName")) FileName = values[0]; else if (name.equals("cmd")) cmd = values[0]; else if (name.equals("Append")) isAppend = values[0].equals("true"); } /**** check Parameters ****/ if (cmd == "" || FileName == "") { out.println("**Error: Writing Missing parameters."); System.out.println("**Error: Writing Missing parameters."); return; } /**** check Legal "cmd" ****/ if (!cmd.equals("WriteModel")) { out.println("**Error: Invalid 'cmd' parameter -->" + cmd); System.out.println("**Error: Invalid 'cmd' parameter -->" + cmd); return; }// FileContent.append((i > 0 ? "\r\n" : "") + values[i].replace('\\','+')); /**** write Data ****/ System.out.println("Writing File --> " + FileName); bin = new BufferedInputStream(request.getInputStream()); bout = new BufferedOutputStream(new FileOutputStream(FileName,isAppend)); int total = request.getContentLength(); byte b[] = new byte[BLOCK]; int count = 1; int len = 0; while ((len=bin.read(b)) != -1) { bout.write(b,0,len); System.out.println(count+++"M bytes block write in."); } bout.flush(); System.out.println(total + " bytes Wrote OK."); } catch (IOException e) { out.print("**Error: " + e.toString()); System.out.println("**Error: " + e.toString()); } finally { if (bin != null) bin.close(); if (bout != null) bout.close(); System.out.println("End File Writing"); } } /** * Returns the servlet info string. */ public String getServletInfo() { return super.getServletInfo(); } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(java.lang.Throwable exception) { System.out.println("--------- UNCAUGHT EXCEPTION ---------"); exception.printStackTrace(System.out); } /** * Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { // insert code to initialize the servlet here super.init(config); } /** * Initialize the class. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void initialize() { try { System.out.println("initialize() method is called."); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } /** * Process incoming requests for information * * @param request Object that encapsulates the request to the servlet * @param response Object that encapsulates the response from the servlet */ public void performTask( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { } /** * service method comment. */ public void service(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { String sMethod; sMethod = request.getMethod(); if (sMethod.equals("GET")) doGet(request, response); else doPost(request, response); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -