我使用apache commons将图像上传到Web托管服务器。
我使用AsyncTask并且它似乎卡在storeFile(),我的copyStreamListener似乎太早完成但后来什么也没做?该方法的其余部分未执行,上传的文件仅部分上传到我们的服务器上(大约30%的图像可见)。 FTP回复代码都可以,但它不会关闭连接或完成asynctask
BUT
我已经在8台设备上测试了它,它只影响其中的3台,而不是所有的时间!有问题的设备都在Android 7.0(Nougat)上运行,也许它与它有关?
我不认为代码存在问题(因为它在大多数设备上都很好)但是这里是如何上传
FTPClient ftp = new FTPClient();
try {
ftp.connect(InetAddress.getByName("my.host"));
if (ftp.login("user", "pass")) {
Log.i("Server", " --- FTP Logged In --- ");
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
String directory = "/user" + String.valueOf(userID);
ftp.makeDirectory(directory);
ftp.changeWorkingDirectory(directory);
for (int i = 0; i < imageFilePaths.size(); i++) {
uploadFile(imageFilePaths.get(i), ftp, directory);
}
ftp.logout();
ftp.disconnect();
Log.i("Server", " --- Logged out --- ");
}
} catch (IOException e) {
e.printStackTrace();
}
private void uploadFile(ImageFiles imageFilePaths, FTPClient ftp, String directory) {
try {
final File finalFile = new File(imageFilePaths.fullPath);
final long fileSize = finalFile.length();
final String filename = String.valueOf(System.currentTimeMillis());
BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(finalFile));
streamListener = new CopyStreamAdapter() {
@Override
public void bytesTransferred(long totalBytesTransferred,
int bytesTransferred, long streamSize) {
totalFileSizeTrasnfered = totalFileSizeTrasnfered + bytesTransferred;
updateProgress();
if (totalBytesTransferred == fileSize) {
removeCopyStreamListener(streamListener);
}
}
};
ftp.setCopyStreamListener(streamListener);
// excecution seems to stop here
boolean result = ftp.storeFile(filename + imageFilePaths.fileExt, buffIn);
Log.i("Server", " --- File uploaded --- " + result);
buffIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
登录后在logcat中显示的所有内容都是......
I/Server: --- FTP Logged In ---
I/art: Background partial concurrent mark sweep GC freed 2256(150KB) AllocSpace objects, 3(35MB) LOS objects, 13% free, 104MB/120MB, paused 2.157ms total 118.293ms
D/libc: [NET] android_getaddrinfofornetcontext+,hn 29(0x70616765616432),sn(),hints(known),family 0,flags 1024, proc=com.proj.myproj
D/libc: [NET] android_getaddrinfo_proxy get netid:0
D/libc: [NET] android_getaddrinfo_proxy-, success
V/FA: Inactivity, disconnecting from the service
否&#34;退出&#34;或&#34;文件已上传&#34;并且应用程序继续运行。
任何人都可以给我任何有关如何解决或指向正确方向的见解吗?谢谢