0

I want to download the file associated to an url which is an http request to download a file

The url can be anything, just i know the final answer of the server will be a file, it can be any type and could have any name. I want to write a method with a signature like this:

public String saveUrl(String url, String outputDir){
  //something here
  return file.getCanonicalPath();
}

Example:

url="http://domain.com/?someparameter=somevalue", 
outputDir="C:\"
returned="C:\myFile.myExtension" 

Here How to download and save a file from Internet using Java? is very explicit about how to download a file. Just one problem. All methods specified requiers we give the target String Filename o target File object, but i want to obtain name from the http request, how to acomplish that?

Just for an example, i would like to feed the method with this: https://demo.xsa.com.mx/xsamanager/downloadCfdWebView?serie=CARS&folio=20&tipo=XML&rfc=TUNO700901493&key=4585d3f7518f5ffa062fa4e3b4e64831

and get the xml output with the name given for the server.

Any help?

Thanks in advance

Community
  • 1
  • 1
jechaviz
  • 541
  • 1
  • 9
  • 22
  • And how you are feeding the URL to the save function? – Ya Wang Jan 09 '15 at 05:32
  • How are you going to "obtain name from the http request"? Does the server return a file name somehow? (Many http requests return content that is not associated with a named file at all.) Or do you want to parse the URL and extract a name? If the latter, how would you handle your example? – Ted Hopp Jan 09 '15 at 05:32
  • @Invexity: The final user will feed the url, so i don't know what he will put there, but the server must answer with a file. – jechaviz Jan 09 '15 at 05:35
  • @TedHopp: Exactly that's the question. I don't know. I know the server answer with a file, just like when we clicks in a link in the browser and it "answers" with a download dialog in the browser with a proposed name; example url: https://demo-partners.xsa.com.mx/xsamanager/downloadCfdWebView?serie=CARS&folio=20&tipo=XML&rfc=TUNO700901493&key=4585d3f7518f5ffa062fa4e3b4e64831 – jechaviz Jan 09 '15 at 05:37
  • I am not sure what you are doing but... you can use selenium and use the webdriver to navigate().to(url) and it will download to the default save location of that browser. Then to get the name of the download you can just search that download directory. Probably another way but I just used selenium in my current project so its fresh in my mind. If you that is what you want to try let me know ill answer with a example. – Ya Wang Jan 09 '15 at 05:41
  • @Invexity: just i will process the answer if http request is an Instance of InputStream or an Instance of String, but still i don't know how to get the name of the file. – jechaviz Jan 09 '15 at 05:42
  • @Invexity: I'm creating a program for download files linked in body mails. Because in a mail we can have any kind of links, i'm interested just in pdf and xml linked files (invoices in pdf, and electronic invoices in xml format, in fact). But providers are a lot so i can't guess if an url points to a file, i need to download it and the check what kind of file is and then process it. That's my need. – jechaviz Jan 09 '15 at 05:45
  • Ok your choices. Are... 1) use selenium and download via there get file/filename from the download directory. or http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests – Ya Wang Jan 09 '15 at 05:47
  • @Invexity: I have used selenium... but i don't know if it will be like a bomb to kill an ant. I just want to get the name of the file! Is it so hard? The other option i think is like greek for me – jechaviz Jan 09 '15 at 05:55
  • It's hard if you don't want to learn the other link I provided to you. Selenium isn't really a bomb and it shouldn't be too taxing considering you intentions are not very time constraining. You only need like 5 lines of code with Selenium. – Ya Wang Jan 09 '15 at 05:58
  • Does the filename matter? What are you going to do with the file after creating it? – user253751 Jan 09 '15 at 06:06
  • @immibis: just save it. So i need a name (it could be anything), but extension couldn't be anything. – jechaviz Jan 09 '15 at 06:27
  • @jechaviz Why can't the extension be anything? Why do you need an extension? – user253751 Jan 09 '15 at 06:28
  • @immibis: i mean, if it's a jpg, i need save it as anyname.jpg or if it is a pdf i need to save it as anyname.pdf. I would like to get to the name from the server to be fileNameProposedByTheServer.pdf, as example, but if i cant get it, content type would be enough, to assign an extension to the file downloaded. – jechaviz Jan 09 '15 at 06:32
  • Are there a small number of extensions you need to handle? In general, the server doesn't tell you the extension. – user253751 Jan 09 '15 at 06:34
  • @immibis:Yes i just need .xml,.pdf,.zip and .rar – jechaviz Jan 09 '15 at 06:35
  • Well, i finally found the answer. It's here: http://stackoverflow.com/questions/29622396/how-to-get-file-name-from-the-server-request – jechaviz Aug 31 '16 at 23:48

0 Answers0