writing file content into a http response - java

How can I write a content of a file into http response? I prefer to write it to an entity first if possible. I searched for examples, but unfortunately didn't find any suitable working one..

Of course it is possible. After all that's what all web servers do when they serve you pages. Add proper Content-Type and Content-Lenght (if known) to your headers, open your file, read it and write it your response.

Related

How to retrieve XML/RDF data from a dbpedia link or URL?

Recently I have been trying to learn Semantic Web. For a project I need to retrieve data from a given dbPedia link. e.g http://dbpedia.org/page/Berlin . But when retrieve data using java.net.URLConnection I get the html data. How can I get the xml from the same link ? I know that there is link in every dbpedia page to download the XML but that is not what I want to do. Thanks in advance.
Note that the URI of the resource is actually http://dbpedia.org/resource/Berlin (with resource, not page). Ideally, you could request that URI with an Accept header of application/rdf+xml and get the RDF/XML representation of the resource. That's how the BBC publishes their data (e.g., see this answer), but DBpedia doesn't do that. Even if you request application/rdf+xml, you end up getting a redirect. You can see if you try with an HTTP client. E.g., using Advanced Rest Client in Chrome, we get this 303 redirect:
In a web browser, you get redirected to the page version by a 303 See Other response code. Ideally, you could request the resource URI with the accept header set to application/rdf+xml and get the data, but DBpedia doesn't place quite so nicely.
So, that means that the easiest way is to note that at the bottom of http://dbpedia.org/page/Berlin, there's the text with some download links:
RDF ( N-Triples N3/Turtle JSON XML )
The URL of the last link is http://dbpedia.org/data/Berlin.rdf. Thus, you can get the RDF/XML by changing page or resource to data, and appending .rdf to the end of the URL. It's not the most ReSTful solution, but it seems to be what's available.
The good to access data from dbpedia is through Sparql. You can use Apache Jena to run sparql queries against http://dbpedia.org/sparql

Parsing a multi-part response from a http get

I'm developing an integration between 2 applications. Application 1 uses HttpClient GetMethod to request from Application 2. Application 2 will return a multipart response with files embedded. I thought this was a simple exercise, but cannot seem to find common support for parsing a multipart response from HTTP GET. How can Application 1 parse the multipart response from Application 2?
As you are using multi part encode to send the request to the server(Servlet). As multi part encode encrypt all the data in that form you have to decrypt them first and then you can use those values.
Please follow this link.
What does enctype='multipart/form-data' mean?.
Convenient way to parse incoming multipart/form-data parameters in a Servlet.
I was also stuck with same problem. I solved it using javax mail MimeMultiPart. you can see my solution here:- https://stackoverflow.com/a/42548549/5236494
For posterity, there is nothing wrong with this pattern even if it is badly supported by HTTP libraries:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
Notice, though that Content-Type isn't multipart/form-data but multipart/mixed but encoding with boundaries between parts is nearly identical.

nutch - Is there a way to get http response header fields parsed?

Can I get http respone header fields parsed with nutch?
Is it built-in capability that's need to be configured?
I've looked the internet and I can't find any info about this.
And also, if i do local file system crawling, is there a way to parse file's header? (size, description etc fields?)
See line 144 here . You can see that http response headers can be obtained and you can use that info.
For second question:
For parsing different file types, there are plugins provided by nutch. You need to study the same for the specific file type and get going.

Returning binary and text data in servlet response

Does anyone have an example of how to send both binary (image) and text-based data in a single servlet response? An example is returning an image and image-map all generated on the server. I was also not able to find a mixed-mode mime type to use to be able to perform this operation.
Thoughts?
Browser support for multipart responses is still pretty dicey (read here). But if you are planning to parse the response on the client side yourself there are some pretty good examples out there. The mime-type you are looking for is multipart/mixed.
You can use Data URI to embed binary objects into generated HTML.
E.g.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
See also: https://serverfault.com/questions/241218/why-do-http-servers-not-send-a-single-file-back-when-possible#241224
This is not how HTTP and HTML work. A first request is made to load HTML code. This HTML code contains <img src="..."/> tags, which point to the URL of the image. A second request is then made by the browser to load the image. You can't download the HTML and the image in a single request.
Many WAP browsers supports multi-part responses, but I don't think "regular" browsers do.
Also see Browser support of multipart responses

Download single file with multiple connection in java?

I'm doing a project on java download manager.i want to download a single file(which is in some website) with multiple connections(just like download Managers do,example-IDM).Is this possible in java ?.if yes please help me how can i implement that.if you people have any sample code then please post.Thank you in Advance..Have a Rocking Future.
Here are a couple of hints. No code though.
A multi-connection download manager relies on the support for the Accept-Ranges header in the HTTP 1.1 specification. Servers would use this header to indicate that they support sending of partial responses to the client.
HTTP clients use the Range header in the request to obtain partial responses. All partial responses will carry a Content-Range header.
A multi-connection download manager would make multiple connections to a server supporting this feature. Each connection would issue it's own range of headers to download. The responses would then be collated in the necessary order to obtain the desired file. The size of the ranges can be pre-calculated using an initial HTTP HEAD request, which returns the actual size of the file in the Content-Length response header; the task of downloading the file may now be split into suitable chunks.
I'd recommend reading about Segmented downloading, thinking of a way to implement it in Java and than asking concrete questions if you have any.

Categories