Get website URL for XML file JSP-Java - java

Let's say i have created the file
String path = application.getRealPath("userSearchFolder");
String name = path + "/" + (String) session.getAttribute("username") + ".xml";
File file = new File(name);
And later I want to make it available as a link, for example
<a href"<%=file.toURI()%>">File</a>
What happens is I get the directory path not url path ->
file:/D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.txt.xml
And when it reaches Firefox, I hover over the link and what i see is
file:///D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.xml
When I right click and choose Copy link Location and paste it in URL the xml file opens, but when I click the link, nothing happens.
How could I get a link like this
http://localhost:8080/GATE_Project/somepage/somepage/mjoraid.xml

The getRealPath will give a File system path (hence "real"), as opposed to web app path. So you cannot make it a href.
The following should suffice.
<a href="/userSearchFolder/${userName}.xml">
(Of course you are risking data mining for such public accessible XML files.)

You could use a servlet to serve the file.
This tutorial shows how to serve a pdf file(!)
the theory is the same:
you load the file in the servlet
Set any required headers
write the data to the response
The ContentType should probably be "application/xml".

Ok, i did it manually, similar to how i used to do it in php, create a variable that contains the website main directory.
String searchFolderURL = "http://localhost:8080/GATE_Project/userSearchFolder/";
and then
<a href="<%=searchFolderURL + file.getName()%>" target="_blank" >See original txt File </a>
Thanks btw.

Related

set Src of Iframe of uploaded PDF client side

I have uploaded a PDF on server by servlet , get that file on client side. Now I want to set the file to the src of Iframe. I have seen the examples but they are setting the src like src='http://www.tutorialspoint.com/java/java_tutorial.pdf?file=http://www.tutorialspoint.com/java/java_tutorial.pdf but I have the file with name like "file12314232343244"
I am setting the src like this
HTML pdf = new HTML("<iframe position='absolute'src='http://www.tutorialspoint.com/java/java_tutorial.pdf?file=http://www.tutorialspoint.com/java/java_tutorial.pdf' />");
what should I do for that ?
Use the type parameter:
type="application/pdf"
as shown https://stackoverflow.com/a/42908837/2979092

How to get Json data on page load OR get url generated from Json data on click event

I'm testing an existing website and I need to pick up the url of a link and the href text = "#".
If I click on the link, whatever it is opens then displays a perfectly normal URL in the browser, so the website is somehow translating "#" into a proper URL. I have several hundred items, all referenced by "#", but all opening completely different items when you click on them.
I need to know what it is before I click it as I have to be able to handle it opening as a pop up or in a separate window or as a load instruction to office etc. and therefore pass the URL to different bits of code to open, depending on the file extension.
I've tried:
teststring = "//div[#class='" + divClass.toString() + "']/div[2]/a";
LesTestString = driver.findElement(By.xpath(teststring)).getAttribute("href");
which returns 'myWebsite.com/my/path/here/#'; and
String LessLink = inputhandler.getAttribute("href");
which does the same.
when I look at the element in firebug it looks like this:
<div class="reference span5 ">
<a class="name math-formula" href="#"> 1.1 Lesson Player </a>
Any suggestions?
Added Information: It would seem that the URL is generated by the browser on click from various bits of information sent in a large Json string. So this questions should now be either (1) how do you pick up the url generated by the browser onclick, or (2) how do you pick up the Json sent from the server using java /selenium or something that will run in that environment?

How to customize the request from HTML to Servlet

I am building a server using Java's servlet and HTML forms.
I already managed to upload files: the user reach an HTML page, chose the file he/she wants to upload in his/her tree folder. The file is sent to a servlet I've written and downloaded on the server (actually I'm only running it on localhost for the moment, so the server is my 'My Documents' folder).
The next step I would like to make is this one:
The user (once logged, but I will manage to do that) reaches an HTML page, select a file that is hosted by the server and download it.
To make it, I will have to send to the 'Download Servlet' the name of the file. So here are my questions:
How to list the files that are in 'My Documents' on the HTML page.
How to send the name of the selected file to the servlet.
How to catch the 'request' and make a String of the name out of it.
To precise these two lasts points, please have a look at this:
List<FileItem> items = null;
items = upload.parseRequest(request);
FileItem item = items.get(0);
String fileName = item.getName();
The block above catches the name of the folder that is in the request. What I actually want to do is to do the same thing if what is in the request is a String (=catch the String contained in the request).
The File API will give you what you need for selecting the files in your directory.
List<File> files = Arrays.asList(new File("/your/directory").listFiles());
List<String> fileNames = new LinkedList<>();
for (File file : files) {
fileNames.add(somePrefix + file.getName());
}
request.setAttribute("fileNames", fileNames);
I do the above because you might not want to give the real path to your files, for security reasons. Once you have the list of file names in your request attributes, you can iterate over them in a jsp.
<form ...>
Select a file:<br />
<c:forEach items="${fileNames}" var="fileName">
<input type="radio" name="fileName" value="${fileName}">
</c:forEach>
<input type="submit" name="submit" value="submit">Submit
</form>
Now the files are each attached to an input element, which will translate to a request parameter. When the form is submitted, you can access the select file name by doing
String fileName = request.getParameter("fileName");
You can then append that file name to some directory structure and go and find it on the file system.

Image Extraction from webpage in java

I have just started working on a content extraction project. First I am trying to the Image URLs in a webpage. In some cases, the "src" attribute of "img" has relative URL. But I need to get the complete URL.
I was looking for some Java library to achieve this and thought Jsoup will be useful. Is there any other library to achieve this easily?
If you just need to get the complete URL from a relative one, the solution is simple in Java:
URL pageUrl = base_url_of_the_html_page;
String src = src_attribute_value; //relative or absolute URL
URL imgUrl = new URL(pageUrl, src);
The base URL of the HTML page is usually just the URL you have obtained the HTML code from. However, a <base> tag used in the document header, may be used for specifying a different base URL (but it's not used very frequently).
You may use Jsoup or just a DOM parser for obtaining the src attribute values and for finding the eventual base tag.

How to get all file paths while using html "file" input with multiple attribute

I am developing a web page in which I have to upload multiple files on
a single browse.
I am using html <input id="filelist" type="file" multiple=multiple>
This enables the multiple file selection and also retrieves the
full file path of all the selected file, which shows in file
upload text area.
<script language="JavaScript">
<!--
function showname(){
var filepath = document.form1.filelist.value ;
alert(filepath); //this shows only first filename among selected file
}
-->
</script>
But the problem is when I get the value of input, it returns only the first file
name among selected files.
Now how can I get the file paths which is shown in file upload text area.
Thanks!
This is browser specific. So you might be running this in a browser that doesn't support this. For example Firefox does. Here's an example of how to use this feature:
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
I would consider using http://www.uploadify.com/about/ or http://www.fyneworks.com/jquery/multiple-file-upload/. They should help you out, and also add some cool features to your file upload form.
File paths are deliberately hidden from the page for security purposes. To look at the local filesystem you need to use Java, Active-x (meh), or Flash.

Categories