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
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();
As you can say you are using LAN to transfer files from server to Android (Sdcard). For this purpose there are two approaches you can use.i.e i) TCP/IP protocol. ii) SMB (Server Message Block) protocol. I recommend you to use SMB protocol because in this you have to just sharing a folder with full permissions and copy all the files to Android Sdcard. At Android side in this case which is your client side you have to use four things. i) IP Address of the server. ii) Password of the Server. iii) UserName of the server and the last iv) Shared FolderName. With the help of these four parameters you make a connection and copy all the files which is placed into the Shared Folder.
Follow the code snippet that is used to make a connection using 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.
// *******************************************************************************************************
Dowbload File from PC Server to Android Client using SMB Connections. where strPCPath = "smb://" + 192.168.98.23+ "/" + strFolderName + "/FileName"; blow code is download a single file includes .config extension you can used this for downloading multiple files.
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.
// *******************************************************************************************************