Java URL and File Download - java

I do have two questions where one would be solved after the first question.
1) I want to open simple URL in my company laptop with java but it does not let me open it. It gives connection timed out error. Would it be cause of the network settings that I need to make them in my java code also ? When I click the IE settings on LAN, there is no proxy settings but the configuration part is clicked and there is an adress http://wgate.company.entp.tgc:8080/wpad.dat . Please let me know what I need to do in java code.
My sample code
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
2) I do have a list of link pages to download Word files. I want to make a loop in java and I want java to go to these web pages and download files at once instead of me. Is there any special function or code to download a file in java ?
Thank you.
sample url I have
http://www.gooogle.com/attachments/change/TestCase_F257579.doc

Related

How to open a file without extension by URL deployed at JBoss

Require to read a flat file that do not have any extension through URL, couldn't get this as browser(404) and java(FileNotFoundException) not taking the file as a resource to open.
import java.net.*;
import java.io.*;
public class URLTest {
public static void main(String[] args) {
/*Block 1: Reading a file "test.txt" (WITH extension) that works */
try{ System.out.println("Block 1");
URL url1 = new URL("http://localhost:8080/CtxPath/test.txt"); //file test.txt available at WebContent
BufferedReader in = new BufferedReader(new InputStreamReader(url1.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch(Exception e) { e.printStackTrace(); }
/* Block 2 : Reading the other one "test" (WITHOUT extension) that throws Exception */
try{ System.out.println("Block 2");
URL url2 = new URL("http://localhost:8080/CtxPath/test"); ////file test also available at WebContent
BufferedReader in = new BufferedReader(new InputStreamReader(url2.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch(Exception e) { e.printStackTrace(); }
}
}
Output
Block 1
File content from test.txt read successfully.
Block 2
java.io.FileNotFoundException: http://localhost:8080/CtxPath/test
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1624)
at java.net.URL.openStream(URL.java:1037)
at URLTest.main(URLTest.java:28)
Jboss 5.1 Java 1.7
Getting exception on open URL for the file(test) without extension. The other file with .txt extension(test.txt) that placed in the same location with similar content opens without any issues.
Is there any app specific or server specific config to do for allowing the file without extension to be served?
In the servlet container implementation, things without suffix "test" is not treat as file, but treat as servlet mapping.
You can download tomcat source code, the logic for this is similar in jboss, and take a look.
And I don't think you should make a file without suffix, if you really need to, then put it under WEB-INF/xxx/, then use a servlet to help read it.
Java is simply calling the URL. It cares not whether it has a . in the path. The server you are calling, on the other hand, /may/ have logic which determines how it attempts to fulfill the request. The FileNotFoundException indicates a 404 http status code was returned.
It may be possible that file does have extension that you may not be able to see because Operating system settings have hide the extensions or some rules on your server may causing it to return 404. Java does not give file not found exception because of extension.
Please let us know the issue, when you resolve it.

Java Applet Output File

I am trying to create a Java Applet that outputs information to a text file located in the same directory as the java applet. I understand Java Applets are not ideal, but I have spent a great deal of time on this and if possible want to solve this through applets. Here is some of my code on how I could read code from a file into a text box. I assume it would be something similar to this, but outputted.
public void readFile() {
String line;
URL url = null;
try {
url = new URL(getCodeBase(), fileToRead);
}
catch(MalformedURLException e) {
}
try {
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader
(new InputStreamReader(in));
strBuff = new StringBuffer();
while((line = bf.readLine()) != null){
strBuff.append(line + "\n");
}
a1.append("File Name : " + fileToRead + "\n");
a1.append(strBuff.toString());
}
catch(IOException e) {
e.printStackTrace();
}
}
If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available.
or on your local machine...
java.io.File file = new java.io.File(System.getProperty("user.home"), "yourfile.txt");
You must have it signed, otherwise...
Keep in mind that from an Applet, you cannot directly write to the server's file system. You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine.
A signed Applet has every right to write to the local file system of the person running the Applet. If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it!
EDIT
Signed Applet Tutorial

How to know if a webpage is fully loaded or not in java

I am working with java to get some information from a web page.
The problem is the information I need is generated by a JavaScript function. How can get this information because the code below brings only page information before full loaded (which means I can get only frames of the page).
code1.
URL target = new URL()
HttpURLConnection con = (HttpURLConnection)target.openConnection();
StringBuffer sb = new StringBuffer();
String line = "";
BufferedReader br = null
try {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((line = br.readLine()) != null){
sb.append(line);
}
} catch(Exception e){
e.printStackTrace();
}
Is there a way to know when the page has fully loaded in java? (Extra library can be an answer, but I wish to do it in java only). Thanks.
Your are making an HTTP request from java, this returns a text stream, the concept of "page loaded" is a browser related concept, the browser requests the content of the page (same as you are doing) and then renders the page and executes Javascript. It's the browser that executes Javascript.
If you want to make this only in Java, you need to implement a headless browser (a browser without user interface), or at least get the Javascript in the page you are loading and executing this. Doing this from scratch in pure Java is not an easy task, check out HtmlUnit for an example.
Java won't execute any client-side JavaScript. It will just read it. If you want a browser, use a browser.

Fetching a file from a PHP file in Google App Engine

I need to know if there is anyway that I can use file_get_html or any equivalent function in php on GAE? I know it has something called URLFetch() but I am not able to understand how I will call that from a php file.
Any help?
You cannot run PHP on Google App Engine. You can create a servlet which will read from any given URL and manipulate the data in any way you would need to, in Java (since you tagged this question with the Java tag).
From the AppEngine URL Fetch Java API Overview:
URL url = new URL("http://www.example.com/atom.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
// ...
}
reader.close();
If you meant that you are running PHP in another application and you wish to call your AppEngine servlet from said PHP application, then you can map the servlet which performs this URL fetch to a URL within your AppEngine application, then hit that URL from your PHP application. This, however, seems like a bad design, as you're making two network calls when you could have just used done it within the PHP application in the first place.
Here's a quick and dirty wrapper function I created for URL Fetch using PHP via Quercus on Google App Engine:
function fetch_url($url){
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
$java_url = new URL($url);
$java_bufferreader = new BufferedReader(new InputStreamReader($java_url->openStream()));
while (($line = $java_bufferreader->readLine()) != null) {
$content .= $line;
}
return $content;
}
// Sample usage:
echo fetch_url('http://google.com');
Hope this helps someone who is as lost as I was.

How to get the result of executing an html file in your local hard disk from a java program?

I'm trying to get the result of executing an html file on my hard disk as a string (thats the type of whats displayed in running the file) form a java program for the first time and it seems i did not get it right.
here is the code:
import java.io.*;
import java.net.URL;
public class MapDir {
public static void main(String[] args) throws FileNotFoundException, IOException
{
String s = "file:///F:/Stuff/Muaz/Programming/Mobile Applications/J2ME/Ride Sharing System/RSS Server/routeDistance.html?addr1=30.065634,31.209473&addr2=29.984177,31.440052";
URL url = new URL("file", "Core", s);
BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));
while((s = r.readLine()) != null)
{
System.out.println(s);
}
}
}
when I run the code I get the following error:
java.net.ConnectException: Connection refused: connect....
Of-course Im sure of the string File (s). I ran it form a browser and it works perfectly. The host name is also correct. So whats wrong? Please reply back as soon as you can. Thanks in advance.
Check the documentation for URL, you are...
String s = "file://location";
URL url = new URL("file", "Core", s);
... initializing it incorrectly. Take a look at this link.
Cheers!
Edit: ok, this is a bit long for a comment.
First, the URL class has no method of executing an html file that I know of. Or any class, for that matter.
Second, your code is READING the file, and it would work if it wasn't for the error we pointed out. How am I supposed to know that's not actually want you want to do? I thought the title was just bad english.
Third, nobody is paying us to answer your questions, so instead of barking, try thanking. The same attitude as in "answer as soon as you can" in your original post won't get you support or help.
Ah. I just had to say that. I'll accept any downvotes as just punishment.
Edit 2:
(From my comment below) #Muaz an HTML is just data. Nobody "runs" an HTML file, the same way you don't run a .doc, or a .avi -- you run another program that knows how to interpret this data and what to do with it. That program can be a web browser, Microsoft Word, or the VLC media player; but whatever the case, it's not the data file the one that's being executed. –

Categories