java


Downloading on Android - FileNotFoundException but file exists


I tried to download a file from a server in Android but all I get is a FileNotFoundException.
My download method works fine for all files I give it, but not on this one specific url.
The url it is not working on looks like this: http://server.com/download.aspx?action=geta&sid=12345&file=somedata.xml&client=android
I see no errors in the url, if I enter it into a webbrowser, i imediately get the file. The permissions are also allright, I can use the method to download every other file from the server, for example with "getfile" instead of "get", but the file i want to download is only accessible through "get", so i can't just replace it.
Anyway, here is my download method:
public String downloadURL (URL url, String fileName) throws IOException {
String destination = null;
try {
destination = Environment.getExternalStorageDirectory() + "/"
+ "myapp/" + fileName;
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destination);
byte buffer[] = new byte[1024];
int bytes = 0;
while ((bytes = input.read(buffer)) != -1) {
output.write(buffer,0, bytes);
}
output.flush();
output.close();
input.close();
}
catch (Exception e) {
Log.e("DOWNLOAD URL", "downloading file failed due to " + e.toString());
}
return destination;
}
I'm kind of in despair here, I searched StackOverflow for a while but got no solution, and would be very grateful for help.
EDIT: The error stack, if that is, what I think it is:
08-16 17:32:58.530: WARN/System.err(24754): java.io.FileNotFoundException: http://myfirm.com/download.aspx?action=get&sid=6d62429x-4e1c&file=data.xml&client=android
08-16 17:32:58.530: WARN/System.err(24754): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:532)
08-16 17:32:58.530: WARN/System.err(24754): at java.net.URL.openStream(URL.java:645)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader.downloadURL(Downloader.java:412)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader.getData(Downloader.java:465)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader$GetFilesTask.doInBackground(Downloader.java:245)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader$GetFilesTask.doInBackground(Downloader.java:1)
08-16 17:32:58.530: WARN/System.err(24754): at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-16 17:32:58.530: WARN/System.err(24754): at java.lang.Thread.run(Thread.java:1020)
The problem is with your destination file. Either the directory myapp does not exist on the SDCard or you didn't include in your manifest the permission to write on the SD Card:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Hard to tell without more precise info, but my hunch is that you need to create your local file before you try to write to it:
File f = new File(Environment.getExternalStorageDirectory() + "/"
+ "myapp/" + fileName);
if(!f.exists()){
f.createNewFile();
}
OutputStream output = new FileOutputStream(f);

Related Links

Longest Palindromic substring
Programmatically Change Drawable file radius android
decompiler code Error
Inserting Time in H2 Database - Function “PARSEDATETIME” not found
Jackson module signature prevents addings serializers for self-referencing generic types
Backwards Incompatible Maven Dependencies
Atomicity of Reads and Writes for Variables in Java
How to use CellTable.Style with GWT 2.7.0 without UiBinder?
How to run very long process in a java based web application?
Android Studio abstract?
Type erasure and type checking
Inserting a JFrame with Input and Displaying the Output
How to copy files from an absolute path to docker image using docker maven plugin
How to generate an empty bpm:comment box in Alfresco Activiti Workflow Task?
How to define margin of CardView
JNLP Launch Error using Git Pages

Categories

HOME
log4j
amazon-ec2
zeromq
image-processing
dictionary
kde
nullpointerexception
office365api
vmware
spagobi
xmpp
rsyslog
kalman-filter
bookshelf.js
spring-cloud-stream
maven-3
cakephp-2.5
twitter-bootstrap-4
c#-2.0
icloud
webpack-2
acquia
in-app-purchase
portia
multiple-records
node-pdfkit
modx-revolution
u-sql
php-7.1
flask-wtforms
apache-metamodel
swingx
reverse-proxy
functional-testing
facebook-instant-articles
angular2-aot
pingfederate
rst2pdf
assistant
quote
gsmcomm
unboundid
microsoft-chart-controls
react-chartjs
google-rich-snippets
create-table
titanium-mobile
usbserial
io-redirection
espeak
gesture
abstract-class
occlusion
az-application-insights
angularjs-factory
tasker
graphenedb
ws-security
http-live-streaming
flashair
cubic-spline
angular-resource
network-flow
prolog-setof
jquery-nestable
python-c-api
promela
python-webbrowser
sage-one
return-value
fancybox-2
tactic
diagnostics
lift-json
nbconvert
specrun
time-and-attendance
azure-sdk
measures
asp.net-4.5
contact-list
captivenetwork
bluegiga
pagerank
freelancer.com-api
intellij-14
jqgrid-formatter
php-ci
sysinternals
tablelayout
phalanger
onactivityresult
project-planning
navigationservice
elliptic-curve
jubula
doskey
transcoding
pyhdf
quantlib-swig
.aspxauth
listings
specification-pattern
isnullorempty
cisco-jtapi
coderush
twrequest
bubble-chart
yetanotherforum
recent-documents
site.master
yslow
createwindow
getresponsestream
bespin
ctp4
genealogy
grid-system

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App