process

//取得所有参数及值;

String url = request.getServletPath() + "?" + request.getQueryString();
     String QueryPath = request.getServletPath() + "?" + request.getQueryString();
     Enumeration enumOne=request.getParameterNames();
     while(enumOne.hasMoreElements()){ 
      String s = (String)enumOne.nextElement(); 
      if(s.equals("method"))
      String queryStr += request.getParameter(s);
      System.out.println("Parameter\t" + s + "\t=\t" +queryStr);
     }

/**
     * 将接收的参数转换编码
     * @param parm
     * @return
     */
    public String enCoderRequestParm(String parm){
  String temp ="";
  try {
   temp= new String(parm.getBytes("ISO-8859-1"), "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return temp;
 }

/**
     * 下载模板文件,
     * @param filePath
     * @param response
     */
 @SuppressWarnings("unused")
 protected void downTemplateFileTo(String filePath, HttpServletResponse response) {
  File file = new java.io.File(filePath);
  long long_len = file.length();
  FileInputStream inputStream = null;
  try {
   inputStream = new FileInputStream(file);
   if (inputStream != null) {
    String fname = file.getName();
    response.setContentType("application/x-msdownload");
    String header = "attachment; filename=" + fname;
    response.setHeader("Content-Disposition", header);
    response.setContentLength((int) long_len);
    byte[] b = new byte[2048];
    int len = 0;
    while ((len = inputStream.read(b)) != -1) {
     try {
     response.getOutputStream().write(b, 0, len);
     }catch(Exception ex){
      
     }
    }
   }
   inputStream.close();
  } catch (Exception ex) {
   System.out.println("Download error: ");
   ex.printStackTrace();
  }
 }
 
 
  /**
     * 下载模板文件,
     * @param filePath
     * @param response
     */
 @SuppressWarnings("unused")
 protected void downTemplateFileTo(String filePath, HttpServletResponse response,String fname) {
  
  try {
   fname = new String(fname.getBytes(), "iso8859-1");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  File file = new java.io.File(filePath);
  long long_len = file.length();
  FileInputStream inputStream = null;
  try {
   inputStream = new FileInputStream(file);
   if (inputStream != null) {
   
    response.setContentType("application/x-msdownload");
    String header = "attachment; filename=" + fname;
    response.setHeader("Content-Disposition", header);
    response.setContentLength((int) long_len);
    byte[] b = new byte[2048];
    int len = 0;
    while ((len = inputStream.read(b)) != -1) {
     try {
     response.getOutputStream().write(b, 0, len);
     }catch(Exception ex){
     }
    }
   }
   inputStream.close();
  } catch (Exception ex) {
   System.out.println("Download error: ");
   ex.printStackTrace();
  } finally{
   File f = new java.io.File(filePath);
   if(f.exists()){
    f.delete();
   }
  }
 }
 
 
 
 
  /**
     * 下载模板文件,
     * @param filePath
     * @param response
     */
 @SuppressWarnings("unused")
 protected void downTemplateFileTo(String filePath, HttpServletResponse response,String fname,String nouser) {
  
  try {
   fname = new String(fname.getBytes(), "iso8859-1");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  File file = new java.io.File(filePath);
  long long_len = file.length();
  FileInputStream inputStream = null;
  try {
   inputStream = new FileInputStream(file);
   if (inputStream != null) {
   
    response.setContentType("application/x-msdownload");
    String header = "attachment; filename=" + fname;
    response.setHeader("Content-Disposition", header);
    response.setContentLength((int) long_len);
    byte[] b = new byte[2048];
    int len = 0;
    while ((len = inputStream.read(b)) != -1) {
     try {
     response.getOutputStream().write(b, 0, len);
     }catch(Exception ex){
     }
    }
   }
   inputStream.close();
  } catch (Exception ex) {
   System.out.println("Download error: ");
   ex.printStackTrace();
  } finally{
   
  }
 }
 
 /**
  * 创建文件
  * @param filePath 文件的全路径
  * @return
  */
 public File createUploadLogWriter(String filePath) {
  File file = null;
  try {
   file = new File(filePath);
   if (file.exists()) {
    log.info("文件存在");
   } else {
    log.info("文件不存在,正在创建...");
    if (file.createNewFile()) {
     log.info("文件创建成功!");
    } else {
     log.info("文件创建失败!");
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return file;
 }
 /**
  * 验证文件的类型是否为传入的类型
  * @param fileName
  * @param type
  * @return
  */
 public boolean checkThisFileType(String fileName, String type) {
  String types[] = fileName.split("\\.");
  String ttype = types[types.length - 1];
  if (ttype.equalsIgnoreCase(type) || ttype.equalsIgnoreCase(type)) {
   return true;
  } else {
   return false;
  }
 }
 /**
  * 创建日志文件
  * fpath = "\\模块目录\\log\\类型Log.txt";
  * @param request
  * @param fpath
  * @return
  */
 public BufferedWriter createLogWriter(HttpServletRequest request, String fpath) {
  String basePath = request.getSession().getServletContext().getRealPath("");
  String fullPath = basePath + fpath;
  BufferedWriter output = null;
  try {
   File f = this.createUploadLogWriter(fullPath);
   output = new BufferedWriter(new FileWriter(f));
  } catch (Exception e) {
   e.printStackTrace();
  }
  return output;
 }

 

你可能感兴趣的:(java,request,log,url,download)