Get size of file before downloading over HTTP - java

I'm downloading a file over the internet using HTTP and I would like to make a progress bar. How do I determine the size of the file I'm going to download, in bytes. I'm using an HttpURLConnection in Java, and I tried getting the Content-Length property in it, and it returned 529, even though the size of the file to be downloaded is ~200MB.

Related

OneDrive download session maximum time

I am writing a program which calls OneDrive REST api to download some files (total 12G) using java on the uri:
GET /drives/{drive-id}/items/{item-id}/content
And the inputstream is closed (socket reset from server) before the end of file transfer, causing the file download failed.
I tried to put the same request in chrome and firefox, same problem also appears.
Now I know that I cloud use partial download by adding the range header to the http request(and it works like a charm):
Range: bytes=0-1023
But still, I wonder what is the maximum download time/size that will trigger the close of the connection?

Tomcat-Https Post-File Upload-Parameters become null

I am trying to create a simple https service which allows an end user to upload a file to my system. I am using the HttpRequester plugin in firefox to test this service.
My code seems to work (file data reaches the server) when the files I am uploading are less than 1MB in size but does not work (request.getParameter(file) returns null) when the file size is larger than 1 MB.
After some googling around I changed some parameters in server.xml
<Connector port="21003" maxPostSize="0" maxHttpHeaderSize="0"/>
But still no luck.
Any idea of what I could try next? Any help would be much appreciated

How to pass image URL from from a HTTP server in selenium

https://saucelabs.com/resources/selenium-file-upload
I am trying to follow the above sample, but I would like to pass the input image file from a HTTP server instead of the local disk. How should I do it? I tried with the HTTP url but it doesn't seem to work as an argument.
You can't directly pass a file that is not on your local drive.
but you can do it in 2 steps:
1 - download the file. ex with http://commons.apache.org/proper/commons-io/:
FileUtils.copyURLToFile(aRemoteUrl, aLocalFile)
2 - upload the local file (as explained in your link)
(you can eventually delete the local file after that)

Having issue while downloading a file using content-disposition

I want my program to have a pop-up save as window option before file start downloading, however when I run my servlet it automatically starts downloading the file. What am I missing here ?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream fis=new FileInputStream("E:/sound.mp3");
response.setContentLength(fis.available());
response.setContentType("audio/basic");
response.addHeader("content-disposition", "attachment;filename=abc.mp3");
while(true){
int read = fis.read();
if(read==-1)break;
outputStream.write(read);
}
fis.close();
}
Your program is NOT desktop/standalone, since it is a servlet running on a server. When you run it in Eclipse by right clicking and run as -> run on server, Eclipse actually opens a web page to display the results. Therefore, your program is now a Web application, and Eclipse (or the page it opens) is the client. The client is saving the information you sent, NOT your program. Got it?
The content-disposition header is there only to suggest the file name of the download. The browser settings define if it will open a Save As Window or not. You cannot control that.
For example, in Google Chrome, in Setting/Advanced Setting/Downloads, there is the option Ask where to save each file before downloading. Only if this option is selected it will open the dialog you want. Otherwise it will save it in a default location (also defined in the browser settings). Similar options exist for all browsers.
Please also note that, depending on the content-type header, the browser will try to display the content, and not download it. For example, the browser will try to display texts and html. But then you can force the download by setting the header to a non-displayable type:
response.setContentType("application/octet-stream");
In case you don't want to create a Web app: Since your program runs on a server, it simply sends the information and is done. It is the client program who decides what to do with it. In your present case the client is a browser (or Eclipse opening a browser page). Headers such as the content-disposition header are aimed at browsers. If you are to create your own client (Swing client, Android app, iPhone app) which is NOT a browser, then the client will receive the information from the server and decide what to do with it (display it, or save it in any way), even ignoring the HTTP headers.
Try looking here: http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
take out the main statement in their code and put run(new FileChooserTest(), 250, 110); in your own code. If I were doing it, I would make an int named saveStatus and 3 finals that equal 0, 1, and 2 named waiting, save, and cancel. Then I would do a while loop in your other programming to see if saveStatus was equal to waiting to pause your program (but not the dialog). Afterwards, I would make an if statement to see if saveStatus was equal to save. If so, download it, and if not, don't. Simple as that.
Your problem is the Mime-Type. Some types (especially those where a specific handler is known) will be downloaded directly by most browsers. It does help a bit to use application/binary, but even then some browsers might be configured to download it or interpret the file name extension in the disposition handler.
I think most solutions use javascript on the page before the download link.
You have to implement the dialog manually, e.g. (http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html). After user selecting the file, you will be able to start the http request download (to your servlet) and save the file to the desired path.

xlsx file corrupted during downloading from server

Folks, here is the situation:
confirmed that the xlsx file is good on the disc.
I copied the file from server and can open without problems.
using FileInputStream and BufferedInputStream to handle the client side download function.
The download function i mean user can download the file by clicking a hyperlink, and a servelet call was made to the java class which uses FileInputStream and BufferedInputStream
Mime type was set correctly as application/vnd.openxmlformats-officedocument.spreadsheetml.shee
After download the file successfully, it will give a "converted failed" error while trying to use office 2003 to open this xlsx file.
any thoughts?
Thanks!
There are bytes which didn't belong in the HTTP response body or were simply missing there.
It's impossible to point out the actual root cause based on the information given as far. You have to check if the right bytes were written from local disk file system to the HTTP response body.

Categories

Resources