通过 post 请求 android studio 发送 .dex 文件
Send .dex file via post request android studio
出于我自己的目的,我想将其他应用程序的dex文件发送到远程服务器并获得答案。
我使用了在这里找到的答案。一开始我尝试创建一个简单的示例,只是连接到服务器并上传 dex 文件。到目前为止,我还没有设法从其他应用程序中提取 dex,所以我想到了使用我已有的 dex 文件。
正如我所读到的,不应该将常见文件存储到“/res/raw”或"assets"
我尝试了很多方法将其作为 File 加载,但其中 none 有效。我在所有情况下使用的路径都是在右键单击文件 -> 复制参考中找到的。
在 /raw 和
下创建一个 res 文件夹
File f = new File("res/raw/filename.dex");
在 /app 下新建一个 assets 文件夹
File f = new File("filename.dex");
在 /main 下创建资产文件夹
File f = new File("main/assets/filename.dex");
等等。
我设法做到这一点的唯一方法是使用 InputStream
Inputstream in = getResources().openRawResources(R.raw.filename_without_dex)
但我无法将它转换为文件,所以我放弃了这个 solution.I 希望将其作为 File 导致以下 POST 请求必须是 multipart/form。
在java中,"load"文件的方式很简单。为什么不在 android 中?
一个真正丑陋、快速和肮脏的解决方案。
没有错误检查或清理。
但它有效。
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;
}
}
出于我自己的目的,我想将其他应用程序的dex文件发送到远程服务器并获得答案。
我使用了在这里找到的答案。一开始我尝试创建一个简单的示例,只是连接到服务器并上传 dex 文件。到目前为止,我还没有设法从其他应用程序中提取 dex,所以我想到了使用我已有的 dex 文件。 正如我所读到的,不应该将常见文件存储到“/res/raw”或"assets" 我尝试了很多方法将其作为 File 加载,但其中 none 有效。我在所有情况下使用的路径都是在右键单击文件 -> 复制参考中找到的。
在 /raw 和
下创建一个 res 文件夹File f = new File("res/raw/filename.dex");
在 /app 下新建一个 assets 文件夹
File f = new File("filename.dex");
在 /main 下创建资产文件夹
File f = new File("main/assets/filename.dex");
等等。
我设法做到这一点的唯一方法是使用 InputStream
Inputstream in = getResources().openRawResources(R.raw.filename_without_dex)
但我无法将它转换为文件,所以我放弃了这个 solution.I 希望将其作为 File 导致以下 POST 请求必须是 multipart/form。
在java中,"load"文件的方式很简单。为什么不在 android 中?
一个真正丑陋、快速和肮脏的解决方案。
没有错误检查或清理。
但它有效。
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;
}
}