以下是我的代码和强制转换的异常
如果我在groovy IDE(智能创意)中运行相同的代码,那么获取castException
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.http.impl.client.DefaultHttpClient;
public class File2 {
private static String paramNameCobSessionToken = "03172014_0:78bf85ca1f";
private static String paramNameUserSessionToken = "dvsdfsdff8d7";
public String verifyAndUpdateBridgetApp(String cobrandSessionToken,String userSessionToken) {
DefaultHttpClient httpclient = new DefaultHttpClient();
String response = null;
System.out.println("hello");
String HOST_URI3 = "https://192.168:10443/srest/v1.0/";
String url = HOST_URI3 + "/updateBridgetData";
try {
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier());
PostMethod pm = new PostMethod(url);
NameValuePair[] params = new NameValuePair[4];
params[0] = new NameValuePair("cobSessionToken", paramNameCobSessionToken);
params[1] = new NameValuePair("userSessionToken", paramNameUserSessionToken);
params[2] = new NameValuePair("bridgetMetaData.bridgetKeyData.bridgetAppId", "10009959");
params[3] = new NameValuePair("bridgetMetaData.bridgetSource.fileName", "bundle.zip");
pm.setQueryString(params);
String sourceFile = "D:\\bundle\\bundle\\bundle.zip";
File f = new File(sourceFile);
Part[] parts = {new FilePart("bridgetMetaData.bridgetSource.bundledZipFile", f)};
pm.setRequestEntity(new MultipartRequestEntity(parts, pm.getParams()));
HttpClient hc = new HttpClient();
int RC = hc.executeMethod(pm);
System.out.println("Response Status Code : " + RC);
response = pm.getResponseBodyAsString();
System.out.println("The response is " + response);
} catch (Exception e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
return response;
}
static class NullHostnameVerifier implements HostnameVerifier
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}
}
public static void main(String[] args){
File2 upload=new File2();
upload.verifyAndUpdateBridgetApp(paramNameCobSessionToken, paramNameUserSessionToken);
}
}
错误日志
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'File2$_verifyAndUpdateBridgetApp_closure1@189f854' with class
'File2$_verifyAndUpdateBridgetApp_closure1' to class 'org.apache.commons.httpclient.methods.multipart.Part'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:371)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.asArray(DefaultTypeTransformation.java:445)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:204)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)
at File2.verifyAndUpdateBridgetApp(File2.groovy:48)
at File2$verifyAndUpdateBridgetApp.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at File2.main(File2.groovy:75)
答案 0 :(得分:1)
在定义[]
{}
而不是Part[]
Part[] parts = [new FilePart("bridgetMetaData.bridgetSource.bundledZipFile", f)]