2011-01-29 83 views
1

我的代碼,使用https錯誤上傳文件?

Properties systemProps = System.getProperties(); 
    systemProps.put("javax.net.ssl.trustStore",  
    System.getProperty("catalina.home")+fs+".keystore"); 
    System.setProperties(systemProps); 

    try { 
    // Open a secure connection. 
    URL url = new URL("https://192.168.6.45:8181/erp_adapter/UploadFile"); 
    String requestParams = "uid=sdfn&password=rsdftesan&active=y&type=F"; 
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 

    // Set up the connection properties 
    con.setRequestProperty("Connection", "close"); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setUseCaches(false); 
    con.setConnectTimeout(30000); 
    con.setReadTimeout(30000); 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    con.setRequestProperty("Content-Length", Integer.toString(requestParams.length())); 

    // Set up the user authentication portion of the handshake with the private 
    // key provided by NAIMES Tech Support. 
    // Based on an example posted by Torsten Curdt on his blog: 
    //  http://vafer.org/blog/20061010073725 (as of Nov, 2009) 
    File pKeyFile = new File(System.getProperty("catalina.home")+fs+".keystore"); 
    String pKeyPassword = "UB#20abba"; 
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 
    KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
    InputStream keyInput = new FileInputStream(pKeyFile); 
    //byte[] Password=pKeyPassword.getBytes(); 
    keyStore.load(keyInput, pKeyPassword.toCharArray()); 
    keyInput.close(); 

這顯示了一個錯誤,

產生java.io.IOException:DerInputStream.getLength():lengthTag = 109,太大了。 (未知源) at sun.security.util.DerValue。 (未知源) at com.gofrugal.raymedi.erp.util.AidapClient.main(AidapClient.java:58)

任何人都可以幫我解決問題嗎?

+0

您發佈了兩個彼此不相關的代碼段。代碼的任何部分都沒有上傳位。你能否更具體地說明錯誤的來源。從當前堆棧跟蹤中,它與https或文件上傳無關,正如您在問題中所述。 – gigadot 2011-01-29 11:06:40

回答

4

您嘗試加載的密鑰庫可能不是Sun PKCS12密鑰庫的實例。發出以下命令找出密鑰庫的類型......

keytool -list -keystore <keystore_location> 

你會發現輸出看起來像......

Keystore type: JKS 
Keystore provider: SUN 

Your keystore contains 76 entries 

... 

在這種情況下,密鑰庫是一個JKS密鑰庫(我猜你也是),你會想要做

KeyStore.getInstance("JKS"); 

而不是你有什麼。

+0

這也適用於使用jarsigner時,請確保不要將storetype指定爲pkcs12 – 2014-01-31 22:28:05