所有文件复制从服务器到Android设备 [英] Copy all files from server to Android Device
问题描述
我想所有文件从服务器复制到Android设备。 假设服务器,我的服务器IP是 http://192.168.98.23 服务器文件夹和名称是数据。在数据文件夹中包含多个文件。 我想所有文件从服务器数据以我的Android设备的SD卡复制。
i want to copy all files from server to Android device. Suppose on server, my server ip is http://192.168.98.23 and the name of the server folder is Data. The Data folder contains many files. I want to copy all files from the server Data to the SD Card of my Android device.
我怎样才能做到这一点?
How can I do this?
推荐答案
这个逻辑从服务器的.zip文件下载数据。这将获取的数据从您的域名服务器文件夹,并保存到PATH =/数据/数据/ your_pkg_name / app_my_sub_dir /图片/;
//下载内容
This logic download a data from server as .Zip file. This will fetch data from your domain server folder and saved into the PATH=""/data/data/your_pkg_name/app_my_sub_dir/images/";
// Download Contents
Thread t = new Thread() {
@Override
public void run() {
try {
URL url = new URL(
"http://192.168.98.23/Data");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
//System.out.println("fileLength: " + fileLength);
int size, BUFFER_SIZE = 8192;
int total = 0, progress = 0;
byte[] buffer = new byte[BUFFER_SIZE];
String PATH = "/data/data/your_pkg_name/app_my_sub_dir/";
String location = PATH + "images/";
try {
if (!location.endsWith("/")) {
location += "/";
}
File f = new File(location);
if (!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(
connection.getInputStream());
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + ze.getName();
File unzipFile = new File(path);
if (ze.isDirectory()) {
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
// check for and create parent
// directories if they don't exist
File parentDir = unzipFile
.getParentFile();
if (null != parentDir) {
if (!parentDir.isDirectory()) {
parentDir.mkdirs();
}
}
// unzip the file
FileOutputStream out = new FileOutputStream(
unzipFile, false);
BufferedOutputStream fout = new BufferedOutputStream(
out, BUFFER_SIZE);
try {
while ((size = zin.read(buffer, 0,
BUFFER_SIZE)) != -1) {
total += size;
progress += total * 70 / fileLength;
if (progress == 1) {
progressBarStatus = progressBarStatus
+ progress;
handlerProgressBar
.sendEmptyMessage(0);
total = progress = 0;
}
fout.write(buffer, 0, size);
fout.flush();
}
zin.closeEntry();
} finally {
fout.close();
}
}
}
} finally {
zin.close();
}
} catch (Exception e) {
}
// this.notify();
} catch (Exception e) {
interrput=true;
handler.sendEmptyMessage(1);
}
}
};
t.start();
这篇关于所有文件复制从服务器到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!