I make an application , this can download file form server.
use this code >>>
public int startDownload(String url, String filename) {
// create url connector
URL u;
byte[] buffer = new byte[1024];
try {
u = new URL(url + filename);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
m_lMaxDownloadSz = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(FILE_PATH, filename));
m_bCancelDownload = false;
m_lCurrentDownloadSz = 0;
int len = 0;
while ((len = in.read(buffer, 0, 1024)) > 0) {
// if download is canceled.
if (m_bCancelDownload) {
f.close();
c.disconnect();
return FILE_DOWNLOAD_CANCELED;
}
if (knot++ >= PROGRESS_STEP) {
knot = 0;
myProgressDialog.setProgress(GetDownloadStatus());
}
f.write(buffer, 0, len);
m_lCurrentDownloadSz += len;
}
f.close();
c.disconnect();
} catch (Exception e) {
return FILE_DOWNLOAD_FAILED;
}
if (GetDownloadStatus() == 100) {
return FILE_DOWNLOAD_FINISHED;
} else {
return FILE_DOWNLOAD_FAILED;
}
}
and I want to use with PHP force download , but it not work , ordinary it use with file path like 'app/aaa.apk' it work! ,and I change to PHP file like 'php/forcedl.php' it not work.
I needs to use with php force download, How do I use?
ps. i have little english language skill , cause english language is not my main language
thank you
I discover for my answer
android-Java Code
__example:
byte[] buffer = new byte[1024];
String url = "http://www.bla-bla.com/forcedownload.php"
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
filesize = execute.getEntity().getContentLength();
fileOutput = new FileOutputStream(new File(FILE_PATH, "file_copyformserver.apk"));
while ((len = content.read(buffer, 0, 1024)) > 0) {
fileOutput.write(buffer, 0, len);
Thread.sleep(100);
}
fileOutput.close();
} catch (Exception e) {
e.printStackTrace();
}
php file
__example:
$file = '/home/bla-bla/domains/bla-bla.com/file/file.apk'; //not public folder
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
this is short code , sorry if cannot run. :)
This is perhaps not the best way to do it, but have you considered renaming the file upon successfully downloading it? I haven't tried it, but I believe you can do it using the File.renameTo() method in Android.
Here's some pseudo code that I think will work, can't try it out right now though:
File.renameTo(new File(FILE_PATH, filename.replace(".apk", ".php")));
Related
I'm downloading different medias files from my http server; mp3, jpg/png/ and html.
Everything worked fine when I used the now deprecated HttpClient.
I decided to use the HttpURLConnection.
But I encounter a problem with text files(html).
read() blocks on small html files, maybe waiting for a EOF or I don't know what, during few seconds and exits with the Exception "unexpected end of stream".
My code is:
URL url = new URL(urlString);
postParams = String.format("registration=%s&"....);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true); // It's a POST request which replies sending a file
urlConnection.setChunkedStreamingMode(0);
if (fileName.contains(".htm")) { // Tried this to see...
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
}
OutputStream output = urlConnection.getOutputStream();
output.write(postParams.getBytes("UTF-8"));
is = new BufferedInputStream(urlConnection.getInputStream());
/* Get information from the HttpURLConnection automatically fires the request
* http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests
*/
int status = urlConnection.getResponseCode();
if (status == 200) {
int len, size = 0;
byte[] buf = new byte[128 * 1024];
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
try {
while ((len = is.read(buf, 0, buf.length)) > 0) {
os.write(buf, 0, len);
size += len;
}
os.flush();
} catch (IOException e) {
Log.d(Constants.APP_TAG, "IOException." + e); // HTML files end here after few seconds
} finally {
nbFilesDownloaded++;
is.close();
os.close();
file.setReadable(true, false);
file.setWritable(true, false);
}
}
Any idea to explain why it cannot normally exit from read()??
EDIT: I verified that for the html files which cause this exception, my webserver doesn't include Content-Length in the header. Can it be the cause of the problem?
Try to use okhttp http://square.github.io/okhttp/. I have downloaded the files correctly. I hope to help.
Paul.
#Override
public void run() {
URL imgurl;
int Read;
try {
imgurl = new URL(ServerUrl);
HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection();
int len = conn.getContentLength();
Log.d("check", "ContentLength:" + len);
Log.d("check", "ServerUrl:" + ServerUrl);
Log.d("check", "LocalPath:" + LocalPath);
byte[] tmpByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(LocalPath);
FileOutputStream fos = new FileOutputStream(file);
for (;;) {
Read = is.read(tmpByte);
if (Read <= 0) {
break;
}
fos.write(tmpByte, 0, Read);
}
is.close();
fos.flush();
fos.close();
conn.disconnect();
} catch (MalformedURLException e) {
ut.CalltoAlertDialog_ok(getString(R.string.alert), getString(R.string.setting_skin_downloadfail));
} catch (IOException e) {
ut.CalltoAlertDialog_ok(getString(R.string.alert), getString(R.string.setting_skin_downloadfail));
}
mAfterDown.sendEmptyMessage(0);
}
This is file download source.
This code prints error "NegativeArraySizeException" from here
byte[] tmpByte = new byte[len];
So, I checked len's value.
len's value was -1.
But..
When i created yesterday, This code was not print error.
I have 2 apk file.
The apk created yesterday is not a problem. Even now this apk is no problem.
But, The apk created today is problem.
I did not modify anything.
What is the cause of this?
I think your problem is here:
HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection();
int len = conn.getContentLength();
Read documentation about the getContentLength method
Returns the content length in bytes specified by the response header
field content-length or -1 if this field is not set.
Returns the value of the response header field content-length.
So this case that getContentLength returned -1 seems to have happened to you. Then you use this -1 to set your Array size. => Exception thrown
Check the solution of this question about getContentLength returning -1, maybe you will have to do something similar.
But at least you will have to check that len > 0 before setting your array size
I have created a HTTP file server with the objective of transferring media files (mp3, ogg etc) to an Android device. When the server is accessed from the android browser like
10.0.2.2:portNumber/path/to/file
The server initiates the file download process. Of course the customer would not do such a thing, Its fine for testing the file server.
I m new to Android development and have learned that httpclient package can manage get/post requests. Here is the sample code I have been using for reading the response
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
The above code works fine when the server sends a list of file in JSON. Since the server part of sending file has been coded, the point where I m stuck is in retrieving the media file on android.
I m confused about how to receive the mp3 files send by the server. Should they be read in a stream ? Thanks
Yes, you want to read the file onto disk via an inputstream.
Here's an example. If you don't want a file download progress bar then remove the progress related code.
try {
File f = new File("yourfilename.mp3");
if (f.exists()) {
publishProgress(100,100);
} else {
int count;
URL url = new URL("http://site:port/your/mp3file/here.mp3");
URLConnection connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
long total = 0;
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)(total/1024),lengthOfFile/1024);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
} catch (Exception e) {
Log.e("Download Error: ", e.toString());
}
I have written an app which uses an URLConnection to get a .html file.
Everything works fine over wifi. But over 3g the file is not returned correctly.
When i try to access the website via the browser it works fine.
Anyone has a suggestion?
Update:
Here is my code:
URL downloadUrl;
URLConnection downloadConnection;
InputStream inputStream;
byte[] inputBytes;
String[] output;
private void downloadSource(String pUrl)
{
try
{
downloadUrl = new URL(pUrl);
downloadConnection = downloadUrl.openConnection();
downloadConnection.setConnectTimeout(10000);
downloadConnection.setReadTimeout(10000);
inputStream = downloadConnection.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
inputBytes = new byte[10000];
int i;
int i1 = 0;
while ((i = inputStream.read(inputBytes)) > 0)
{
result.write(inputBytes, 0, i);
result.flush();
i1 += i;
}
result.flush();
result.close();
output = result.toString().split("\n");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Maybe it's a little bit late for answer but I had the same problem, with Wifi the html downloaded had different spaces than the one downloaded with 3G.
I solved it deleting the User-Agent in the connection:
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","");
I hope it helps someone!
I have tried other methods to download info from a URL, but needed a faster one. I need to download and parse about 250 separate pages, and would like the app to not appear ridiculously slow. This is the code I am currently using to retrieve a single page, any insight would be great.
try
{
URL myURL = new URL("http://www.google.com");
URLConnection ucon = myURL.openConnection();
InputStream inputStream = ucon.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);
int current = 0;
while ((current = bufferedInputStream.read()) != -1) {
byteArrayBuffer.append((byte) current);
}
tempString = new String(byteArrayBuffer.toByteArray());
}
catch (Exception e)
{
Log.i("Error",e.toString());
}
Try to keep the connection open if the requests are to the same server. Also, try to avoid reallocations in the buffer, and read as much as possible in one go.
const int APPROX_MAX_PAGE_SIZE = 300;
try
{
URL myURL = new URL("http://www.google.com");
URLConnection ucon = myURL.openConnection();
ucon.setRequestHeader("Connection", "keep-alive") // (1)
InputStream inputStream = ucon.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(APPROX_MAX_PAGE_SIZE); // (2)
int current = 0;
byte[] buf = new byte[APPROX_MAX_PAGE_SIZE];
int read;
do {
read = bufferedInputStream.read(buf, 0, buf.length); // (3)
if(read > 0) byteArrayBuffer.append(buf, 0, read);
} while (read >= 0);
tempString = new String(byteArrayBuffer.toByteArray());
}
catch (Exception e)
{
Log.i("Error",e.toString());
}
Set Keep-alive header (not sure if you need this, on J2SE it is a configurable property as well)
Allocate what is "usually enough" in the buffer to avoid reallocation.
Read more than one byte at once
Disclaimer: This was written "in the blind" without access to a Java compiler. It may be that setRequestHeader is only available on a HttpURLConnection (cast needed), or that some parameters are wrong, but please feel free to edit if so.
Why don't you use the built in apache http components?
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(uri);
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
response.getEntity().writeTo(ostream);
}
Use a pooled HTTPClient and try to make 2 or 3 requests at once. And try to create a memory pool to avoid allocations and the GC stalls.