Download a file with its given name

Normally if you want to download a file through your browser, the browser asks you for the destination path and the name of the file is already given. Now I want to download a file with that specific filename. How can I do that?
18 Replies
JavaBot
JavaBot•3mo ago
⌛ This post has been reserved for your question.
Hey @TomasGnG 😴! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st•3mo ago
What do you mean with downloading a file with a specific filename? Do you want to let the user choose the location and name?
TomasGnG 😴
TomasGnG 😴OP•3mo ago
No I want to get the original file name as I would be downloading it from the browser, you know?
Kyo-chan
Kyo-chan•3mo ago
Do you ever plan to explain? How could anyone ever guess what it is you're speaking about?
TomasGnG 😴
TomasGnG 😴OP•3mo ago
Did you ever download a file with your browser? Then you should've seen that the name of the file is always given. But when I download a file with Java I need to use a filename which I want to avoid
TomasGnG 😴
TomasGnG 😴OP•3mo ago
No description
dan1st
dan1st•3mo ago
Are you downloading a file from a Java application using the URL? If so, can you show the code? What are you using for downloading the file? I think the filename is in an HTTP header
TomasGnG 😴
TomasGnG 😴OP•3mo ago
I still didnt code it yet
dan1st
dan1st•3mo ago
You can get the Content-Disposition response header and extract the filename from there
TomasGnG 😴
TomasGnG 😴OP•3mo ago
Okay, I'll look into the headers real quick
dan1st
dan1st•3mo ago
MDN Web Docs
Content-Disposition - HTTP | MDN
In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.
TomasGnG 😴
TomasGnG 😴OP•3mo ago
Is this correct?
HttpURLConnection conn = (HttpURLConnection) new URI("https://tomasgng.dev/plugins/dynamicseasons/download/DynamicSeasons-2.5.0.jar").toURL().openConnection();
conn.connect();
HttpURLConnection conn = (HttpURLConnection) new URI("https://tomasgng.dev/plugins/dynamicseasons/download/DynamicSeasons-2.5.0.jar").toURL().openConnection();
conn.connect();
Because I am only getting these headers:
No description
dan1st
dan1st•3mo ago
Servers don't have to provide the Content-Disposition field In this case, the file name is inferred from the URL
TomasGnG 😴
TomasGnG 😴OP•3mo ago
Hm I see, let me try another url
dan1st
dan1st•3mo ago
preferably one where the name in the URL is different from the name you get when downloading it in the browser
TomasGnG 😴
TomasGnG 😴OP•3mo ago
Unfortunately the site where I will be downloading the files from is not creating that header :/ Still thank you for your help
JavaBot
JavaBot•3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot•3mo ago
Post Closed
This post has been closed by <@386870647894048768>.
Want results from more Discord servers?
Add your server