2014-02-12 98 views
2

這是我通過HTTP發送文件到服務器的代碼, 但我試圖使用HTTPS傳輸,我嘗試了使用它正在工作的參數傳遞值,但我不知道如何使用文件流進行編碼, can有人能幫我解決這個我有SSL證書使用aspx將文件發佈到https服務器?

void Sending() { 
     String iFileName = "video.mp4"; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     String Tag = "fSnd"; 
     try { 
      Log.e(Tag, "Starting Http File Sending to URL"); 

      // Open a HTTP connection to the URL 



      HttpURLConnection conn = (HttpURLConnection) connectURL 
        .openConnection();   

      // Allow Inputs 
      conn.setDoInput(true); 

      // Allow Outputs 
      conn.setDoOutput(true); 

      // Don't use a cached copy. 
      conn.setUseCaches(false); 

      // Use a post method. 
      conn.setRequestMethod("POST"); 

      conn.setRequestProperty("Connection", "Keep-Alive"); 

      conn.setRequestProperty("Content-Type", 
        "multipart/form-data;boundary=" + boundary); 

      DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 

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

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

      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" 
        + iFileName + "\"" + lineEnd); 
      dos.writeBytes(lineEnd); 

      Log.e(Tag, "Headers are written"); 

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

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

      // read file and write it into form... 
      int 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); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // close streams 
      fileInputStream.close(); 

      dos.flush(); 

      Log.e(Tag, 
        "File Sent, Response: " 
          + String.valueOf(conn.getResponseCode())); 

      InputStream is = conn.getInputStream(); 

      // retrieve the response from server 
      int ch; 

      StringBuffer b = new StringBuffer(); 
      while ((ch = is.read()) != -1) { 
       b.append((char) ch); 
      } 
      String s = b.toString(); 
      Log.i("Response", s); 
      dos.close(); 
     } catch (MalformedURLException ex) { 
      Log.e(Tag, "URL error: " + ex.getMessage(), ex); 
     } 

     catch (IOException ioe) { 
      Log.e(Tag, "IO error: " + ioe.getMessage(), ioe); 
     } 
    } 

回答

0

花了一天張貼到https安全連接, 終於找到了一個簡單的方法,它只是將單個文件發佈到服務器,但您可以循環併發送儘可能多的文件ü也可以發送任何文件格式與此代碼做簡單的鄉親享受

public void btnclick(View v) { 

for (int i = 0; i < 10; i++) { 
    String pathToOurFile = "/mnt/sdcard/" + "1.png"; 
    String urlServer = "https://google/post.aspx"; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 

    try { 

     FileInputStream fileInputStream = new FileInputStream(new File(
       pathToOurFile)); 

     TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
      public X509Certificate[] getAcceptedIssuers() { 
       return new X509Certificate[0]; 
      } 

      public void checkClientTrusted(X509Certificate[] certs, 
        String authType) { 
      } 

      public void checkServerTrusted(X509Certificate[] certs, 
        String authType) { 
      } 
     } }; 

     // Ignore differences between given hostname and certificate 
     // hostname 
     HostnameVerifier hv = new HostnameVerifier() { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }; 

     // Install the all-trusting trust manager 
     try { 
      SSLContext sc = SSLContext.getInstance("SSL"); 
      sc.init(null, trustAllCerts, new SecureRandom()); 
      HttpsURLConnection.setDefaultSSLSocketFactory(sc 
        .getSocketFactory()); 
      HttpsURLConnection.setDefaultHostnameVerifier(hv); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "exception", 
        Toast.LENGTH_SHORT).show(); 
     } 

     URL url = new URL(urlServer); 
     connection = (HttpsURLConnection) url.openConnection(); 

     // Allow Inputs & Outputs 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("ENCTYPE", "multipart/form-data"); 
     connection.setRequestProperty("Content-Type", 
       "multipart/form-data;boundary=" + boundary); 
     connection.setRequestProperty("uploaded_file", "second"); 
     outputStream = new DataOutputStream(
       connection.getOutputStream()); 
     outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
     outputStream 
       .writeBytes("Content-Disposition: form-data; name=\"sample\";filename=\"" 
         + pathToOurFile + "\"" + lineEnd); 
     outputStream.writeBytes(lineEnd); 

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

     // Read file 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

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

     outputStream.writeBytes(lineEnd); 
     outputStream.writeBytes(twoHyphens + boundary + twoHyphens 
       + lineEnd); 

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

     Toast.makeText(getApplicationContext(), 
       serverResponseCode + "," + serverResponseMessage, 
       Toast.LENGTH_LONG).show(); 

     fileInputStream.close(); 
     outputStream.flush(); 
     outputStream.close(); 
    } catch (Exception ex) { 
     // Exception handling 
    } 
} 

}

相關問題