2016-11-17 138 views
0

爲了我自己的目的,我想將其他應用程序的dex文件發送到遠程服務器並獲得答案。通過發佈請求發送.dex文件android studio

我已經使用了我在這裏找到的答案。我試着首先創建一個簡單的例子,只是連接到服務器並上傳dex文件。到目前爲止,我還沒有設法從其他應用程序中提取dex,所以我想使用我已有的dex文件。 正如我讀過的,不應該將常見文件存儲到「/ res/raw」或「assets」 我嘗試了許多方法將其作爲文件加載,但它們都沒有工作。我在所有情況下使用的路徑都是右鍵單擊文件 - >複製參考。

  1. 創建res文件夾下的/生

    File f = new File("res/raw/filename.dex"); 
    
  2. 下/應用

    File f = new File("filename.dex"); 
    
  3. 創建資源文件夾中創建一個新的資產文件夾下/主

    File f = new File("main/assets/filename.dex"); 
    

    等等。

我好不容易纔這樣做的唯一方法是使用的InputStream

Inputstream in = getResources().openRawResources(R.raw.filename_without_dex) 

,但我無法施展它的文件,所以我放棄了這個solution.I想把它作爲一個文件導致以下POST請求必須是multipart/form。

在java中,「加載」文件的方式很簡單。爲什麼不在android中?

+0

您不能打開它作爲一個文件,因爲它不是作爲文件系統中的文件存在。而你問題的最後部分是沒有意義的:「我想把它作爲一個文件因爲下面的POST請求必須是多部分/表單。」沒有理由通過讀入InputStream中的數據來做到這一點。 – JesusFreke

+0

@JesusFreke我明白你的觀點。我試過這種方式[鏈接](https://developer.android.com/reference/java/net/HttpURLConnection.html)它使用InputStream,我沒有設法得到它的工作。你能舉一個正確使用InputStream到getOutputStream()的例子嗎? –

+0

@JesusFreke我問的原因是,我發現很多使用'MultipartEntityBuilder'創建多部分/表單數據請求的例子,它需要'File',但沒有一個使用'HttpURLConnection'和'InputStream'創建多部分POST請求。類 –

回答

0

一個真正醜陋,快速和骯髒的解決方案。
沒有錯誤檢查或清理。
但它工作。

import android.content.pm.ApplicationInfo; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile; 
import java.util.zip.ZipInputStream; 

public class MainActivity extends ActionBarActivity { 
    final String TAG = "GetDex"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     getAppPaths(); // to list all packages 

     String s = "/data/app/SoftKeyboard/SoftKeyboard.apk"; // a sample package 
     byte[] b = getFile(s); 
     byte[] dex = unzipDex(b, s); 
     Log.d(TAG, String.format("DEX size is %d", dex.length)); 
    } 





List<String> getAppPaths() { 
    final PackageManager pm = getPackageManager(); 
    //get a list of installed apps. 
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); 
    List<String> paths = new ArrayList<>(); 

    for (ApplicationInfo packageInfo : packages) { 
     Log.d(TAG, "Installed package :" + packageInfo.packageName); 
     Log.d(TAG, "Apk file path:" + packageInfo.sourceDir); 
     paths.add(packageInfo.sourceDir); 
    } 

    return paths; 
} 


byte[] getFile(String filename) { 
    try { 
     RandomAccessFile f = new RandomAccessFile(filename, "r"); 
     byte[] b = new byte[(int)f.length()]; 
     f.readFully(b); 
     return b; 
    } catch(IOException exception) { 
     exception.printStackTrace(); 
    } 

    return null; 
} 

public byte[] unzipDex(byte[] bytes, String filename) { 
    try{ 
     ZipFile zipFile = new ZipFile(filename); 
     ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bytes)); 
     ZipEntry ze = zis.getNextEntry(); 

     while(ze!=null){ 
      String entryName = ze.getName(); 
      if(!entryName.equals("classes.dex")) { 
       ze = zis.getNextEntry(); 
       continue; 
      } 

      InputStream is = zipFile.getInputStream(ze); 

      ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 

      int nRead; 
      byte[] data = new byte[16384]; 

      while ((nRead = is.read(data, 0, data.length)) != -1) { 
       buffer.write(data, 0, nRead); 
      } 

      buffer.flush(); 

      return buffer.toByteArray(); 
     } 
     System.out.println("Done"); 

    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 

    return null; 
} 

}

+0

謝謝@羅伊。雖然不是最合適的解決方案,但它幫助我理解 –