专注于高品质PHP技术等信息服务于一体 [STIEMAP] [RSS]

百度提供的广告:
java
当前位置:首页 > 技术文档 > java >  > 
java常用的代码片断

精典代码锦集
Struts
中英文转换
HttpSession session=request.getSession();
        String en=request.getParameter("en");
        Locale local=new Locale(en);
  session.setAttribute(Globals.LOCALE_KEY, local);
  return mapping.findForward("index");
转网页编码
protected void process(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException, ServletException {
  // TODO Auto-generated method stub
  arg0.setCharacterEncoding("utf-8");
  arg1.setCharacterEncoding("utf-8");
  arg1.setContentType("text/html;charset=utf-8");
  
  super.process(arg0, arg1);
}
ActionSerlet DispcathAction 操作类单例
private oper op;  
public void setServlet(ActionServlet arg0) {
  // TODO Auto-generated method stub
  op=new oper();
  System.out.println(" 操作类,被实例化一次..");
  super.setServlet(arg0);
}
public oper getOp() {
  return op;
}
Filter 应用范例
转编码
public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException {
  // TODO Auto-generated method stub
  arg0.setCharacterEncoding("utf-8");
  ((HttpServletResponse)arg1).setCharacterEncoding("utf-8");
  ((HttpServletResponse)arg1).setContentType("text/html;charset=utf-8");
  System.out.println(" 进入转编码过滤器。。。。");
        arg2.doFilter(arg0, arg1);
}
图片加水印
HttpServletRequest request=(HttpServletRequest)arg0;
  HttpServletResponse response=(HttpServletResponse)arg1;
  String imgPath=request.getServletPath();
  String path=request.getRealPath(imgPath);
  
  FileInputStream in=new FileInputStream(path);
  Image img=ImageIO.read(in);
  int w=img.getWidth(null);
  int h=img.getHeight(null);
  BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_INT_BGR);
  Graphics g=bi.getGraphics();
  g.drawImage(img,0,0,w,h,null);
  
  g.setFont(new Font("宋体",Font.BOLD,69));
  g.setColor(new Color(255,0,0));
  g.drawString(" 清如许", w-300, h-200);
  g.dispose();
  
  JPEGImageEncoder n=JPEGCodec.createJPEGEncoder(response.getOutputStream());
  n.encode(bi);
  response.getOutputStream().close();
  
  arg2.doFilter(arg0, arg1);
生成验证码
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Random r=new Random();
  int w=80;
  int h=40;
        BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        Graphics g=img.getGraphics();
        g.setColor(getColor());
        g.fillRect(0, 0, w, h);
        g.setColor(getColor());
        g.drawRect(1, 1, w-2, h-2);
        int i=25;
        while(i>0)
        {
         int x1=r.nextInt(w);
         int x2=r.nextInt(w);
         int y1=r.nextInt(h);
         int y2=r.nextInt(h);
         g.setColor(getColor());
         g.drawLine(x1, y1, x2, y2);
         /*x1 - 第一个点的 x 坐标。
           y1 - 第一个点的 y 坐标。
           x2 - 第二个点的 x 坐标。
           y2 - 第二个点的 y 坐标。
            */
         --i;
        }
        i=4;
        g.setFont(new Font("宋体",Font.PLAIN,21));
        while(i>0)
        {
         int str=r.nextInt(20901)+19968;
         g.setColor(getColor());
         g.drawString((char)str+"", w/5*i, h/2);
         i--;
        }
        g.dispose();
        ImageIO.write(img, "GIF", response.getOutputStream());
        
}
public Color getColor()
{
  Random rd=new Random();
  int r=rd.nextInt(150)+100;
  int g=rd.nextInt(150)+100;
  int b=rd.nextInt(150)+100;
  return new  Color(r,g,b);
}

两种输出 语句:
//        ImageIO.write(img, "JPEG",response.getOutputStream());
//        JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
//        e.encode(img);
//        response.getOutputStream().close();