에이치의 모바일 앱 개발
DBFileDownload version 1.0.1 본문
/**
* Created by lsh on 2016-06-29.* version 1.0.1
*/
public class DBFileDownload extends AsyncTask<Void, Void, String> {
private Context context;
private String serverPath, localFolderPath, localFilePath;
private int version;
public DBFileDownload(Context context, int version, String serverPath, String localFolderPath, String localFilePath) {
this.context = context;
this.version = version;
this.serverPath = serverPath;
this.localFolderPath = localFolderPath;
this.localFilePath = localFilePath;
}
private long fileSizeCheck() {
long fileSize = 0;
return fileSize;
}
/**
* 다운로드시 버전 정보와 파일 크기를 비교하여 이어받기 동작이 가능하게 한다.
*
* @param params
* @return
*/
protected String doInBackground(Void... params) {
String zipName = null;
long localFileSize, remainingFileSize, totalFileSize;
int Read;
File folder = new File(localFolderPath);
try {
if (!folder.exists()) {
folder.mkdirs();
}
URL mUrl = new URL(serverPath);
HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int len = conn.getContentLength();
byte[] tmpByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(localFilePath);
FileOutputStream fos = new FileOutputStream(file);
for (; ; ) {
Read = is.read(tmpByte);
if (Read <= 0) {
break;
}
fos.write(tmpByte, 0, Read);
}
is.close();
fos.close();
conn.disconnect();
zipName = file.getName();
} catch (MalformedURLException e) {
zipName = null;
Log.e("Error", "MalformedURLException");
} catch (SocketTimeoutException e) {
zipName = null;
Log.e("Error", "MalformedURLException");
} catch (IOException e) {
zipName = null;
Log.e("Error", "IOException");
e.printStackTrace();
}
return zipName;
}
protected void onPostExecute(String fileName) {
if (fileName != null) {
Log.e("Download", "success");
new DBunzip(context, version, localFilePath, localFolderPath).execute();
} else {
Log.e("Download", "fail");
IntroActivity.AppFinish(context);
}
}
public static boolean fileExists(String localFolderPath, String localFileName) {
boolean file_flag = false;
File[] listFiles = (new File(localFolderPath).listFiles());
for (File file : listFiles) {
if (file.getName().equals(localFileName)) {
file_flag = true;
}
}
return file_flag;
}
}
'Android > Android 개발 소스' 카테고리의 다른 글
File & I/O (0) | 2017.12.23 |
---|---|
DBunzip version 1.0.1 (0) | 2017.12.23 |
SQLiteOpenHelper 1.0.1 (0) | 2017.12.23 |
ripple (0) | 2017.12.23 |
selector (0) | 2017.12.23 |