gpt4 book ai didi

java - 改进通过套接字从桌面到 Android 服务的数据传输

转载 作者:行者123 更新时间:2023-12-02 04:59:01 25 4
gpt4 key购买 nike

我对套接字编程有点陌生,我在网上搜索了很多,这让我更加困惑。所以我想将数据从我用 Java 编写的桌面应用程序传输到我的 Android 设备。逻辑如下:我从我的电脑中的应用程序启动一个服务器套接字,我从 android 连接到该服务器。在android中,它在服务中连接并创建套接字,然后在服务中,有一个等待接收数据的while循环。当它收到数据时,它会显示进度通知并获取数据。

但是这里有问题:

  1. 有时在我的桌面应用程序中,它会转到finally子句并显示传输完成消息。但我的 Android 设备中的数据传输尚未结束,然后突然停止传输。当文件很大时,这种情况会发生得更多。但对于小文件就可以[在@greenapps的帮助下修复]

  2. 当它开始接收文件时,Android 操作系统变得非常慢,我无法用手机做任何事情,并且 LMK 开始一次又一次地终止进程。甚至发射器也被杀死。我使用了一个线程为什么会发生这种情况? [已修复] 我在每个循环中都过于频繁地调用 updateProgressNotif() 。我更改了代码,如果进度超过 5%,则更新进度条。如果其他人想使用代码,请为其他人编辑代码

  3. 数据传输速度慢。传输 25MB 数据大约需要 15-20 秒。我认为它应该更快?有什么办法可以提高速度吗?

这是代码:

代码编辑1:我使用了@greenapps的建议并优化了我的代码。现在发送文件完全正常,发送前后的文件长度在桌面应用程序上完全相同,在 Android 应用程序上接收前后的文件大小相同。

代码编辑2:我删除了BufferedReaders,因为它们没用。我已经读取 64*1024 字节,因此不需要 BufferdReaders。另外,我使用了 @greenapps 的建议,并使用 readLine() 进行“name:size”编码。

代码编辑 3: 增加了用户选择多个文件的功能,现在代码知道应从输入流读取多少内容,并且不会读取超过文件大小的内容。

桌面应用程序文件传输代码:首先,它发送文件的名称和长度,然后使用 Thread.Sleep 暂停,然后发送文件。例如,它首先发送:myfile.txt:3456。因为我需要知道文件的名称和长度才能显示我在通知中收到的文件的名称。还有在进度条中使用它的长度

public class SendFile extends Thread {
private Socket client = null;
private Main mainFrame;
private File[] files;
FileInputStream fis = null;

public SendFile(Socket client, Main main, File[] files){
this.mainFrame = main;
this.client = client;
this.files = files;
}

public void run(){
try {
mainFrame.btnChooseFile.setEnabled(false);
for(int i=0; i<files.length; i++){
File file = files[i];
String fileInfo = file.getName() + ":" + file.length() + "\n";
client.getOutputStream().write(fileInfo.getBytes("UTF-8"), 0, fileInfo.getBytes("UTF-8").length);
client.getOutputStream().flush();
Thread.sleep(200);
fis = new FileInputStream(file);
byte[] fileBuffer = new byte[64*1024];
int bytesRead;
int current = 0;
while ((bytesRead = fis.read(fileBuffer)) != -1) {
current += bytesRead;
client.getOutputStream().write(fileBuffer, 0, bytesRead);
}
client.getOutputStream().flush();
fis.close();
Thread.sleep(300);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}finally{
mainFrame.log("File Sent Successfully");
mainFrame.btnChooseFile.setEnabled(true);
}

} }

Android应用程序文件接收代码:该线程在服务中运行。所以当我从桌面发送文件时。即使应用程序未打开但 Android 变得如此缓慢,它也会自动显示通知并开始接收文件

private class WaitForFile extends Thread{
@Override
public void run() {
FileOutputStream fos;
BufferedReader in;
String name;
int fileSize;
try {
while(true) {
String fileInfo;
in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
fileInfo = in.readLine();
name = fileInfo.split(":")[0];
fileSize = Integer.parseInt(fileInfo.split(":")[1]);
final String fileName = name;
final int size = fileSize;
handler.post(new Runnable() {
@Override
public void run() {
createGetNotification(fileName);
}
});
String PATH = Environment.getExternalStorageDirectory() + "/mydownloads/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
fos = new FileOutputStream(outputFile);
InputStream is = con.getInputStream();
byte[] fileBuffer = new byte[64*1024];
int bytesRead;
int current = 0;
float prevIncr = 0;
int bytesToRead = Math.min(fileBuffer.length, size);
while ((bytesRead = is.read(fileBuffer, 0, bytesToRead)) != -1) {
current += bytesRead;
fos.write(fileBuffer, 0, bytesRead);
bytesToRead = Math.min(fileBuffer.length, size-current);
float incr = ((float)current/(float)size)*100;
if(incr - prevIncr > 5) {
final float increment = incr;
prevIncr = increment;
handler.post(new Runnable() {
@Override
public void run() {
updateProgressNotif(increment);
}
});
}
if(current == size)
break;
}
if(size == current) {
fos.flush();
}else{
outputFile = null;
}
final File receivedFile = outputFile;
handler.post(new Runnable() {
@Override
public void run() {
doneGetNotification(receivedFile);
}
});

fos.close();

}
} catch (Exception e) {
e.printStackTrace();
handler.post(new Runnable() {
@Override
public void run() {
stopForeground(true);
disconnectedNotif();
stopSelf();
}
});
}
}

}

如有任何帮助,我们将不胜感激

最佳答案

答案就在评论里。

传输文件名和大小以及使用 sleep() 是不好的。您现在发送例如 myfile.txt:3456。但您最好发送像 myfile.txt:3456\n 这样的行并删除 sleep()。然后在客户端中使用 readLine() 来读取该行。使用 getFileSize() 进行分割也可以做得更好。您可以完全取消该函数,只使用 Integer.valueOf()。

关于java - 改进通过套接字从桌面到 Android 服务的数据传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com