Error downloading file from a given URL - url

I am trying to download the given URL:
http://www.addic7ed.com/original/9521/7
but when i try to download the file using my Java Code:
URL url = new URL("http://www.addic7ed.com/original/9521/7");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream("abc.srt");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
The html page gets downloaded whereas the file to be downloaded should be a .srt extension file.
But when I try to download the above link using Internet Download Manager the file gets downloaded.
IDM converts the above given URL into http://www.addic7ed.com/srtcache/Supernatural/Supernatural%20-%2004x06%20-%20Yellow%20Fever.720p%20CTU.English.orig.Addic7ed.com.srt
So my question is how to achieve this in JAVA...?? Are there any API available to achieve this.

Have you looked at the HTML file? I suspect that it is actually an error page from the server, and that it contains clues about what is actually going on.
Here are some possibilities:
Maybe you need to supply authentication credentials.
Maybe the server is sending a redirect (3xx) response, and that the client side is not performing the redirect.
Maybe you need to set some extra headers to make the server realize that it should not turn the response as HTML. For example, an Accept header.
But note that the details will depend on the server that you are trying to talk to.
If I was trying to download files programmatically in Java, I would either use HttpUrlConnection or the Apache HttpClient libraries. Both will give you more control over the download process than simply using URL.openStream()

This is probably because the http response from http://www.addic7ed.com/original/9521/7 is a 302 redirect which your java code is not correctly handling. IDM correctly handles the redirect. A great tool to use if you are on a *nix based systme, or have cygwin installed on windows is curl.
curl -v http://myurl
will display all the http traffic information (request/response)

Have you tried with?
URL url = new URL("http://www.addic7ed.com/original/9521/7");

Related

WOPI Host implementation, trying to render doc in iframe

I'm trying to get Wopi host implementation in Ruby on Rails application.
My domain is whitelisted under CSPP. Trying to get the file contents in iframe, but I just see "Word Online" and a loading gif, I return binary file contents of docx as response to ..wopi/files/:id/contents. I don't get any calls hitting my host server.
Sample wopi_src_url: https://word-view.officeapps-df.live.com/wv/wordviewerframe.aspx?ui=1033&rs=1033&dchat=false&IsLicensedUser=0&WOPISrc=https://sgdevwopi.test-wopi.sycamoreinformatics.com/wopi/files/31/contents?access_token=eyJhbGciOiJIUzI1NiJ9.eyJ1c2&access_token_ttl=160000000
Able to get the Wopi validation page in Iframe using .wopitest file. How should I proceed further? Or what am I missing? Please help.
Note: I'm using ngrok to make my local app server visible publicly with whitelisted domain.
I see to problems with your URL. You must ensure that
the URL is of the form http://server/<...>/wopi/files/(file_id) (so drop the /contents part - WOPI client will call the /contents endpoint automatically when necessary)
the WOPISrc parameter value is encoded to a URL-safe string
More info here and here.

How to Upload Video to Rails Server from App Inventor

My goal is essentially to upload a video file, like this, but with Ruby on Rails instead of PHP. I can successfully send JSON data to my server, but haven't been able to get file uploads working. The end objective is to have the file be in the server's /tmp directory, just like files uploaded via a webpage file_field_tag.
This image:
shows what I've tried so far. The result is that on the server, the parameter list is empty, unlike if you had used a file_field_tag. In the PHP example, they are able to get the contents of the file from the input stream... maybe there is something similar in Rails?
I know my API works, as I was able to successfully make a request using a JavaScript XMLHttpRequest, so I'm led to believe the solution involves working around what App Inventor offers for HTTP requests.
Edit: Removed unsupported header since the PHP example doesn't use headers anyways

Downloading a file using luasocket

I'm sorry if this has a simple solution, or if this has been resolved before, but I am so far removed from any kind of network programming that I really have no idea what is even wrong nor what to search for to fix it.
I would like to be able to download a json file from "https://api.coinbase.com/v2/exchange-rates" (and other places) using a language I know well, Lua, with the extension, luasocket.
I've tried using http.request and ftp.get, but neither returns any kind of information. I don't understand why I can go to that url on my browser, and it downloads a file, but I can't get the file data through luasocket. If I cannot use luasocket, then what are the differences between a request made from your web browser and a request made from something like luasocket?
As you may see the URL you have is https not plain http. You need to use encrypted connection. Use luasec for that instead of luasocket (actually luasec depends on luasocket but let's not bother for now; in short https.request from luasec is built on http.request from luasocket).
local https = require "ssl.https" --luasec
local r,c,h,s = https.request {
url = "https://api.coinbase.com/v2/exchange-rates",
sink = ltn12.sink.file(io.stdout)
}
This will print body of requested content in console.
More about luasocket's http and luasec's https.

Loading/Downloading a URL with a space followed by a number

I have a few files I'm trying to download in the following format:
http://someserver.com/movie_title 5.1.12.mov
When I place the following code on it:
mediaURL =[[mediaURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I get the following result:
http://someserver.com/movie_title%205.1.12.mov
If I copy paste that into my browser on any machine, it downloads correctly. However, It won't download as an NSURL on my project; it just fails.
Any suggestions? I'm guessing it's something silly that I'm forgetting.
If I were you I would confirm that the escaping is the problem, it could be some other part of your code that is failing (if you have access to the server manually rename the file with only characters.mov)
If you try this and it works, make sure you are using the expected headers and http method (POST or GET).
Else please put the code on git so we can download it and figure out what is wrong.
I've found that the server that the video was coming from did not support streaming capabilities. I tried putting the same link on my safari browser and the item did not stream.
Thanks everyone

Alfresco CMIS, Bad filenames for download of files

I have a website that have an integration against an Alfresco installation through CMIS. The problem is that the content-url that I get from Alfresco is ugly. The major problem is that the filename is "content.xxx" (xxx-being the file-extension).
In another project we have solved this by streaming the document through the website and then to the visitor, but for this case (internal web) that doesn't make any sence and only introduce another source for problems. But I can't make the user to accept content.xxx as filename for all files they are going to use so I need a way to fix this.
Is streaming the file through the website my best choise after all?
It appears that you may be using the deprecated CMIS URLs. When I get the content stream for an object named "test.txt" using the appropriate CMIS URLs and the AtomPub binding (/alfresco/cmisatom) I use the following URL:
/alfresco/cmisatom/1b8980cc-1f1b-4ac3-b26f-17aeee0cefc9/content/test.txt?id=workspace%3A%2F%2FSpacesStore%2Fc20d54f9-01b6-4c80-861b-094c2246ab21%3B1.0
If I then connect using the deprecated URL (/alfresco/s/api/cmis) the content stream URL becomes:
/alfresco/s/cmis/s/workspace:SpacesStore/i/c20d54f9-01b6-4c80-861b-094c2246ab21/content.txt
Can you double-check that you are using the non-deprecated URL and see if this addresses your issue?

Resources