InputStream won't close, or takes forever to - android

I'm attempting to download an external mp3 into internal storage. However, the files I'm attempting to download are big, so I'm trying to download them in 1MB chunks so that you can begin playing them while the rest is downloaded. Here's my stream code:
InputStream is = null;
OutputStream os = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet( url );
HttpResponse response = client.execute( get );
MyLog.d( "Connection established" );
byte[] buffer = new byte[8192];
is = new BufferedInputStream( response.getEntity().getContent(), buffer.length );
os = openFileOutput( filename, MODE_PRIVATE );
int size;
int totalSize = 0;
while (( size = is.read( buffer ) ) != -1 && totalSize < 1048576) {
os.write( buffer, 0, size );
totalSize += size;
}
MyLog.d( "Finished downloading mix - " + totalSize + " bytes" );
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if ( os != null ) {
try {
os.flush();
os.close();
}
catch (IOException e) {
MyLog.e( "Failed to close output stream." );
}
}
if ( is != null ) {
try {
is.close();
}
catch (IOException e) {
MyLog.e( "Failed to close input stream." );
}
}
}
It downloads the file fine, but when it gets to is.close() in the finally statement, it hangs. If I wait a really long time it'll eventually close. It seems like it's still downloading the rest of the file. How do I avoid this and close the stream immediately?

With HTTP, You normally still have to read (and throw away) the whole rest of the response stream - you cannot just close. The entire stream must be consumed. I am not sure if Android's httpclient is based on commons httpclient 3 or 4 - with 4 you can use HttpUriRequest#abort() to end early. Not sure if 3 has such an option
Edit: Looked it up and it looks like httpclient 3 , and you can do httpget.abort()

Related

Android Large File Upload as Byte Array to Restful WCF Service giving Bad Request

I am facing problem while trying to upload a zip file containing images converted into byte array to a restful wcf service from a json client using HTTPPost. The byte array is encoded into BASE64 enclosed into JSON object and sent using StringEntity with 2 more parameters. Around 6KB of file gets uploaded without any flaws but file more than 6KB are not send and I get a Bad Request - 400 status code. Following code is used to upload the file:
File file = new File(dir, "file.zip");
byte[] buf = new byte[10240000];
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
Log.v("read : buf ", buf + " : " + readNum + " bytes");
}
byte[] bytes = bos.toByteArray();
imgData = Base64.encodeToString(bytes, Base64.DEFAULT);
JSONObject sendData=null;
Log.d("Image Data length", imgData.length()+"");
Log.d("Image data ", imgData);
try {
sendData= new JSONObject();
sendData.put("_binaryData", imgData);
sendData.put("_fileName", "fileName");
sendData.put("userid", userID);
int len = imgData.length();
int l=sendData.toString().length();
entity = new StringEntity(sendData.toString());
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Send request
int len = imgData.length();
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("Connection", "Keep-Alive");
request.setParams(httpParameters);
DefaultHttpClient httpClient = new DefaultHttpClient();
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
String str=response.getStatusLine().getReasonPhrase();
int i=response.getStatusLine().getStatusCode();
Log.v("ReasonPhrase :: StatusCode",str+" "+i);
int contentLength = (int) responseEntity.getContentLength();
char[] buffer = new char[(int) responseEntity
.getContentLength()];
InputStream stream = responseEntity.getContent();
Please help me in solving this.
If a message with <6k bytes does through, but messages with >6k don't, I'd take a look at the client and host limits for things like:
MaxBufferSize
MaxBufferPoolSize
MaxReceivedMessageSize
You don't say whether or not you have control over the host server settings, but you can increase and decrease the limits on items like those mentioned earlier. You can set them to Integer.Max if necessary, a size that would allow file uploads > 1 GB.

My file downloader occur error suddenly

#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

android httpclient to trigger a download from server

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());
}

Sending Large file over network/localhost

Here's the code:
private void sendFile(InputStream file, OutputStream out) throws IOException {
Log.d(TAG, "trying to send file...");
final int buffer_size = 4096;
try {
byte[] bytes = new byte[buffer_size];
while(true) {
int count = file.read(bytes, 0, buffer_size);
if (count == -1) {
break;
}
out.write(bytes, 0, count);
Log.d("copystream", bytes + "");
}
} catch (Exception e) {
Log.e("copystream", "exception caught while sending file... " + e.getMessage());
}
}
I'm trying to send a large File (InputStream file) over an output stream (OutputStream out). This code works for smaller files, but for something like 5mb and above (I haven't benchmarked the limit), it just freezes after sometime without error or anything.
Log.d("copystream", bytes + ""); would output for some time, but will eventually stop logging.
Log.e("copystream", "exception caught while sending file... " + e.getMessage()); never shows.
This is part of a larger codebase which is actually a file server that runs on the Android device.
Any ideas?
Here's what made it work:
while (true) {
synchronized (buffer) {
int amountRead = file.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
Use Multipart POST. Something like
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,null);
entity.addPart("File", new FileBody (new File(FILE_PATH), MIME_TYPE));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
return response;
Use AsyncTask Class for this, here is link for example
http://developer.android.com/reference/android/os/AsyncTask.html

How to download file on android with PHP force download

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")));

Categories

Resources