java - 使用https错误上传文件?

标签 java https ioexception

我的代码是,

 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, too big. at sun.security.util.DerInputStream.getLength(Unknown Source) at sun.security.util.DerValue.init(Unknown Source) at sun.security.util.DerValue.(Unknown Source) at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(Unknown Source) at java.security.KeyStore.load(Unknown Source) at com.gofrugal.raymedi.erp.util.AidapClient.main(AidapClient.java:58)

谁能帮我解决问题是什么?

最佳答案

您尝试加载的 keystore 可能不是 Sun PKCS12 keystore 的实例。发出以下命令以找出 keystore 的类型...

keytool -list -keystore <keystore_location>

你会发现输出看起来像...

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 76 entries

...

在这种情况下, keystore 是一个 JKS keystore (我猜你的也是),你会想要这样做

KeyStore.getInstance("JKS");

而不是你拥有的。

关于java - 使用https错误上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4836180/

相关文章:

java - 在 CDI 中指定不同的子类实现

java - OpenCV人脸检测找不到任何

java - Spring Security 拦截器 URL 不起作用

security - 当IE提示 “Only secure content is displayed”时,我能知道不安全内容的网址吗?

python - 用 suds 为 SOAP 编写 python 客户端

java.io.IOException : More data recieved than is allowed by the channel data window

java - 您可以像 Java 应用程序一样部署用 smalltalk/squeak/pharo 编写的应用程序吗?

java - 如何使用 HttpComponents (Java apache lib) 发出请求,更改 TLS 握手中的服务器名称指示

java - 为什么 InputStream.close() 声明抛出 IOException?

java.io.FileNotFoundException(权限被拒绝)尽管 chmod 777