dingfirst

On the Road

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  8 随笔 :: 2 文章 :: 3 评论 :: 0 Trackbacks

2006年7月18日 #

励精图治
posted @ 2007-09-11 16:29 dingfirst 阅读(168) | 评论 (0)编辑 收藏

现在在干啥啊!
没有方向,没有动力。
十年之后会是什么样子呢?
nnd
郁闷!
极其郁闷!!!

改变,
要改变啊!!

posted @ 2007-04-25 17:13 dingfirst 阅读(208) | 评论 (0)编辑 收藏

问题:
      直接用URLEncoder.encode(fileName,"UTF-8"),得到的文件名长度会被截断。

解决方法是:
      文件名先用“GB2312”编码,然后用“ISO8859_1”解码。当然也可以在将文件名保存到数据库之前用“GB2312”编码。

代码如下:

 1conn = DBUtil.getConnection();
 2            ps = conn.prepareStatement("SELECT FILE_NAME, CONTENT_TYPE, CONTENT FROM PUB_JOB_ATTACHMENTS WHERE ATTACHID = ?");
 3            ps.setString(1,getAttachId());
 4            rs = ps.executeQuery();
 5            if(rs.next())
 6            {
 7                //java.net.URLEncoder.encode(rs.getString("FILE_NAME"), "UTF-8")
 8                response.setContentType(rs.getString("CONTENT_TYPE"));
 9                String fileName=rs.getString("FILE_NAME");
10                fileName=URLEncoder.encode(fileName,"GB2312");
11                fileName=URLDecoder.decode(fileName, "ISO8859_1");
12                response.addHeader("Content-Disposition""attachment; filename=\"" + fileName + "\"");
13                Blob content = rs.getBlob("CONTENT");
14                InputStream ins = content.getBinaryStream();
15                byte buffer[] = new byte[1024];
16                int length = -1;
17                outs = response.getOutputStream();
18                while((length = ins.read(buffer)) != -1)
19                    outs.write(buffer, 0, length);
20                ins.close();
21                outs.flush();
22            }

posted @ 2006-11-27 18:59 dingfirst 阅读(1149) | 评论 (0)编辑 收藏

先看一下Proxy的两种用法:

public   interface  Foo  {
    
int  doSomthing();
}
   一个基本的实现:
public class FooImpl implements Foo {
    
public FooImpl() {
    }


    
public int doSomthing() {
        System.out.println(
"FooImpl doSomthing");
        
return 10;
    }

    
}
   调用处理类,这也是aop的一种实现方式,呵呵。
public class MyInvocationHandler implements InvocationHandler{
    Object proxyObject;
    
    
public MyInvocationHandler(Object _proxyObject) {
        
this.proxyObject=_proxyObject;
    }


    
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(
"in proxy instance");
        
return method.invoke(proxyObject,args);
    }

}
   两种实现方式:
public class ProxyUse {
    
public ProxyUse() {
    }


    
public void useProxy1() throws SecurityException, NoSuchMethodException, InvocationTargetException,
            IllegalArgumentException, IllegalAccessException, InstantiationException 
{
        FooImpl obj 
= new FooImpl();
        InvocationHandler handler 
= new MyInvocationHandler(obj);
        Class proxyClass 
= Proxy.getProxyClass(
                Foo.
class.getClassLoader(), new Class[] {Foo.class});
        Foo f 
= (Foo) proxyClass.
                getConstructor(
new Class[] {InvocationHandler.class}).
                newInstance(
new Object[] {handler});
        System.out.println(f.doSomthing());
    }

    
    
public void useProxy2() throws SecurityException, NoSuchMethodException, InvocationTargetException,
            IllegalArgumentException, IllegalAccessException, InstantiationException 
{
        FooImpl obj 
= new FooImpl();
        InvocationHandler handler 
= new MyInvocationHandler(obj);
        Foo f 
= (Foo) Proxy.newProxyInstance(
            Foo.
class.getClassLoader(),
            
new Class[] { Foo.class },
            handler
            );
        System.out.println(f.doSomthing());
    }

    
    
public static void main(String [] args){
        ProxyUse use
=new ProxyUse();
        
try{
            use.useProxy1();
            use.useProxy2();
        }
catch(Exception ex){
            ex.printStackTrace();
        }

    }


}

看一下java api doc

static InvocationHandlergetInvocationHandler(Object proxy)
          返回指定代理实例的调用处理程序。
static Class<?>getProxyClass(ClassLoader loader, Class<?>... interfaces)
          返回代理类的 java.lang.Class 对象,并向其提供类加载器和接口数组。
static booleanisProxyClass(Class<?> cl)
          当且仅当指定的类通过 getProxyClass 方法或 newProxyInstance 方法动态生成为代理类时,返回 true。
static ObjectnewProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
          返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。

没时间了,先这样吧。


 

posted @ 2006-07-26 17:48 dingfirst 阅读(256) | 评论 (0)编辑 收藏

     摘要: 原文名称:Double-checked locking and the Singleton pattern                            A comprehensive look at this broken programming idiom 1,请首先看一下下面的这个类,只有第一个getInstance()方法是正确的。其中get...  阅读全文
posted @ 2006-07-18 19:17 dingfirst 阅读(319) | 评论 (0)编辑 收藏