How to call application servlet from java class with relative path.
I want to call with GET method
/myServlet
from
myClass.
EDIT
So far i find:
BufferedReader in = null;
URL url = null;
try {
url = new URL("http://localhost:8080/myApp/myServlet");
in = new BufferedReader(new InputStreamReader(url.openStream()));
result = in.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) in.close();
} catch (IOException e) { }
}
but as i said i want to call a servlet with relative path. Not to decler hostname and appName.
Related
There is file PULS.TXT on URl mobile.puls-radio.ru
How can I read this file from java code ?
I use this code, but does not work
URL url = null;
try {
url = new URL("http://mobile.puls-radio.ru/PULS.txt");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
InputStream is = url.openStream();
Scanner s = new Scanner(is).useDelimiter("\\A");
Log.i("TEST", s.hasNext() ? s.next() : "");
} catch (IOException e) {
e.printStackTrace();
}
After reading a file, I'm trying to delete it, but the following error appears:
java.io.IOException: Unable to delete file test.zip at
org.apache.commons.io.FileUtils.forceDelete (FileUtils.java:1390) at
org.apache.commons.io.FileUtils.cleanDirectory (FileUtils.java:1044)
at org.apache.commons.io.FileUtils.deleteDirectory
(FileUtils.java:977)
Here is my code. I've been careful to close the InputStream in the finally clause and only then call the method that deletes the file, but even then I can only delete it when I stop the program.
InputStream is = null;
try {
is = new URL(filePath).openStream(); // filePath is a string containing the path to the file like http://test.com/file.zip
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line.trim());
}
String xml = sb.toString(); // this code is working, the result of the "xml" variable is as expected
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
removeFileAndFolder(absolutePath);
}
private void removeFileAndFolder(String absolutePath) {
new Thread(new Runnable() {
#Override
public void run() {
try {
String folder = getFolder(absolutePath); // this method just get the path to the folder, because I want to delete the entire folder, but the error occurs when deleting the file
FileUtils.deleteDirectory(new File(folder));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
After some tests I discovered that I can manually delete the file just before the line "is = new URL (filePath) .openStream ();". After it, and even after the line "is.close ();" I can not delete the file manually unless I stop the program.
I got it. I was opening the file by the url (is = new URL(filePath).openStream();), and trying to delete it by absolute path. I changed it to "is = new FileInputStream(absoluteFilePath);" and it works!
i use eclipse
and my structure looks like this:
TSG-jar
->src
->myBean.java (here i need a path)
and another one web application
TSG-war
->WebContent
->js
->boot.js
so they are both in the same workspace
I call the boot.js file from TSG-war in my TSG-jar (from myBean.java), and i need a path...but i have no idea how to do it...
In myBean.java i got this code:
private String getSrc(String path) {
try {
BufferedReader in = new BufferedReader(new FileReader(path));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I need the path of the javaScript.js file to send it to the function...
can u help me?
i tried:
String s = "..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
String s = "\\..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
String s = "\\..\\..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
System.out.println(getSrc(s));
its always null...
where is the error?
I have to read a file which can be local or in remote.
The file path or URI will come form user input.
Currently I am reading only local file,this way
public String readFile(String fileName)
{
File file=new File(fileName);
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ( (line=bufferedReader.readLine())!=null ) {
stringBuilder.append(line);
stringBuilder.append(System.lineSeparator());
}
return stringBuilder.toString();
} catch (FileNotFoundException e) {
System.err.println("File : \""+file.getAbsolutePath()+"\" Not found");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
The parameter String fileName is the user input which can be a path to local file or a URI
How can i modify this method to work with both Local path and URI ?
Suposing you have the URI in String fileName you can read url easily with:
URI uri = new URI(filename);
File f = new File(uri);
Check this link for further info
File class has also a constructor based on the URI so it easy to say new File(uri).. and everything stays the same.
However, the URI is system-dependent, as specification says "An absolute, hierarchical URI with a scheme equal to "file", a non-empty path component, and undefined authority, query, and fragment components".
I think that if you need to read something remote, the best way to do it is by using Sockets.
How do I get the path of a an executed .jar file, in Java?
I tried using System.getProperty("user.dir"); but this only gave me the current working directory which is wrong, I need the path to the directory, that the .jar file is located in, directly, not the "pwd".
Could you specify why you need the path?If you need to access some property from the jar file you should have a look at ClassLoader.getSystemClassLoader();
don't forget that your classes are not necessary store in a jar file.
// if your config.ini file is in the com package.
URL url = getClass().getClassLoader().getResource("com/config.ini");
System.out.println("URL=" + url);
InputStream is = getClass().getClassLoader().getResourceAsStream("com/config.ini");
try {
if (is != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
else {
System.out.println("resource not found.");
}
}
catch (IOException e) {
e.printStackTrace();
}
regards.
Taken From Java-Forumns:
public static String getPathToJarfileDir(Object classToUse) {
String url = classToUse.getClass().getResource("/" + classToUse.getClass().getName().replaceAll("\\.", "/") + ".class").toString();
url = url.substring(4).replaceFirst("/[^/]+\\.jar!.*$", "/");
try {
File dir = new File(new URL(url).toURI());
url = dir.getAbsolutePath();
} catch (MalformedURLException mue) {
url = null;
} catch (URISyntaxException ue) {
url = null;
}
return url;
}
Perhaps java.class.path property?
If you know the name of your jar file, and if it is in the class path, you can find its path there, eg. with a little regex.