1

httpを使用してこれを成功させました。次に、1)サーバーとコードに必要な変更を加えました。2) 必要な証明書を生成し、Android の raw フォルダーに追加しました。(bks 形式) 3) 2 日経っても、多くの変更を加えてもエラーが発生します。サーバー上の certis の場所が間違っていると思います。servertruststore.jks と server.jks が必要な正確な場所を教えてください。

私はcertisのためにこのリンクをたどりました(かなり素晴らしいです!)

私のコード:

public int uploadFile(String sourceFileUri, Context context) {
          String upLoadServerUri = "https://10.112.72.217/upload_test/upload_media_test.php";
          String fileName = sourceFileUri;

          HttpsURLConnection conn = null;
          DataOutputStream dos = null;  
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024; 
          File sourceFile = new File(sourceFileUri); 
          if (!sourceFile.isFile()) {
           Log.e("uploadFile", "Source File Does not exist");
           return 0;
          }


              try {

                  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                InputStream clientTruststoreIs = context.getResources().openRawResource(R.raw.clienttruststore);
                  KeyStore truststore = null;
                  truststore = KeyStore.getInstance("BKS");
                  truststore.load(clientTruststoreIs, "meghna".toCharArray());


                  trustManagerFactory.init(truststore);
                  System.out.println("Loaded server certificates: " + truststore.size());


//                  //final KeyStore keystore = this.loadStore(context.getResources().openRawResource(R.raw.client, "meghna", "BKS");
//                  KeyStore keystore = null;
//                  try {
//                      InputStream clientTruststoreIs2 = context.getResources().openRawResource(R.raw.client);
//                      
//                      keystore = KeyStore.getInstance("BKS");
//                      keystore.load(clientTruststoreIs2, "meghna".toCharArray());
//                      System.out.println("Loaded server certificates: " + keystore.size());
//                  } catch (KeyStoreException e) {
//                      // TODO Auto-generated catch block
//                      e.printStackTrace();
//                  }

                  // open a URL connection to the Servlet

//                  TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
//                  tmf.init(keystore);


                  SSLContext context2 = SSLContext.getInstance("TLS");
                  context2.init(null, trustManagerFactory.getTrustManagers(), null);

                  URL url = new URL(upLoadServerUri);
                  conn = (HttpsURLConnection) url.openConnection();
                  conn.setSSLSocketFactory(context2.getSocketFactory());
                  InputStream in = conn.getInputStream();




                  FileInputStream fileInputStream = new FileInputStream(sourceFile);




               conn.setDoInput(true); // Allow Inputs
               conn.setDoOutput(true); // Allow Outputs
               conn.setUseCaches(false); // Don't use a Cached Copy
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
               conn.setRequestProperty("uploaded_file", fileName); 
               dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd); 
               dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
               dos.writeBytes(lineEnd);

               bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size

               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               buffer = new byte[bufferSize];

               // read file and write it into form...
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

               while (bytesRead > 0) {
                 dos.write(buffer, 0, bufferSize);
                 bytesAvailable = fileInputStream.available();
                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);               
                }

               // send multipart form data necesssary after file data...
               dos.writeBytes(lineEnd);
               dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

               // Responses from the server (code and message)
               serverResponseCode = conn.getResponseCode();
               String serverResponseMessage = conn.getResponseMessage();

               Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
               if(serverResponseCode == 200){
                   runOnUiThread(new Runnable() {
                        public void run() {
                            tv.setText("File Upload Completed.");
                            Toast.makeText(Mainactivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                        }
                    });                
               }    

               //close the streams //
               fileInputStream.close();
               dos.flush();
               dos.close();

          } catch (MalformedURLException ex) {  
              dialog.dismiss();  
              ex.printStackTrace();
              Toast.makeText(Mainactivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
              Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
          } catch (Exception e) {
              dialog.dismiss();  
              e.printStackTrace();
              //Toast.makeText(Mainactivity.this, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
              Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);  
          }
          dialog.dismiss();       
          return serverResponseCode;  
         } }

エラーログは次のとおりです。

04-08 23:49:59.843: I/Adreno200-EGL(7728): <qeglDrvAPI_eglInitialize:290>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_ICS_STRAWBERRY_RB2.04.00.04.22.006_msm7627a_ICS_STRAWBERRY_RB2.2_CL2527005_release_AU (CL2527005)
04-08 23:49:59.843: I/Adreno200-EGL(7728): Build Date: 09/12/12 Wed
04-08 23:49:59.843: I/Adreno200-EGL(7728): Local Branch: 
04-08 23:49:59.843: I/Adreno200-EGL(7728): Remote Branch: m/ics_strawberry_rb2.2
04-08 23:49:59.843: I/Adreno200-EGL(7728): Local Patches: NONE
04-08 23:49:59.843: I/Adreno200-EGL(7728): Reconstruct Branch: NOTHING
04-08 23:50:02.093: I/System.out(7728): Loaded server certificates: 1
04-08 23:50:02.233: W/System.err(7728): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.233: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:413)
04-08 23:50:02.243: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
04-08 23:50:02.243: W/System.err(7728):     at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:210)
04-08 23:50:02.243: W/System.err(7728):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:477)
04-08 23:50:02.253: W/System.err(7728):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
04-08 23:50:02.263: W/System.err(7728):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-08 23:50:02.263: W/System.err(7728):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-08 23:50:02.263: W/System.err(7728):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-08 23:50:02.263: W/System.err(7728):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-08 23:50:02.263: W/System.err(7728):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
04-08 23:50:02.273: W/System.err(7728):     at com.example.uploadimagedemo.Mainactivity.uploadFile(Mainactivity.java:145)
04-08 23:50:02.273: W/System.err(7728):     at com.example.uploadimagedemo.Mainactivity$1$1.run(Mainactivity.java:78)
04-08 23:50:02.273: W/System.err(7728):     at java.lang.Thread.run(Thread.java:864)
04-08 23:50:02.273: W/System.err(7728): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.283: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:184)
04-08 23:50:02.283: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163)
04-08 23:50:02.283: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:593)
04-08 23:50:02.283: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
04-08 23:50:02.283: W/System.err(7728):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
04-08 23:50:02.283: W/System.err(7728):     ... 12 more
04-08 23:50:02.293: W/System.err(7728): Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.293: W/System.err(7728):     ... 17 more
04-08 23:50:02.313: E/Upload file to server Exception(7728): Exception : java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.313: E/Upload file to server Exception(7728): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:413)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:210)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:477)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at com.example.uploadimagedemo.Mainactivity.uploadFile(Mainactivity.java:145)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at com.example.uploadimagedemo.Mainactivity$1$1.run(Mainactivity.java:78)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at java.lang.Thread.run(Thread.java:864)
04-08 23:50:02.313: E/Upload file to server Exception(7728): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:184)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:593)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
04-08 23:50:02.313: E/Upload file to server Exception(7728):    ... 12 more
04-08 23:50:02.313: E/Upload file to server Exception(7728): Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
04-08 23:50:02.313: E/Upload file to server Exception(7728):    ... 17 more
04-08 23:50:02.313: I/System.out(7728): RES : 0
4

0 に答える 0