I have following folder structure:
ProjectFolder/images/some images
In the same folder
ProjectFolder/WEB-INF/classes/com/xyz/here is java file of controller.
How can I get the image path in the controller?
Please, help.
Thanks :)
If its a web context may be something like this could help
InputStream is = null ;
is = request.getSession().getServletContext().getResourceAsStream("/images/someimage.jpg");
or may be something like this:
InputStream is = null ;
String realPath = request.getSession().getServletContext().getRealPath("/images/someimage.jpg");
is = new FileInputStream(realPath);
You can store your image path in properties file.
store that property file in your classpath.
now access that property in your controller class like this:
Properties properties = new Properties();
/*to access your filename.properties file */
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
String sServerLocation = properties.getProperty("server.upload.docs.path");
note that you should use escape charater in your property file like :
server.upload.docs.path=D:\\JDIS3\\DOCS\\
Related
I have a file under resources folder src/test/resources/file.xml
and another under src/test/resources/test.properties. I want to set a property in properties file to point to file.xml. How can I achieve this?
Say I have a property
test.file = file.xml
and my java class reads the file as follows:
File cert = new File(fileName); // fileName is the value of test.file
This does not work however.
You can use Properties class to read and write to config files.
Firstly, you need to find the relative path for the resources using
below steps
Secondly, you can configure and load the test properties file
Finally, read the property value and append with the resource
directory
Code:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);
I want to open a .config file outside of the .jar file so that I can use its properties inside of the .jar. How would I do this?
For clearance you will use a config file from the (executable) jar like this:
InputStream in = this.getClass.getResourceAsStream("config.properties");
Properties p = new Properties();
p.load(in);
that loads config.properties relative to the class of the current object.
For a config outside you propably use a file:
InputStream in = new FileInputStream(System.getProperty("user.dir")+"/"+"config.properties");
Properties p = new Properties();
p.load(in);
Use this.
String path = System.getProperty("user.dir")
path += "/config/myApp.properties"
Now you have the path of your properties file.
You know what to do next
I have a bean that needs to take some parameters from a properties file but I can not find it (java.lang.NullPointerException) in order to open it. My bean is in the extra.beans package while the properties file is in the extra.dao package. I am trying to do
file = new FileInputStream("database.properties");
prop.load(file);
and I have tried any possible combination for the path but I can not find it. I am using Netbeans 7.4. How can I open it?
You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username
Specify the full path. It should work.
If you're loading the properties file into a Properties object, try the something like:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));
I don't know your full package structure but using this approach and putting the full path to the properties file would also work, ie: /extra/dao/database.properties
file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);
I need to know that how to read the directory path from a .properties file in java.
for example,
class clazz
{
string filename="d:/file.txt";
public void somemethod()
{
FileWriter f=new FileWriter(filename, true);
// etc
}
i want to read the filename from .properties file rather than from a variable.
how it can be done?
A fairly simple way is to use a resource file, through the ResourceBundle class.
ResourceBundle bundle = ResourceBundle.getBundle("com/file");
Where com/file is the file.properties in the com package.
This file could have a line like this:
file.name=C:\dir\file.txt
For get this particular data.
String filename = bundle.getString("file.name");
I hope this can help you. Good luck!
Check out the Properties class load() method. It reads in a file (in the common properties format) and then you can query the value of a property based on its name
If the properties file is in your class path then do this:
Properties prop = new Properties();
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.txt"));
Sting fileName = prop.get("fileName");
I need to get real path for file in my WebContent directory, so that framework that I use can access that file. It only takes String file as attribute, so I need to get the real path to this file in WebContent directory.
I use Spring Framework, so solution should be possible to make in Spring.
If you need this in a servlet then use getServletContext().getRealPath("/filepathInContext")!
getServletContext().getRealPath("") - This way will not work if content is being made available from a .war archive. getServletContext() will be null.
In this case we can use another way to get real path. This is example of getting a path to a properties file C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties:
// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");
// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");
if (decoded.startsWith("/")) {
// path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");
you must tell java to change the path from your pc into your java project so
if you use spring use :
#Autowired
ServletContext c;
String UPLOAD_FOLDEdR=c.getRealPath("/images");
but if you use servlets just use
String UPLOAD_FOLDEdR = ServletContext.getRealPath("/images");
so the path will be /webapp/images/ :)
In situations like these I tend to extract the content I need as a resource (MyClass.getClass().getResourceAsStream()), write it as a file to a temporary location and use this file for the other call.
This way I don't have to bother with content that is only contained in jars or is located somewhere depending on the web container I'm currently using.
Include the request as a parameter. Spring will then pass the request object when it calls the mapped method
#RequestMapping .....
public String myMethod(HttpServletRequest request) {
String realPath = request.getRealPath("/somefile.txt");
...
You could use the Spring Resource interface (and especially the ServletContextResource): http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/core/io/Resource.html
This approach uses the resource loader to get the absolute path to a file in your app, and then goes up a few folders to the app's root folder. No servlet context required! This should work if you have a "web.xml" in your WEB-INF folder. Note that you may want to consider using this solely for development, as this type of configuration is usually best stored externally from the app.
public String getAppPath()
{
java.net.URL r = this.getClass().getClassLoader().getResource("web.xml");
String filePath = r.getFile();
String result = new File(new File(new File(filePath).getParent()).getParent()).getParent();
if (! filePath.contains("WEB-INF"))
{
// Assume we need to add the "WebContent" folder if using Jetty.
result = FilenameUtils.concat(result, "WebContent");
}
return result;
}
my solve for: ..../webapps/mydir/ (..../webapps/ROOT/../mydir/)
String dir = request.getSession().getServletContext().getRealPath("/")+"/../mydir";
Files.createDirectories(Paths.get(dir));
try to use this when you want to use arff.txt in your development and production level too
String path=getServletContext().getRealPath("/WEB-INF/files/arff.txt");