I am trying to figure out how to get the default file name of a file I download using an HttpUrlConnection.
I use a REST API to download the file so I can't parse the URL for the name and the "Content-Disposition" header doesn't contain the name either but when I put the link in my browser it will download the file with the right name so I'm thinking it must be possible to get the name from the result of the HTTP request somehow.
I have read the following two posts which addressed this problem without solving it for my particular situation: HttpURLConnection downloaded file name and
Get download file name from URL or HttpUrlConnection?
Related
I am trying this download link in swagger from post call in java using RestAssured. But after hitting the endpoint, the response is some unwanted characters displayed on console. This is a zip file we are talking about. How can i download the file and save it in my local?
Save file from response:
Path pathToFile = new File("path/to/your/file").toPath();
Files.copy(response.asInputStream(), pathToFile);
I am currently storing text on the cloud using
bucket.create(blobName, "hello".getBytes());
and blob name looks like this 1/1/1674/2020-06-02/9998-2-202062
The requirement is to generate pdf file from the data of multiple blobs. We can do this on our backend in java by getting the content from blob path like this
Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
new String(blob.getContent())
But we don't want to increase the load on the server by downloading the content first on the server and then send it to the front-end. So we are sending the signURL on the front-end so we can get the content at the front-end(angular 8) by using that URL and create the pdf. We are creating singurl this way.
Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
URL url=blob.signUrl(1, TimeUnit.HOURS,SignUrlOption.signWith(
ServiceAccountCredentials.fromStream(new FileInputStream(jsonfilePath))));
But the issue on front-end is whenever we click the URL on any browser it downloads the file with the blob-name
9998-2-202062 instead of just showing the content.
Is there any way so we can read the data from that cloud URL instead of downloading the content by chrome automatically?
And we do not change the browsers setting because it can not be done on the client machine?
The signUrl method only creates a link to the specified resource, you have to implement a way to read it, join the rest of the files you want to add and process them to create your PDF on the client side.
That said, for example you can create a javascript file and use the signed URL in a fetch function to gather all the text from the blobs, I found this example on another stackoverflow answer that might help you:
Promise.all([
fetch(signedURL1).then(x => x.text()),
fetch(signedURL2).then(x => x.text())
]).then(([sampleResp, sample2Resp]) => {
console.log(sampleResp);
console.log(sample2Resp);
});
Replace the signedURL1 and signedURL2 for the actual signed URLs that you created.
Once that you have the content of your files, you just have to create the PDF, I found a library named jsPDF that could be of your interest.
I have a cloud application having public URLs of files stored on S3 Amazon server i want to get File() object from that URL like:
File file = new File("https://jcomplycustomers.s3.amazonaws.com/business%2FLite%2Fe38f3d28-cf78-4b74-9158-3f260782a123%2FLibrary%2Fcea3737f-25ed-4473-b340-27ec257b59f3.docx");
but failed to get this file, exception return by the system is 'File doesn't exist', but if i paste this URL in browser, browser open this file.
I have check this with converting URL to URI and try to get object but failed. same exception there as well.
Actually i don't want to first download this file and again access with that local URL. and don't want to convert it in streams etc
Now my problem is only this how can i get object of File() from this public URL?
How can we get File() type object in Java from S3 amazon server open file URL?
You can't. The question doesn't make sense. A URL represents a resource which is usually remote. A File represents a filename on a filesystem accessible to your computer.
If you're trying to download the file, use URL.openConnection(), or URL.openStream(), etc.
If you're not, as you claim, you will have to explain exactly what you mean by 'get object of File() from this public URL'.
In downloading a file (Eclipse's win32 zip) from the following URL in Firefox, the filename is known to be eclipse-jee-juno-SR1-win32.zip.
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32.zip&url=http://download.eclipse.org/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32.zip&mirror_id=1
However, this file name is not specified in the Content-disposition header, which is the standard method of acquiring the server-suggested file name.
Here, eclipse's download is simply an example. I see that the file name is a part of the URL, but is there an alternative method to get the file name? I could use regex to extract the file name from the URL in this case, but it isn't guaranteed to be a part of every URL without a Content-disposition header.
Question: How can the download's file name be acquired when no Content-disposition header is present? Or, more localized, how does Firefox come up with the aforementioned name?
Or is Firefox simply parsing the URL here, and I've come across a case where it simply happens to work despite extracting the file name from an indirect, script-delivered download?
Content Disposition is the standard method for the server to suggest a file name. In the absence of a content disposition header, it's entirely up to the client to come up with a file name. The most common option is to take the last segment of the path.
In the absence of a content disposition header, the server isn't even really saying that the url should be downloaded to a file rather than displayed. It's just that most browsers default to saving as a file anything they cannot display.
I need a download a text/plain file in to a folder. The url does not end with .txt but it has content-type etc... properly set. When I use the browser it immediately prompts me to save the file. The browser automatically puts proper file name also.
Using java how can i download that url in to a folder? Note that I dont know the filename also but I want the file to be saved in a directory.
code to download a file is easy... my question is that I dont know by what name should i save my file. the filename is part of content-disposition header, now how do i extract that?
The HTTP protocol uses the HTTP headers to define some information about the data transferred.
You have the content-disposition header that can have a property filename that is generated by the server. This holds the name of the file being transferred. But it is optional. Should you handle the case it is not present. Here is the doc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
Depending on how you download the file, you'll have dozen of ways to retrieve this file name from the http header.
Give a look to the apache http client for instance.
HIH
M.