on 01-07-2011 4:20 PM
Hi,
I'd like to have one Web Dynpro component to read the content of a text file.
Where can I place the text file in order to read it from the component controller and what code do I have to write in order to get a Java standard File instance that points to that file?
I've seen other threads around and I've tried to follow the advices posted to no avail.
(e.g. : place the file under mimes/Components/...)
Thank you,
Pietro
Hi Pietro,
You can store the file in km and read the content of it using below code. Below code will give you the content of txt file in a string array.
You ll need certain jar files to use KM api , these jar files are available in the plugin folder of your nwds installation.
Also define sharing referece in webdynpro references as PORTAL:sap.com/com.sap.km.application
//******* Read file from KM
String repository_km;
String FileURL;
repository_km="/documents/data/data.txt";
try
{
//Getting the user......
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
IUser sapUser = wdClientUser.getSAPUser();
com.sapportals.portal.security.usermanagement.IUser ep5User =
WPUMFactory.getUserFactory().getEP5User(sapUser);
//Getting the Resource.........
IResourceContext resourseContext = new ResourceContext(ep5User);
IResourceFactory resourseFactory = ResourceFactory.getInstance();
//path to the KM Folder ("/documents/data/data.txt")
RID rid= RID.getRID(repository_km);
com.sapportals.wcm.repository.IResource resource =
resourseFactory.getResource(rid, resourseContext);
if (resource != null)
{
String text = "";
BufferedReader in = new BufferedReader(new InputStreamReader(resource.getContent().getInputStream()));
int count=0;
while ((text = in.readLine()) != null)
{
strText[count] =text;
count++;
}
}
}
catch (RuntimeException e2) {
wdComponentAPI.getMessageManager().reportException("Error in reading file from KM : "+e2.getMessage(),true);
// TODO Auto-generated catch block
}
You can also store the file in your project folders then u ll need to modify the project to change the content of txt file. Hence better way would be to keep it in KM.
Best Regards
Deepak
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your answer Deepak, but I don't know what KM is and where to place the file... (/documents/data is a relative path but what is the local root directory?).
Isn't there a way to put the text file inside the source directory of the component?
(i.e. under Project/src directory?)
Thank you,
Pietro
Hi Pietro,
if you don't have KM configuerd in EP then you can follow the bellow steps to acive this.
1. place the file in any of the directory in server.
2. Login to the J2EE engine Visual administrator by running the batch file in the following location (on a windows machine).
$:\usr\sap\<SID>\JC00\j2ee\admin\go.bat
Select the u201CHTTP Provideru201D and Add a new alias.
For eg: I have created an alias u201Ctestu201D and mapped it to the path u201CE:\test folder\text.txtu201D.
3. You will like to get the URL link as dynamically
// code to get URL dynamically
String resultUrl = null;
try
{
String str = WDURLGenerator.getAbsoluteWebResourceURL("/");
if (str != null)
{
URL url = new URL(str);
resultUrl = url.getProtocol()+ "://"+ url.getHost()+ ":"+ url.getPort();
File filename = new File(resultUrl+"/test/text.txt");
// Write the code to read content from txt file
.
.
.
.
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
wdComponentAPI.getMessageManager().reportException("Problem in getting URL",true);
}
revert back if you have any quaries.
Thanks & Regards,
Bhargava
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Pietro,
it is very simple thing if you follow the steps i had mentioned.
Ask your basis persion to do the following steps:
1. create a folder in any directory availbale in server and ask him/her to put your text file in that folder.
2. create the alias for that folder in visual admin tool under Globalconfigarations>HTTP Provider->Virtual Hosts>defult>Aliases> specify the alias name and path of the folder(which is holding the text file)->click on ADD.
then wirte the code mentioned in previous replay for this thread.
Thanks & Regards,
Bhargava
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.