Opening a Webpage at an Anchor in a Browser through Java - java

I have created an application in JSwing that has a button that I want to open the user manual (which is a html file) in a browser. I can successfully open the entire webpage, but I want to link to certain anchors in the document. For example I am trying to use this code:
URI uri = new URI("c:/Giggafriggin/user_manual/user_manual.html#h1_3");
Desktop.getDesktop().browse(uri);
But this causes an error, claiming the file cannot be found. But if leave off "#h1_3" it opens the page in a browser without a problem. The anchors work when i enter them into the browser manually. Any ideas?

You -could- have that linking to another html page which goes to the end uri. Unfortunately, Java is not a web browser.

Looks like this is a known issue you wouldn't run into if you were using HTTP instead of a local file.
One easy fix is simply to point to a version of the that's already online instead of on disk.
If you can't assume the content is available online, you could always spin up an embedded HTTP server like jetty inside your application and point to that instead.

Related

What is a java.net.URLConnection doing?

When I connect() to a URL. Is Java fetching the webpage as a browser would, just without displaying it?
I'm trying to understand for example, if I were to connect to a Youtube Video URL. Even though i could not see the page, is the URL Connection loading the page and playing the video as if from a typical browser (without the UI or visual representation of the page)?
It is fetching the raw HTML of the web page. Similar if you where to open a page in your browser and right click->View Source.
If you connect to a Youtube page, you will get the raw HTML and within that code, will be a reference (href), most likely a tag that points to the source of the video.
--Edit
A browser then obviously interprets that HTML into what you see on your screen.
A modern browser automatically connects to all references on that HTML page, as if loading multiple pages simultaneously, and putting them together.
No, the URLConnection represents only that, a URL connection. Using UrlConnection.openConnection() is connecting to the page, but it still needs to be told what to do. It will provide you with a "printing" of the elements on the page only if told to do so. However, it has accessed that file. Connecting to and reading information from a page is a multistep process.
Please see the Oracle Java Documentation about URLConnections. It provides a lot more information and clarity into how this class works as well as how to use it.
http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html

File upload with Ajax - not getting complete fileName

It is quite a common question but I can't find an answer to it
I have a simple HTML with an input text box (type=file) and a submit button. On clicking the submit button, I call a js function where I try to get the complete path of the file
var data = $('#fileName').val();
the issue is I am not getting complete file path of the file I am uploading. I know due to security reasons chrome gives me a C:\fakePath\filename and firefox gives me only the fileName. But in case I need a complete path what shall I do?
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
You cannot get the complete path! there is no way to do that!! Even though you are on an intranet and you have enough permissions.
A workaround for this is to have a textarea and ask the user to enter the complete path of the file.
In short you can't have the full name of a file once is loaded on server side, you will just have the file name and its content in a raw byte array (among other attributes). This is not a Java thing nor other server side technologies issue, is related to browser implementation (but it looks that IE6 may contain a flaw about this).
Not directly related to your question but caught my attention
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
Usually, you can't handle a file upload using ajax because it can lead to security holes. Still, there are some browsers (like Chrome and Firefox) that allows you to send a file using XMLHttpRequest but that isn't allowed on some browsers (like IE8-) so you have to use an iframe in order to make the file ajax uploading work.
In order to avoid handling all these problems, I would advice you to use a third-party js library that handles the ajax file upload. An example is blueimp jQuery file upload that also has Java server side examples (DISCLAIMER: I do not work in this project nor I'm associated with blueimp in any way). Note that using this plugin requires that you have a mid knowledge on HTML/JavaScript/jQuery/Java Server Side so if you're a starter it may take you some time to make it work, but once it does is pretty good.
I dont know which technology you are using.. but you can always get file name once it is uploaded on server (Using php or .net )
your steps to upload should be like below:
1) Upload file to the server (e.z. /uploadedFiles/...filename
2) Create a method which will fetch file name from the uploaded path
3) simply insert file name in to the database (this will give you flexibility to change folder name of uploaded docs in future if required)
Generally filenames are not stored as it is . to avoid name conflict in future. So it is a advisable to always rename your filename by adding minutes & seconds after itsname.
If any doubts do ask.
Hope it helps.
Browsers block the filepath access on javascript for securit reasons.
The behavior makes sense, because the server doesn't have to know where the user stores the file on his computer, it is irrelevant to the upload process.

Possible to check if a website is open in browser from java

Is it possible to check if a website is already opened in the default browser from a java program? I need my program to open a specific website before doing some other stuff. So is it possible to check whether this website is already open?
EDIT:
Ok, i'll try til explain the situation a little further. So i want to download some files from a webpage (http://aula.au.dk/main/document/document.php?cidReq=IMFFOUANAE12). When you click a file you're redirected to some file destinations where you can download it. My program list all these files, and then when you click a filename the browser opens the url that will redirect you to the download of that specific file. My problem is then, if the url i linked above isn't open i get an SQL-error from the website. Apparantly this error only show when the above url isn't open i an tab. So if i download a file, close the browser, try to download a new i get the problem. But as below, it seems cookies can help me out.
I'm not that in to all this http, website kinda stuff.
Regards
Jesper
No it is not possible to do the thing that you have asked.
I am not sure whether it is possible, you can open Default browser by using Desktop class. Or you can do
Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{"C:\\Program Files\\Mozilla Firefox\\firefox.exe", "-new-window", "example.com"});
Why should you worry even if your page is already opened ?
Yes but even if you try to open same page browser will keep the session of old page.It will be tracked using jsessionid.
Please check the links using cookies you can do
http://www.mkyong.com/servlet/a-simple-cookie-example-in-servlet/
http://www.tutorialspoint.com/servlets/servlets-session-tracking.htm
Its not possible purely because Java has no way of knowing whether browsers are open and what browsers are in use. If such a feature were to be implemented it would literally require natively hooking into the api of every browser. Something which while can be done would only support the browsers you choose it to (Firefox,Chrome,IE,Safari,Opera) etc etc, but it requires directly interfacing with the native C libraries, something outside of the scope of this and certainly overkill for such a trivial feature.

Open MS Word document from network

Here is the case.
User should be able to open MS Word document which is located somewhere in the network (ie. \remote\machine\documents\document_to_edit.docx) with MS Word 2007 by clicking link in the browser. Browser is IE7+.
Edit the document, close it and save it (no "Save as..." just "Save") in the same place in the network (\remote\machine\documents\document_to_edit.docx)
Is that at all possible? If so how should I do that?
Don't think this is relative, but I'm using JAVA + Wicket for my web application.
EDIT:
Any suggestions are welcome.
Main thing is - open file as it was on your computer and save it after edit.
(Read update below)
Short answer: not possible.
Long answer: When you open anything from a browser, even some local file, it will open the given file from the browser cache (or some temporary download directory) but not the original file linked. Therefore, after you save the changes the copied file will be updated.
Added:
Ok, after I thought about it for a while, there could be some ways to do it in intranet application. Here's how: Java applet: run native code from browser?
So, basically, you will serve the applet which will communicate with your javascript (I guess this way would be easiest to implement) (info on how to do it) and send a network path to the applet. Applet will start winword.exe passing it the required parameters.
Or, there's another option with ActiveX: http://codereflex.net/how-to-run-exe-on-webpage/ . The downside is - it works only with IE, but that seems what you need anyway.
IE can open \\server\share\file.docx type links and it opens the original file, not a downloaded version. You may need to add the domain of your http server to trusted sites in the security settings of IE first though. Have tested this and it does work

Interact with Web Page from java

So I'm making a program for android that tries to download something from www.wupload.com. What I want isn't a browser but to interact with the webpage without actually showing it. Like how HtmlUnit is supposed to work.
I'm using apache for the html requests and what I've done so far is send a post that simulates clicking on slow download on the web page. Then I read the response so I can get some variables needed to make the next post and execute the next post. In theory, the web page should be showing the captcha cause the response I get is please enter the captcha, but no image url.
The next step would be to enter the captcha and finally download the file, the problem I'm having is I don't know how to show the captcha image to the user. Do I have to capture it somehow? I know how to make the post to send what the user would type, but the image url of the captcha isn't in the source code.
I thought of inspecting the web page so I could get the url from the DOM tree, like what inspect element on google chrome does, but I have no idea if it's even possible. Any ideas would be great.
thx
The captcha is probably generated using JavaScript. Therefore when you get the source of the website, the captcha hasn't yet been generated and you won't see the image in the source HTML. You would need to run the Javascript somehow. You could try using a WebView because it has built-in support for Javascript, or get a Javascript library for Java and use it somehow. I think it would be a lot of work.
Edit:
Actually, if they are using a thirdy-party captcha library, I'm sure it uses some sort of HTTP request system, so you might be able to inspect it with this plugin for Firefox.

Categories

Resources