Hello everybody,

I try to find out how to download any file from a URL that doesn't specify. For example:

http://translate.google.com/translate_tts?q=Daniweb

^^This URL directs to an .mp3 file, which is the spoken text by Google Translate.

I have this code already, but it helps only when I use URL with the name of the file, such as:
http://google.com/undefined.pdf
(THIS URL IS A MADE-UP)

package Download;
import java.io.*;
import java.net.*;
public class SampleFile
{
public static void main(String args[]) throws IOException
{
 
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new 
 
java.net.URL("http://bdonline.sqe.com/documents/testplans.pdf").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("testplans.pdf");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int x=0;
while((x=in.read(data,0,1024))>=0)
{
bout.write(data,0,x);
}
bout.close();
in.close();
}
}

Any help will be appreciated, Thanks!

Recommended Answers

All 6 Replies

i never ..., but i think that you need search for InputStream

http://download.oracle.com/javase/tutorial/essential/io/index.html

http://www.java2s.com/Code/Java/File-Input-Output/CatalogFile-Input-Output.htm

http://www.java2s.com/Code/Java/Tiny-Application/FileDownloadManager.htm

http://www.java2s.com/Code/Java/Tiny-Application/Browser.htm

Thank for your reply, but the browsers and the download manager also get the same error- they can't download files from unspecific url.

gosh, and you're surprised that you can't access a document if you don't know where to find it?

What'd a taxi driver say when you go to him and tell him "I'd like to visit a friend of mine, but I don't know where he lives or what his name is, I think it was a village with an "e" in the name somewhere"?

gosh, and you're surprised that you can't access a document if you don't know where to find it?

What'd a taxi driver say when you go to him and tell him "I'd like to visit a friend of mine, but I don't know where he lives or what his name is, I think it was a village with an "e" in the name somewhere"?

but the browser like FF can download the file from it, so why I can't?

> http://translate.google.com/translate_tts?q=Daniweb

This URL gives me a 404 (document not found) hence doesn't yield anything. The actual URL should be: http://translate.google.com/translate_tts?ie=UTF-8&q=hello&tl=en&prev=input .

I'd recommend using a property HTTP library which offers sufficient abstraction, something like HttpClient or Resty. A sample test code using Resty can be found here.

> http://translate.google.com/translate_tts?q=Daniweb

This URL gives me a 404 (document not found) hence doesn't yield anything. The actual URL should be: http://translate.google.com/translate_tts?ie=UTF-8&q=hello&tl=en&prev=input .

I'd recommend using a property HTTP library which offers sufficient abstraction, something like HttpClient or Resty. A sample test code using Resty can be found here.

Thank you very much SOS!
You really helped me- the Resty is what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.