download doc file on android from server
hi i have a word document on server which i want to download it from android.i am using the following code
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.setDoOutput(true);
conexion.setConnectTimeout(60000);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(conexion.getInputStream());
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() +
"/abcd.doc");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();`
when i try this code with a sample flickr link(http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg) it works but when i t开发者_JAVA百科ry my server url it doesnt connect.The file gets downloaded from browser though.Also can sombody tell me what is the difference between two methods openConnection() and connect() ?
Update:
It also works fine with our localhost but not on the server. In logcat I see, Request time failed: Address family not supported. Something need to be set for Doc files?
URL.openConnection(); prepares connection to be made Connect is the start of conection i think sorry if i'm wrong.
JAVADOC SAYS THESE
connect() Opens a connection to the resource. This method will not reconnect to a resource after the initial connection has been closed.
URL.openConnection() Opens a connection to the remote resource specified by this URL. This connection allows bidirectional data transfer.
hope it helped
You may need to set the content-type for word documents:
Response.ContentType = "application/ms-word";
But there are variations depending on the version.
精彩评论