2010年9月11日星期六

发布一些个人收藏的java工具类,很实用。

发布一些个人收藏的java工具类,很实用。: "

[工具类] 成各种密码随机串,加密解密,编码解码,执行url.java
[工具类] 读取、打印输出、保存xml .java
[工具类] 获得汉字拼音首字母的java工具类 .java
[工具类] 获取绝对路径 .java
[工具类] 记录log日志文件的工具类 .java
[工具类] 连接数据库的工具类 .java
[工具类] 使用Java程序来实现HTTP文件的队列下载 .java
[工具类] 文件操作工具类 .java
[工具类] 序列化保存为XML文件的工具类 .java
[工具类] 一个压缩工具类 .java
[工具类] 用java编写简单UDP网络通信程序 .java
[工具类] img .jsp
[工具类] 分页split_page.jsp .jsp
[工具类] 中文验证 .jsp
[工具类] CookieCounter .java
[工具类] Java中计算任意两个日期之间的工作天数 .java
[工具类] java抓取网页 .java
[工具类] MD5 .java


import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
try {
URL url = new URL("http://jj.24365pt.com/index.jhtml");

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream in = null;
in = url.openStream();
String content = pipe(in,"utf-8");
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}

static String pipe(InputStream in,String charset) throws IOException {
StringBuffer s = new StringBuffer();
if(charset==null||"".equals(charset)){
charset="utf-8";
}
String rLine = null;
BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
PrintWriter pw = null;

FileOutputStream fo = new FileOutputStream("../index.html");
OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8");
pw = new PrintWriter(writer);
while ( (rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
int str_len = tmp_rLine.length();
if (str_len > 0) {
s.append(tmp_rLine);
pw.println(tmp_rLine);
pw.flush();
}
tmp_rLine = null;
}
in.close();
pw.close();
return s.toString();
}
}

 


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>Title: servlet读取cookie</p>
* <p>Description: 这个servlet演示怎样创建和获取cookie并设置cookie的期限</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: CookieCounter.java</p>
* @author 杜江
* @version 1.0
*/
//必须继承HttpServlet类
public class CookieCounter extends HttpServlet {
&nbsp;&nbsp;private int pageCount = 0;

/**
*方法说明:初始化
*输入参数:ServletConfig config 服务器配置对象
*返回类型:
*/

&nbsp;&nbsp;public void init(ServletConfig config) throws ServletException&nbsp;&nbsp;{
&nbsp; &nbsp; super.init(config);
&nbsp;&nbsp;}
/**
*方法说明:实现service方法
*输入参数:HttpServletRequest req 客户请求对象
*输入参数:HttpServletResponse res 服务器应答对象
*返回类型:
*/
&nbsp;&nbsp;public void service(HttpServletRequest req, HttpServletResponse res)
&nbsp; &nbsp;&nbsp; &nbsp; throws IOException
&nbsp;&nbsp;{
&nbsp; &nbsp; boolean cookieFound = false;
&nbsp; &nbsp; Cookie thisCookie = null;
&nbsp; &nbsp;
&nbsp; &nbsp; // 设置内容类型
&nbsp; &nbsp; res.setContentType("text/html; charset=GB2312");
&nbsp; &nbsp; // 调用getWriter()
&nbsp; &nbsp; PrintWriter out = res.getWriter();
&nbsp; &nbsp;
&nbsp; &nbsp; // 从请求获取coolies
&nbsp; &nbsp; Cookie[] cookies = req.getCookies();
&nbsp; &nbsp;
&nbsp; &nbsp; if(cookies!=null){
&nbsp; &nbsp;&nbsp; &nbsp;for(int i=0; i < cookies.length; i++) {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;thisCookie = cookies[i];
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//检查是否存在CookieCount数据
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if (thisCookie.getName().equals("CookieCount")) {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; cookieFound = true;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp; }
&nbsp; &nbsp; if (cookieFound == false) {
&nbsp; &nbsp;&nbsp; &nbsp;// 创建新的Cookie并设置它的存活期
&nbsp; &nbsp;&nbsp; &nbsp;thisCookie = new Cookie("CookieCount", "1");
&nbsp; &nbsp;&nbsp; &nbsp;thisCookie.setMaxAge(60*1);
&nbsp; &nbsp;&nbsp; &nbsp;// 在response对象中加入cookie
&nbsp; &nbsp;&nbsp; &nbsp;res.addCookie(thisCookie);
&nbsp; &nbsp; }
&nbsp; &nbsp; //输出页面
&nbsp; &nbsp; out.println("<html><head>\n" + "<title>Cookie计数器</title></head><body>\n" +
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "<center><h1>Cookie 计数器</h1></center></font>");
&nbsp; &nbsp; pageCount++;
&nbsp; &nbsp; out.println("<p>");
&nbsp; &nbsp; out.println("<font color=blue size=+1>");
&nbsp; &nbsp; out.println("<p>这个页面您已经拜访了 " + pageCount +
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; " 次.\n");
&nbsp; &nbsp;
&nbsp; &nbsp; // 显示客户端详细信息,是否存在计数器cookie
&nbsp; &nbsp; if (cookieFound) {
&nbsp; &nbsp;&nbsp; &nbsp;int cookieCount = Integer.parseInt(thisCookie.getValue());
&nbsp; &nbsp;&nbsp; &nbsp;cookieCount++;
&nbsp; &nbsp;&nbsp; &nbsp;// 设置cookie的新值, 加到相应对象中
&nbsp; &nbsp;&nbsp; &nbsp;thisCookie.setValue(String.valueOf(cookieCount));
&nbsp; &nbsp;&nbsp; &nbsp;thisCookie.setMaxAge(10);
&nbsp; &nbsp;&nbsp; &nbsp;res.addCookie(thisCookie);
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;out.println("<p>这是你近10秒内第 " +
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;thisCookie.getValue() +
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;" 次拜访这一页\n");
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; } else {
&nbsp; &nbsp;&nbsp; &nbsp;out.println("<p>你在近10秒内没有拜访过此页或者你的浏览器不支持cookie "+
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;"如果你的浏览器支持cookie,请确认是否打开了!\n");
&nbsp; &nbsp; }
&nbsp; &nbsp; out.println("</body></html>");
&nbsp; &nbsp;
&nbsp;&nbsp;}
}



发帖有限制,代码较多不能全部贴上来,我打包放到http://www.javacs.cn/bbs/thread-30-1-1.html 需要的朋友可以去下载。


使用的时候,请保有作者信息,支持原创精神。


 






作者: kangqii 


声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!




已有 4 人发表回复,猛击->>这里<<-参与讨论





JavaEye推荐






"

没有评论:

发表评论