Java tutorial
/** * PureInfo Command * @(#)SearchCheckCodeAction.java 1.0 2008-12-2 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.outlay.action; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import org.apache.commons.httpclient.util.DateUtil; import org.apache.poi.util.StringUtil; import org.apache.struts.action.ActionForward; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.lang.StrConvertor; import com.pureinfo.force.lang.StrUtil; import com.pureinfo.srm.sendoutlayunit.model.MD5; public class SearchCheckCodeAction extends ActionBase { public ActionForward executeAction() throws PureException { // TODO Auto-generated method stub String zy = request.getParameter("dp_abstract"); String amt1 = request.getParameter("dp_money1"); String amt2 = request.getParameter("dp_money2"); String datestr1 = request.getParameter("date1"); String datestr2 = request.getParameter("date2"); String fno = request.getParameter("dp_checkcode"); if (zy != null) { try { zy = new String(zy.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException ex) { // TODO Auto-generated catch block ex.printStackTrace(System.err); } } String urlStr = "http://cwcx.zju.edu.cn/servlet/cwTxtPub?funcno=KYDKINFO"; StringBuffer url = new StringBuffer(); url.append(urlStr); MD5 md5 = new MD5(); if (fno != null) { url.append("&fno=" + fno); } else { url.append("&fno="); } if (zy != null) { url.append("&zy=" + zy); } else { url.append("&zy="); } if (amt1 != null && !"".equals(amt1.trim())) { url.append("&amt1=" + amt1); } else { url.append("&amt1=-99999999999"); } if (amt2 != null && !"".equals(amt2.trim())) { url.append("&amt2=" + amt2); } else { url.append("&amt2=99999999999"); } if (datestr1 != null && !"".equals(datestr1.trim())) { url.append("&date1=" + datestr1); } else { url.append("&date1=1800-01-01"); } if (datestr2 != null && !"".equals(datestr2.trim())) { url.append("&date2=" + datestr2); } else { url.append("&date2=2099-12-31"); } String test = url.toString(); String[] arr = test.split("\\?"); String md5test = "KYDKINFO" + arr[1] + "A3ixoSisQ"; String md5sign = md5.getMD5Encode(md5test); url.append("&sign=" + md5sign); String content = getPageContent(url.toString(), ""); String tableInfo = formTable(content); url.setLength(0); request.setAttribute("content", tableInfo); return mapping.findForward("success"); } /** * * @param _content * @return */ public String formTable(String _content) { String tableInfo = ""; if (_content != null && !"".equals(_content.trim())) { String[] rowData = _content.split("\\n"); StringBuffer tableStr = new StringBuffer(); for (int i = 0; (rowData.length >= 20 ? i < 20 : i < rowData.length); i++) { tableStr.append("<TR id=\"tr_37431\" bgcolor=\"#F6F6F6\">"); String[] columnData = rowData[i].split("~"); tableStr.append("<TD align=\"center\" class=\"tablebody\">" + i + "</TD>"); for (int j = 0; j < columnData.length; j++) { if (j == 0) { String datatemp = ""; if (!"".equals(columnData[3])) { datatemp = columnData[3].substring(0, 10); } tableStr.append( "<td class=\"tablebody\" align=\"left\" ><a href=\"javascript:top.opener.insertCheckCodeInfo('" + columnData[0] + "','" + datatemp + "','" + columnData[5] + "','" + columnData[4] + "');top.window.close();\" target=\"\" title=\"\" >" + columnData[0] + "</a> </td>"); } else { tableStr.append("<td class=\"tablebody\" align=\"left\" NOWRAP>"); if (j == 3) { if (!"".equals(columnData[j])) { tableStr.append(columnData[j].substring(0, 10)); } } else { tableStr.append(columnData[j]); } tableStr.append("</td>"); } } tableStr.append("</TR>"); } tableStr.append("</td></tr>"); tableInfo = tableStr.toString(); tableStr.setLength(0); } return tableInfo; } /** * * @param strUrl * @param strPostRequest * @return */ public static String getPageContent(String strUrl, String strPostRequest) { // StringBuffer buffer = new StringBuffer(); System.setProperty("sun.net.client.defaultConnectTimeout", "5000"); System.setProperty("sun.net.client.defaultReadTimeout", "5000"); try { URL newUrl = new URL(strUrl); HttpURLConnection hConnect = (HttpURLConnection) newUrl.openConnection(); //POST if (strPostRequest.length() > 0) { hConnect.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(hConnect.getOutputStream()); out.write(strPostRequest); out.flush(); out.close(); } // BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream())); int ch; for (int length = 0; (ch = rd.read()) > -1; length++) buffer.append((char) ch); rd.close(); hConnect.disconnect(); return buffer.toString().trim(); } catch (Exception e) { // return ":"; return null; } finally { buffer.setLength(0); } } public String makeMD5(String password) { MessageDigest md; try { // MD5 md = MessageDigest.getInstance("MD5"); // md5 md.update(password.getBytes()); // digest()md5 hash8md5 hash16hex8 // BigInteger816hexhash String pwd = new BigInteger(1, md.digest()).toString(16); System.err.println(pwd); return pwd; } catch (Exception e) { e.printStackTrace(); } return password; } }