Skip to content

Commit

Permalink
1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiansen574 committed Apr 25, 2024
1 parent dd86d69 commit 79a3d44
Show file tree
Hide file tree
Showing 35 changed files with 230 additions and 93 deletions.
4 changes: 2 additions & 2 deletions HybridFileXfer-Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "top.weixiansen574.hybridfilexfer"
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"
versionCode 11
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Binary file modified HybridFileXfer-Android/app/debug/app-debug.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion HybridFileXfer-Android/app/debug/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0_1711385041641",
"versionName": "1.0",
"outputFile": "app-debug.apk"
}
],
Expand Down
Binary file modified HybridFileXfer-Android/app/release/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions HybridFileXfer-Android/app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"versionCode": 11,
"versionName": "1.1",
"outputFile": "app-release.apk"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public ArrayList<ParcelableRemoteFile> listClientFiles(String path) throws Remot
try {
ArrayList<RemoteFile> remoteFiles = fileTransferServer.listClientFiles(path);
ArrayList<ParcelableRemoteFile> parcelableRemoteFiles = new ArrayList<>();
System.out.println("获取到电脑端文件,size:"+remoteFiles.size());
for (RemoteFile remoteFile : remoteFiles) {
System.out.println(remoteFile.getName());
parcelableRemoteFiles.add(new ParcelableRemoteFile(remoteFile));
}
return parcelableRemoteFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,34 @@ private void waitPCConnect() throws IOException {
wifiReceiveThread.setOnExceptionListener(this);
wifiReceiveThread.start();





}

public ArrayList<RemoteFile> listClientFiles(String path) throws IOException {
dos.writeShort(ControllerIdentifiers.LIST_FILES);
dos.writeUTF(path);
//这里不用json了,导入json库会让服务端jar膨胀250kb,直接用字节流传输
int listSize = dis.readInt();
ArrayList<RemoteFile> remoteFiles = new ArrayList<>(listSize);
//| name | path | lastModified | size | isDirectory |
//| ---------- | ---------- | ------------ | ------- | ----------- |
//| String:UTF | String:UTF | long:8b | long:8b | boolean |
for (int i = 0; i < listSize; i++) {
RemoteFile remoteFile = new RemoteFile(
dis.readUTF(),//name
dis.readUTF(),//path
dis.readLong(),//lastModified
dis.readLong(),//size
dis.readBoolean()//isDirectory
);
remoteFiles.add(remoteFile);
}
return remoteFiles;

/*
因为安卓release版会删减无用代码,导致RemoteFile序列化ID不一致
objectInputStream.readObject方法将出现异常,导致电脑端目录显示是空白
再就是服务端不支持夸语言,仅限于java
已弃用以下代码
int contentLength = dis.readInt();
byte[] content = new byte[contentLength];
Expand All @@ -139,21 +158,26 @@ public ArrayList<RemoteFile> listClientFiles(String path) throws IOException {
throw new IOException(e);
} finally {
byteArrayInputStream.close();
}
}*/
}

public void transferToMe(List<String> files, String remoteDir, String localDir) throws IOException {
dos.writeShort(ControllerIdentifiers.TRANSPORT_FILES);//标识,传输文件到服务端
dos.writeUTF(localDir);//服务端路径(手机)
dos.writeUTF(remoteDir);//客户端路径(电脑)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
dos.writeInt(files.size());//文件数量
for (String file : files) {
dos.writeUTF(file);//文件路径
}
//ObjectInputStream可能会出现序列化不一致相关问题,已弃用
/*ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(files);
objectOutputStream.flush();
byte[] bytes = outputStream.toByteArray();
outputStream.close();
dos.writeInt(bytes.length);//content length
dos.write(bytes);//要传输到服务端的文件列表
dos.write(bytes);//要传输到服务端的文件列表*/
}

public void transferToClient(List<File> files, File localDir, String remoteDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public void run() {
InetAddress serverWifiAddress;
try {
serverWifiAddress = getServerWifiAddress();
} catch (IOException e){
} catch (IOException e) {
System.out.println("连接手机失败,因为服务端未运行!请在手机上启动服务器并等待连接!");
return;
}
if (Arrays.equals(serverWifiAddress.getAddress(),new byte[4])){
if (Arrays.equals(serverWifiAddress.getAddress(), new byte[4])) {
System.out.println("连接手机失败,因为手机没有连接WIFI!");
return;
}
Expand All @@ -65,24 +65,24 @@ public void run() {
Socket usbSocket = new Socket(InetAddress.getLoopbackAddress(), ServerInfo.PORT_USB);//USB-ADB forward的端口是本地127.0.0.1

//接收线程只管接收
usbReceiveThread = new ReceiveThread(fileTransferEvents,ReceiveThread.DEVICE_USB,usbSocket.getInputStream());
usbReceiveThread = new ReceiveThread(fileTransferEvents, ReceiveThread.DEVICE_USB, usbSocket.getInputStream());
usbReceiveThread.setName("usbReceive");
usbReceiveThread.setOnExceptionListener(this);
usbReceiveThread.start();
wifiReceiveThread = new ReceiveThread(fileTransferEvents,ReceiveThread.DEVICE_WIFI,wifiSocket.getInputStream());
wifiReceiveThread = new ReceiveThread(fileTransferEvents, ReceiveThread.DEVICE_WIFI, wifiSocket.getInputStream());
wifiReceiveThread.setName("wifiReceive");
wifiReceiveThread.setOnExceptionListener(this);
wifiReceiveThread.start();

usbSendThread = new SendThread(fileTransferEvents,SendThread.DEVICE_USB,jobPublisher,usbSocket.getOutputStream());
usbSendThread = new SendThread(fileTransferEvents, SendThread.DEVICE_USB, jobPublisher, usbSocket.getOutputStream());
usbSendThread.setName("usbSend");
usbSendThread.setOnExceptionListener(this);
usbSendThread.start();
wifiSendThread = new SendThread(fileTransferEvents,SendThread.DEVICE_WIFI,jobPublisher,wifiSocket.getOutputStream());
wifiSendThread = new SendThread(fileTransferEvents, SendThread.DEVICE_WIFI, jobPublisher, wifiSocket.getOutputStream());
wifiSendThread.setName("wifiSend");
wifiSendThread.setOnExceptionListener(this);
wifiSendThread.start();
System.out.println("已连接至手机!WLAN IP:"+serverWifiAddress.getHostAddress());
System.out.println("已连接至手机!WLAN IP:" + serverWifiAddress.getHostAddress());
waitingForRequest();

} catch (IOException | ClassNotFoundException e) {
Expand All @@ -101,25 +101,32 @@ private InetAddress getServerWifiAddress() throws IOException {
}

private void waitingForRequest() throws IOException, ClassNotFoundException {
w:while (true) {
w:
while (true) {
short identifiers = dis.readShort();
switch (identifiers) {
case ControllerIdentifiers.LIST_FILES:
String path = dis.readUTF();
System.out.println("获取文件列表 "+path);
System.out.println("获取文件列表 " + path);
handleListFiles(path);
break;
case ControllerIdentifiers.TRANSPORT_FILES:
String serverDir = dis.readUTF();//服务器(手机)路径
String dir = dis.readUTF();//客户端(电脑)的路径
int contentLength = dis.readInt();//内容长度(文件列表)
int listSize = dis.readInt();//文件路径列表大小
List<String> toTransferFilePaths = new ArrayList<>(listSize);
for (int i = 0; i < listSize; i++) {
toTransferFilePaths.add(dis.readUTF());
}
//ObjectInputStream可能会出现序列化不一致相关问题,已弃用
/*int contentLength = dis.readInt();//内容长度(文件列表)
byte[] bytes = new byte[contentLength];
dis.readFully(bytes);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
List<String> remoteFiles = (List<String>) objectInputStream.readObject();
inputStream.close();
handleTransferFileToServer(serverDir,dir,remoteFiles);
inputStream.close();*/
handleTransferFileToServer(serverDir, dir, toTransferFilePaths);
break;
case ControllerIdentifiers.SHUTDOWN:
System.out.println("收到关闭指令,准备关闭连接……");
Expand All @@ -140,38 +147,53 @@ private void waitingForRequest() throws IOException, ClassNotFoundException {
}

private void handleListFiles(String path) throws IOException {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
ArrayList<RemoteFile> files = new ArrayList<>();
if (!path.equals("/")) {
File file = new File(path);
File[] list = file.listFiles();
if (list != null) {
for (File file1 : list) {
files.add(new RemoteFile(file1));
}
ArrayList<RemoteFile> files = new ArrayList<>();
if (!path.equals("/")) {
File file = new File(path);
File[] list = file.listFiles();
if (list != null) {
for (File file1 : list) {
files.add(new RemoteFile(file1));
}
}
} else {
File[] roots = File.listRoots();
//判断是否是Linux的目录结构,Windows的根目录是C:\\,而不是所有盘符,Linux的根目录是“/”没有盘符概念
if (roots.length == 1 && roots[0].getAbsolutePath().equals("/")){
roots = roots[0].listFiles();
if (roots == null){
throw new RuntimeException("无法获取根目录下的文件列表,请检查运行时权限");
}
for (File root : roots) {
files.add(new RemoteFile(root.getName(), root.getPath(), root.lastModified(), root.length(), root.isDirectory()));
}
} else {
File[] roots = File.listRoots();
for (File root : roots) {
files.add(new RemoteFile(root.getPath(),root.getPath(),root.lastModified(),root.length(),root.isDirectory()));
files.add(new RemoteFile(root.getPath(), root.getPath(), root.lastModified(), root.length(), root.isDirectory()));
}
}
objectOutputStream.writeObject(files);
objectOutputStream.flush();
byte[] bytes = byteArrayOutputStream.toByteArray();
dos.writeInt(bytes.length);
dos.write(bytes);
}
//已弃用ObjectOutputStream
//| name | path | lastModified | size | isDirectory |
//| ---------- | ---------- | ------------ | ------- | ----------- |
//| String:UTF | String:UTF | long:8b | long:8b | boolean |
dos.writeInt(files.size());//listSize
for (RemoteFile file : files) {
dos.writeUTF(file.getName());
dos.writeUTF(file.getPath());
dos.writeLong(file.getLastModified());
dos.writeLong(file.getSize());
dos.writeBoolean(file.isDirectory());
}
}

private void handleTransferFileToServer(String serverDir,String dir,List<String> remoteFiles){
private void handleTransferFileToServer(String serverDir, String dir, List<String> remoteFiles) {
File localDir = new File(dir);
List<File> transferFiles = new ArrayList<>();
for (String remoteFile : remoteFiles) {
transferFiles.add(new File(remoteFile));
}
jobPublisher.addJob(new TransferJob(localDir,serverDir,transferFiles));
jobPublisher.addJob(new TransferJob(localDir, serverDir, transferFiles));
}


Expand Down
2 changes: 1 addition & 1 deletion HybridFileXfer-PC/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
25 changes: 17 additions & 8 deletions HybridFileXfer-PC/src/top/weixiansen574/hybridfilexfer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

public class Main {
public static void main(String[] args) {
if (executeAdbForwardCommand(5740)){
if (executeAdbForwardCommand(5740,args)){
System.out.println("USB : 5740 端口转发成功!");
} else {
System.out.println("USB : 5740 端口转发失败,请检查是否开启USB调试和授权此电脑!");
return;
}
if (executeAdbForwardCommand(5741)) {
if (executeAdbForwardCommand(5741,args)) {
System.out.println("USB : 5741 端口转发成功!");
} else {
System.out.println("USB : 5741 端口转发失败,请检查是否开启USB调试和授权此电脑!");
Expand All @@ -23,27 +23,36 @@ public static void main(String[] args) {
new FileTransferClient().startUp();
}

public static boolean executeAdbForwardCommand(int port) {
public static boolean executeAdbForwardCommand(int port,String[] args) {
try {
// 获取当前jar包所在的目录
String jarDirectory = System.getProperty("user.dir");
StringBuilder adbCommand = new StringBuilder();
adbCommand.append("./adb");
for (String arg : args) {
adbCommand.append(" ").append(arg);
}
adbCommand.append(" forward tcp:");
adbCommand.append(port);
adbCommand.append(" tcp:");
adbCommand.append(port);

// 构建adb forward命令
String adbForwardCommand = "adb.exe forward tcp:" + port + " tcp:" + port;
String adbForwardCommand = adbCommand.toString();

// 执行adb forward命令
Process process = Runtime.getRuntime().exec(adbForwardCommand, null, new java.io.File(jarDirectory));

BufferedReader bufferedReader = process.errorReader();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//BufferedReader errorReader = process.errorReader();
String l;
while ((l = bufferedReader.readLine()) != null){
while ((l = errorReader.readLine()) != null){
System.err.println(l);
}
// 读取adb forward命令执行的输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("adb.exe:"+line);
System.out.println("adb:"+line);
}

// 等待命令执行完成
Expand Down
Loading

0 comments on commit 79a3d44

Please sign in to comment.