java.lang.Exception:没有方法签名:static java.util.Base64 $ Encoder.encodeToString()适用于参数类型:([B]

时间:2016-08-19 03:01:54

标签: java groovy

我在Groovy中获得了这段代码:

def getPhoto(params){
    def data = entity.find(params).first();
    byte[] mybyte = (byte[]) data.photo;
    String str = java.util.Base64.Encoder.encodeToString(mybyte);
    return str;
}

但是当执行此代码时,它会抛出错误

java.lang.Exception: No signature of method: static java.util.Base64$Encoder.encodeToString() is applicable for argument types: ([B) values: {[-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0,...too long...

已更新

org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list:
  private java.util.Base64$Encoder#<init>(boolean, [B, int, boolean)
   java.util.Base64$Encoder#<init>(boolean, [B, int, boolean, java.util.Base64$1)
        at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:2419)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1250)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1182)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:805)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:227)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNew0(ScriptBytecodeAdapter.java:234)
        at JobSearchService.getPhoto(script1000034.groovy:113)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:333)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:77)
        at JobSearchService$_search_closure2.doCall(script1000034.groovy:59)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:248)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:754)
        at groovy.lang.Closure.call(Closure.java:292)
        at groovy.lang.Closure.call(Closure.java:305)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1078)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1055)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
        at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:754)
        at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:765)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:753)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
        at JobSearchService.search(script1000034.groovy:57)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:754)
        at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:754)
        at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:765)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:753)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
        at JobSearchService.invokeMethod(script1000034.groovy)
        at com.rameses.osiris3.script.ScriptExecutor.invokeMethod(ScriptExecutor.java:41)
        at com.rameses.osiris3.script.ManagedScriptExecutor$1.call(ManagedScriptExecutor.java:142)
        at com.rameses.osiris3.script.InterceptorChain.fireChain(InterceptorChain.java:79)
        at com.rameses.osiris3.script.ManagedScriptExecutor.execute(ManagedScriptExecutor.java:140)
        at com.rameses.osiris3.script.ScriptRunnable.run(ScriptRunnable.java:62)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
error class java.lang.Exception Could not find which method <init>() to invoke from this list:
  private java.util.Base64$Encoder#<init>(boolean, [B, int, boolean)
   java.util.Base64$Encoder#<init>(boolean, [B, int, boolean, java.util.Base64$1)

3 个答案:

答案 0 :(得分:2)

Encoder#encodeToString(byte[])是一个实例方法,而不是静态方法。在实例上调用它:

new Base64.Encoder().encodeToString(mybyte)

请注意Encoder is thread-safe,因此您可以将副本保存在私有静态最终字段中。

在您的方法上使用@TypeChecked@CompileStatic可以防止出现此类错误。

答案 1 :(得分:1)

尝试

  org.apache.commons.codec.binary.Base64.encodeBase64(mybyte)

答案 2 :(得分:0)

由于我使用JDK 6,这是最好的方法:

new sun.misc.BASE64Encoder().encode(mybyte);