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

Retrieve .txt from server
Struts 2. s:property. How to get the value of a variable whose name is passed as a parameter to the jsp?
passing search query from main activity to fragment
ClickListner is not working on SearchView
Can select but not write/update to SQL server
Writing a GIF image over PNG image
Not able to insert or update to child table using annotion mapping in java
Changing scene content
Freemarker preprocessing
Using “gradle-3.5” to build web application but always fail in JPA
Passing resultset value to another class
Design approach : Excel export in client v/s in server side
How to handle a strategy with dependency injection
Couldn't capture the input video on hadoop
Where is the option to set the BigQuery maximum billing tier in the new Java SDK?
Porting ImageJ image stabalization over to OpenCV

Categories

HOME
pandas
vbscript
debugging
push-notification
relayjs
alfresco
q
analysis
spring-jdbc
graphql
mvvmcross
pivotal-cloud-foundry
awesome-wm
google-translate
spring-xd
carthage
undefined
connection-string
sms-gateway
jtextfield
plunker
physics-engine
orleans
tar
mps
csrf-protection
zurb-foundation-6
emgucv
microsoft-sync-framework
uninstall
filezilla
google-sites-2016
fog
noraui
catch-all
xmlreader
atl
mapdb
disassembling
qwerty
scaffold
automake
multilingual
abstract-class
twitch
opshub
lumberjack
sfdc
grid.mvc
midl
total-commander
segment
cubic-spline
filepicker
revapi
promela
composite-key
lift-json
np-complete
hittest
query-performance
topbeat
google-cdn
objective-c-swift-bridge
wdf
passport-google-oauth
azure-sdk
url-masking
phishing
transmitfile
lttng
rvest
apachebench
wapiti
qtableview
tmuxinator
wyam
map-projections
myo
graph-drawing
mono-embedding
operation
unity3d-gui
coldbox
phpthumb
rebol3
reactfx
mechanize-ruby
android-2.2-froyo
ccss
google-cloud-save
easy-install
sqlperformance
type-equivalence
delphi-6
referrer
angularjs-controller
itmstransporter
quickdialog
word-processor
qtkit
qt-jambi
vdsp
iphone-web-app
mysql-error-1005
sortable-tables
zend-tool
brewmp
privilege

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App