1,105,242 Community Members

Code for downloading a file from URL without the name of the file

Member Avatar
yosi501r
Newbie Poster
9 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,345 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 
Member Avatar
yosi501r
Newbie Poster
9 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jwenting
duckman
8,330 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

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"?

Member Avatar
yosi501r
Newbie Poster
9 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

> 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.

Member Avatar
yosi501r
Newbie Poster
9 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

> 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.

Question Answered as of 3 Years Ago by jwenting, ~s.o.s~ and mKorbel
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: