android


Download files from android application


I have a strange issue in downloading files in my android application all the files without space can be downloaded but when I have a space in my filename the file will not be downloaded for example:
Will not be download but this link:
http:..../DIV/Bon de Commande.pdf
will be downloaded:
http:..../DIV/POLITIQUE_QUALITE_V6.doc
This how I download file:
protected String downloadfile(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
SharedPreferences myPreference= PreferenceManager.getDefaultSharedPreferences(getContext());
String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Document" ;
String Fichename=sUrl[0].replace(myPreference.getString("lientelecharge", ""), "");
String filePath=path+"/"+Fichename;
File file = new File(filePath);
if(file.exists()) {
}else{
// download the file
input = connection.getInputStream();
File folder = new File(path);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
output = new FileOutputStream(path+"/"+Fichename);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
if (fileLength > 0) // only if total length is known
output.write(data, 0, count);
}
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
Any help would be appreciated
Petrus is right - you have to urlencode string like:
String addressToGo = URLEncoder.encode("www.123.com/55 U.doc", "utf-8");
More ways to encode the string can be found at (my favourite one is without extra libraries): URL encoding in Android

Related Links

Drawables are always read from mdpi folder, regardless of screen size
Not able to write to sys/class/uwb/scan file through JNI using android application
HttpURLConnection get uploaded path of server in android
android compiler query
Android: Unable to start Activity, Resource is not a drawable
How to clean up an Android project?
Global Listener in Android
Listen for ringer muted by volume button click
making background thread to check if network available and notify when ever it is available
Android requestLocationUpdates using PendingIntent with BroadcastReceiver
button.setOnClickListener is giving me errors
opening google map in a tab in android not working
Android Emulator Reports 600x1024 MDPI as XLarge?
Surface View Appears Black while the associated audio is playing in the background(android)
how to make dialog box appear on the left?
How to do webview animation in android

Categories

HOME
assembly
facebook-oauth
jmeter
wakanda
ecmascript-6
angular-formly
zend-framework
soa
google-apps
jquery-ui
pcl-crypto
categorical-data
openpyxl
github-enterprise
opengl-es-3.0
libc
endpoint
radgridview
favicon
selenium-ide
renjin
docx
capistrano3
mvc-mini-profiler
google-picker
jmp
oledb
drive
dpdk
gulp-watch
pagespeed
exploit
ckfinder
fat
intersystems-cache
send
testbed
dsx-desktop
android-maps-extensions
baidu-map
fiware-cygnus
lint
filesystemwatcher
visa
softmax
naming
movilizer
install.packages
android-exoplayer
drupal-theming
httrack
jquery-ui-draggable
edit
mach-o
lsyncd
shinobi
rmongo
angular2-testing
atlas
flex-monkey
file-uri
tooleap
phpwebsocket
halcon
mobiledoc-kit
concurrentmodification
boomerang
rubaxa-sortable
ember-cli-rails
typemock
haskell-warp
public-html
qtwebengine
angular-gettext
filesplitting
deform
cbind
tweenlite
unix-socket
personalization
live555
vsx
spring-3
embedded-fonts
model-driven-development
modular
observium
asdf
time.h
octal
mantle
mysql-error-2013
joomla3.1
inkcanvas
google-maps-mobile
android-holo-everywhere
exponentiation
cgbitmapcontextcreate
msr
load-time
maven-1
burndowncharts
skype4java
mathematical-notation
windows-identity
java1.4
dsoframer

Resources

Encrypt Message