6

How to get original file name when i download file from URL with java like this

File file = new File( "test" ) ;
FileUtils.copyURLToFile(URL, file)

Because when i create file i must put a name but at this stage i don't know yet the original name of downloading file.

5
  • 1
    You could get the path portion of the URL (something like new URL(URL).getPath()) and then the last component of that, but that may not exist, or the last component may be a directory-like component, so you'd have to handle those somehow. And all those are based on the URL, there's no way, in general, to get the file name on the server.
    – blm
    Nov 20, 2015 at 18:56
  • @blm unfortunately there is no file name in the last component of url.
    – Hayi
    Nov 20, 2015 at 19:07
  • 1
    Also look at URL.openConnection and URLConnection.getContentType. The server may send a suggested filename in the Content-Type header (or it may not). You'll have to open the connection yourself instead of just handing the URL to copURLToFile (although there may be another form that does that for you, I haven't looked).
    – blm
    Nov 20, 2015 at 19:14
  • yes it help it display application/zip but i want it exactly
    – Hayi
    Nov 20, 2015 at 19:48
  • 1
    Is application/zip the entirety of what getContentType returns, no filename parameter? If that's the case, then I don't think there's any way to do what you want. If the URL doesn't have it and the server's not telling you, then you'll have to come up with the name yourself.
    – blm
    Nov 20, 2015 at 20:16

1 Answer 1

10

For me the suggested file name is stored in the header file Content-Disposition:

Content-Disposition: attachment; filename="suggestion.zip"

I am downloading a file from nexus, so for different servers/applications it might be stored in a different header field but it is easy to find out with some tools like firebug for firefox.

The following lines work fine for me

URL url = new URL(urlString);
// open the connection
URLConnection con = url.openConnection();
// get and verify the header field
String fieldValue = con.getHeaderField("Content-Disposition");
if (fieldValue == null || ! fieldValue.contains("filename=\"")) {
  // no file name there -> throw exception ...
}
// parse the file name from the header field
String filename = fieldValue.substring(fieldValue.indexOf("filename=\"") + 10, fieldValue.length() - 1);
// create file in systems temporary directory
File download = new File(System.getProperty("java.io.tmpdir"), filename);

// open the stream and download
ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
FileOutputStream fos = new FileOutputStream(download);
try {
  fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} finally {
  fos.close();
}
2
  • Do you have any feedback?
    – Clerenz
    Dec 17, 2015 at 20:32
  • i try it now and it works just i have to change fieldValue.length() - 1 to fieldValue.length() - 2 in substring method
    – Hayi
    Dec 17, 2015 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.