opening multiple sites in IE using java program - java

i want to open multiple websites in IE using java program.
here is the code
the websites are fetched from txt file
while((line = br.readLine()) != null)
{
StringTokenizer webLink = new StringTokenizer(line,";");
while(webLink.hasMoreTokens())
{
String webPathString = webLink.nextToken();
System.out.println(webPathString);
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(webPathString));
}
}
problem is only last site is getting opened in a browser insted of all.

desktop.browse will open all links in a current window only
For more details check the below discussion
Can Java's Desktop library launch a URL in a new Browser Tab or Window?

Related

Read and edit onlinefiles with java

I want to read and write a text document which i upload in "one drive" or "google drive" in java.
BufferedReader bufferedReader;
String line;
URL url = new URL("https://docs.google.com/document/d/1JLRmW7eaXHyyoPdNCyNN7AHWzKWaAPWgL84sxHUB7dQ/edit");
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
With this code i only read the HTML documentation and thats not helpful.
In this text are some IPs from servers I have to connect with clients.
Note that i want to access this file from a java program, not an android app.
As i researched, the available google drive apis are not quite suited for java implementation,
since they are build for android solutions.
the available google drive apis are not quite suited for java implementation
Google APIs support Java. Android is Java, so the API (classes, methods you need to call, etc.) is the same on either platform.
The Drive API is part of the Google API Client Library. Here is a great overview:
Google API Client Library
A Developer's Guide with instructions on getting set up and some Examples.
The drive-cmdline-sample project would be a good place to start. There is a downloadFile method that looks like it will get the source file:
/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
// create parent directory (if necessary)
java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

using java not javascript, open URL in same tab of default browser

I have written a java script whose work is to open URL's mentioned in the text file and it should repeat this process continuously but the problem is as soon as it reads the new URL mention in next line of text file it opens that URL in new tab but I want to open all the URL's in same tab of chrome browser.
Code what I have written:
while(true){
BufferedReader buf = new BufferedReader(new FileReader("C:\\link.txt"));
String currentLine = null;
while((currentLine = buf.readLine())!=null){
System.out.print(currentLine+"\n");
Desktop.getDesktop().browse(new URL(currentLine).toURI());
Thread.sleep(10000);
}
}
}
Is there any other option then Desktop.getDesktop() that will also work
My text file has two links like this:
https://www.google.co.in/?gfe_rd=cr&ei=y8a_WPTUFLOl8weF8bK4DQ
https://in.yahoo.com/
How to open them in same tab?
Desktop.getDesktop().browse()
Does NOT support this, as seen in Can Java's Desktop.browse provide an HTML Target in order to reuse a browser window?
Though, alternatives for this are the use of Process as seen in
Process oProc = Runtime.getRuntime().exec( currentLine );
And then make use of this process to send more commands to.
Or make use of a library / API such as Browserlaunch
The issue with Chrome, however is that Chrome likes to open new tabs in a new screen. If you hit ctrl + N (new tab on a lot of browsers) on Chrome, it'll open up a new screen instead

Making web crawler download multiple web pages locally

I would like my web crawler to download all the browsed URL's locally. At the minute it will download every site it comes to but then overwrite the local file in each website visited. The crawler start at www.bbc.co.uk, downloads that file and then when it hits another it overwrites that file with the next URL. How can I make it download them in to single files so I have a collection at the end? I have this code below but I dont know where to go from here. Any advice would be great. The URL inside the brackets (URL) is a string which is used to manipulate all the browsed webpages.
URL url = new URL(URL);
BufferedWriter writer;
try (BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()))) {
writer = new BufferedWriter
(new FileWriter("c:/temp/data.html", true));
String line;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
writer.write(line);
writer.newLine();
}
}
writer.close();
You need to give to your files a unique name.
You can save them in different folders (one root directory for each web site).
Or you can give them a unique name (using a counter for example).

Open mails in outlook from java using the protocol "mapi://"

I developp a Java application using Windows Desktop Search from which I can retrieve some information about files on my computer such as urls (System.ItemUrl). An example of such url is
file://c:/users/ausername/documents/aninterestingfile.txt
for "normal" files. This field give also urls of mail items indexed from Outlook or Thunderbird. Thunderbird's items (only available using vista and seven) are also files (.wdseml). But outlook's items urls start with "mapi://" like :
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
The problem I have is opening the real item from Java in Outlook using this url. If I copy/paste it in the run dialog of Windows, it works ; it also works if I use "start" followed by the copied/pasted url in command line.
The url seems to be encoded in UTF-16. I want to be able to write such code :
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";
Runtime.getRuntime().exec("cmd.exe /C start " + url);
I doesn't work and I've tried other solutions like :
String start = "start";
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";
FileOutputStream fos = new FileOutputStream(new File("test.bat");
fos.write(start.getBytes("UTF16");
fos.write(url.getBytes("UTF16"));
fos.close();
Runtime.getRuntime().exec("cmd.exe /C test.bat");
without any success. Using the solution above, the file "test.bat" contains the correct url and the "start" command, but the run of "test.bat" results in the well known error message :
'■' is not recognized as an internal or external command, operable program or batch file.
Has anybody an idea to be able to open "mapi://" items from Java ?
Well, my question was a little bit tricky. But I finally found an answer and will share it here.
What I suspected was true : Windows uses UTF-16 (little endian) urls. It makes no differences working in UTF-8 when we only use paths to files such as images, text, etc. But to be able to access Outlook items, we must use UTF-16LE. If I were coding in C#, there wouldn't have been any problem. But in Java, you have to be more inventive.
From Windows Desktop Search, I retrieve this:
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
And what I did is creating a temporary VB script and run it like this:
/**
* Opens a set of items using the given set of paths.
*/
public static void openItems(List<String> urls) {
try {
// Create VB script
String script =
"Sub Run(ByVal sFile)\n" +
"Dim shell\n" +
"Set shell = CreateObject(\"WScript.Shell\")\n" +
"shell.Run Chr(34) & sFile & Chr(34), 1, False\n" +
"Set shell = Nothing\n" +
"End Sub\n";
File file = new File("openitems.vbs");
// Format all urls before writing and add a line for each given url
String urlsString = "";
for (String url : urls) {
if (url.startsWith("file:")) {
url = url.substring(5);
}
urlsString += "Run \"" + url + "\"\n";
}
// Write UTF-16LE bytes in openitems.vbs
FileOutputStream fos = new FileOutputStream(file);
fos.write(script.getBytes("UTF-16LE"));
fos.write(urlsString.getBytes("UTF-16LE"));
fos.close();
// Run vbs file
Runtime.getRuntime().exec("cmd.exe /C openitems.vbs");
} catch(Exception e){}
}

How to read a file from remote system using java?

I have a file copied in one computer and I need to access the file from other computer.
I am not sure, which protocol or which technology to use for this?
Please provide me any hints for this..
Update:
I am using Ubuntu Linux system.
I used the code :
File f = new File("//192.168.1.157/home/renjith/picture.jpg");// 192.168.1.157 is the ip of the computer, where I have the picture file
Image image = ImageIO.read(f);
But it is giving an exception:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
I have shared renjith folder also.
There are any number of ways to access files on remote machines, but they virtually all depend on the remote machine having been set up to provide the file in some way first. If you with to access files via java, the easiest method would probably be to set up an HTTP server on the remote machine (this can be done pretty easily using Apache HTTP server on a variety of platforms) and then using Apache Commons HTTPClient on the client side java app. Further discussion of how to install these or configure them is generally beyond the scope of Stack Overflow and would at least require a more specific question
HTTP is an option. However, if these are Windows machines on the same LAN, it would be easier to expose the directory on the remote machine via a file share and access the file through a regular file path. Similarly, if these are Unix-like machines, you could use regular file paths if you're using NFS. FTP's yet another option.
if the remote computer is in the same network and on a shared folder to the computer where your java code is running then try this piece of code for accessing it
File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");
here Comp-1 is the DNS name of the machine containing the file in the network!!!
You might try:
URL url = new URL("file://192.168.1.157/home/renjith/picture.jpg");
Image image = ImageIO.read(url);
You could try to mount that path first, and then load it. Do a :
subst x: \\192.168.1.157
and then:
File f = new File("x:\\home\\renjith\\picture.jpg");
Image image = ImageIO.read(f)
It should work.
Share the directory and access the file thruogh java code
try this one:
File f = new File("//10.22.33.122/images")
File[] files = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
// Specify the extentions of files to be included.
return name.endsWith(".bmp") || name.endsWith(".gif");
}
});
// get names of the files
String[] fileNamesArray = null;
for (int indx = 0; indx < files.length(); indx++) {
fileNamesArray[indx] = files[indx].getName();
}
return fileNamesArray;
Map your IP to network drive and try let us say the drive letter is X,
then code changes to File f = new File("x:\\home\\renjith\\picture.jpg");
Infact your file is already loaded in object f , try priting the value of the path f.getAbsolutePath() to console and see.. Actual error is with ImageIO
You can read from remote and write to remote using jcifs-1.3.15.jar jar in java but first you need to share location from remote system then it's possible.
try{
String strLine="";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("WORKGROUP", "username", "passwd"); // Authentication info here, domain can be null
// try (InputStream is = new SmbFile("smb://DESKTOP-0xxxx/usr/local/cache/abc.txt", auth).getInputStream()) {
try (InputStream is = new SmbFile("smb://xx.xx.xx.xxx/dina_share/abc.txt", auth).getInputStream()) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
String smbURL="smb://xx.xx.xx.xxx/dina_share/abcOther.txt";
SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL,auth));
byte bytes[]="Wellcome to you".getBytes();
fos.write(bytes);
}catch(Exception e){
e.printStackTrace();
}

Categories

Resources