我想将所有文件从服务器复制到Android设备。 假设在服务器上,我的服务器ip是http://192.168.98.23,并且服务器文件夹的名称是数据。 Data文件夹包含许多文件。 我想将服务器Data中的所有文件复制到我的Android设备的SD卡上。将所有文件从服务器复制到Android设备
我该怎么做?
我想将所有文件从服务器复制到Android设备。 假设在服务器上,我的服务器ip是http://192.168.98.23,并且服务器文件夹的名称是数据。 Data文件夹包含许多文件。 我想将服务器Data中的所有文件复制到我的Android设备的SD卡上。将所有文件从服务器复制到Android设备
我该怎么做?
正如你可以说你正在使用LAN将文件从服务器传输到Android(Sdcard)。 为此,您可以使用两种方法。即i)TCP/IP协议。 ii)SMB(服务器消息块)协议。 我建议你使用SMB协议,因为在这你必须共享一个具有完全权限的文件夹并将所有文件复制到Android Sdcard。在这种情况下,Android是您的客户端,您必须使用四件事。 i)服务器的IP地址。 ii)服务器的密码。 iii)服务器的用户名和最后一个iv)共享文件夹名称。 在这四个参数的帮助下,您进行连接并复制放置到共享文件夹中的所有文件。
请按照用于使用smb protocole建立连接的代码片段。
public boolean VerifyUser(String address, String username, String password)
{
try
{
if (address != "" && username != "" && password != "")
{
setDomain(UniAddress.getByName(address));
setAuthentication(new NtlmPasswordAuthentication(null,
username, password));
SmbSession.logon(getDomain(), authentication);
return true;
}
else
{
return false;
}
}
catch (UnknownHostException e)
{
return false;
}
catch (SmbException e)
{
return false;
}
}// End VerifyUser Method.
// *******************************************************************************************************
使用SMB连接从PC服务器Dowbload文件从PC服务器到Android客户端。其中strPCPath =“smb://”+ 192.168.98.23+“/”+ strFolderName +“/ FileName”;吹码是下载单个文件包含.config扩展名,您可以使用它来下载多个文件。
public boolean downloadConfigFileFromServer(String strPCPath , String strSdcardPath)
{
SmbFile smbFileToDownload = null;
try
{
File localFilePath = new File(strSdcardPath);
// create sdcard path if not exist.
if (!localFilePath.isDirectory())
{
localFilePath.mkdir();
}
try
{
smbFileToDownload = new SmbFile(strPCPath , authentication);
String smbFileName = smbFileToDownload.getName();
if (smbFileName.toLowerCase().contains(".config"))
{
InputStream inputStream = smbFileToDownload.getInputStream();
//only folder's path of the sdcard and append the file name after.
localFilePath = new File(strSdcardPath+ "/" + smbFileName);
OutputStream out = new FileOutputStream(localFilePath);
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.flush();
out.close();
inputStream.close();
return true;
}
else
return false;
}// End try
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}// End downloadConfigFileFromServer Method.
// *******************************************************************************************************
你能解释一下使用代码吗? – user1153176
@Imran:我错过了什么?如何复制? – RvdK
使用SMB方法必须使用“jcifs-1.1.11.jar”文件,因为所有的smb类都依赖于这个jar文件。你可以下载它,我没有这个jar文件的链接。你可以从谷歌搜索它。 –
该逻辑从服务器下载数据为.Zip文件。这将从您的域名服务器文件夹获取数据并保存到PATH =“” /数据/数据/ your_pkg_name/app_my_sub_dir /图片/“;
//下载内容
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();
您打算在局域网使用此或不是本地的网络环境?你打算把这个任务重复一遍或者只是一次性的事件吗? – Jester
它是一些特定的服务器(例如Apache,IIS?)。 –
你可以将文件夹转换成.zip文件并且下载它在您的SDCard或您的应用程序路径... – prakash