I am trying to read a xml file and parse it using Spring MVC, but for some reason it's not finding the file path. I tried different folder, even root, but still cant read or find the file in my project. I'm also using a bean and its not working ether.
<bean class="com.fdm.routePlanner.data.LineDataReader">
<property name="file">
<value>/resources/CompleteTube.xml</value>
</property>
</bean>
and
SAXBuilder saxbuilder = new SAXBuilder();
try {
return generateNetwork( saxbuilder.build(new File(file)) );
} catch (JDOMException e) {
When I try to parse the file using SAXBuilder it gives me a NullPointerException.
Related
I have a Spring project named 'finman'.
The directory structure
As you can see on the image the folder with properties (named 'config') is on the same level as 'src'. So I need to locate that path using
systemEnvironment['FINMAN_ROOT']
The code fragment from 'spring-mvc.xml' responsible for the resource bundle location:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:cacheSeconds="60"
p:defaultEncoding="UTF-8">
<property name="basenames" value="file:///#{systemEnvironment['FINMAN_ROOT']}/config/messages/app"/>
</bean>
The problem is that I don't know how to set that variable.
Environment variables set in OS:
Windows: https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Linux: http://www.cyberciti.biz/faq/set-environment-variable-linux/
After it do not forget to restart java (or IDE), as they get environments, when started.
Is there any easy way to get the host name in spring configuration file ? Currently I am using Java code to get the host name and and auto wire the property in the bean . But looking for less coding approach if any !
Thanks
The following will give you the hostname in java
return InetAddress.getLocalHost().getHostName();
where InetAddress belongs to the java.net package. You can add that to your java configuration file. If you want to do it in xml, you can do the following
<bean id="localhostInetAddress"
class="java.net.InetAddress"
factory-method="getLocalHost"/>
<bean id="hostname"
factory-bean="localhostInetAddress"
factory-method="getHostName"/>
When my FTP mput transfer is successfully complete, I want to rename the file in the local directory. For this, I need to use the local dir path from PropertyPlaceholderConfigurer. But this doesn't seem to be working. Please can you suggest the syntaxt to expand the value of the property? ${local.request.dir} represents a directory path like /home/jainr/REQUEST.
<int-ftp:request-handler-advice-chain>
<bean id="requestFileRename" class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="trapException" value="true" />
<property name="onSuccessExpression" value="T(org.apache.commons.io.FileUtils).moveFile(new java.io.File(#{${local.request.dir}} + '/' + headers['RequestFileName']), new java.io.File(#{${local.request.dir}} + '/' + headers['RequestFileName'] + '.processed'))" />
</bean>
</int-ftp:request-handler-advice-chain>
You need to define the resolved placeholder as a literal, from a SpEL perspective...
new java.io.File('${local.request.dir}/' + headers...
Notice I also removed the #{...} - that is initialization time SpEL - this is runtime SpEL.
In future, instead of statements like...
But this doesn't seem to be working.
...provide the error message and/or stack trace.
I am creating a spring web project where i am uploading a csv file and saving it to database. I need to keep the file in the relative path of the project so that it can be accessed through the url.for example: localhost:port/project_name/file_name
But I am getting the absolute path everytime using servlet context or URL.
Please help me out to get the relative path in spring controller.
You can save the file wherever you want. I particularly create a folder in the tomcat's directory and access it through the Java System Property System.getProperty("catalina.base");
Then to the url you can choose one of these possibilities:
Create a controller that serves the file.
Declare an Context in tomcat: option1 or option2
For example, I saved the file in:
System.getProperty("catalina.base")+File.separator+"mydata"+File.separator+filename;
I can create the controller:
#Controller
public class MyDataController {
#RequestMapping("/mydata/{filename}")
public String helloWorld(#PathVariable("filename") String filename) {
String path = System.getProperty("catalina.base")+File.separator+"mydata"+File.separator+filename;
return new FileSystemResource(new File(path));
}
}
or declare a context in tomcat create the file: [tomcat6directory]/conf/Catalina/localhost/appcontext#mydata.xml containing
<Context antiResourceLocking="false" privileged="true" path="/mydata" docBase="${catalina.base}/mydata" />
Issue: Using spring batch, i need to read a file which has todays date. E.g test_02032015.txt.This file will be in a directory /test/example. Its an unix environment that i need to fetch file from.
question is how to configure spring batch xml so that above mentioned file is read
Any pointers to relevant website or solution would be of great help.
You have a few ways to address a requirement like this:
If you don't need to worry about the other files in a directory, you can just use a wild card in the file name like this:
<property name="resource" value="data/iosample/input/*.xml" />
Another alternative would be to pass the value into the job as a parameter and reference it like this:
<property name="resource" value="#{jobParameters['input.file']}" />
Finally you could use SpEL to build the file name (Sorry I don't have an example of that handy).