By using ServletContext, We can get WebContent and WebInf folder paths. Here the trick is getting ServletContext in Restful web service. Find below example program
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;

@Path("readContent")
public class ContentResource {
/*
 * this class is for reading file content
 * REST Full web services
 */
    
    /*
     * This method is for reading files in WebContent
     * @return String
     */
    @GET
    @Path("webcontent")
    public String readWebContentFile(@Context ServletContext context, @QueryParam("file") String fileName){
        String fullPath = context.getRealPath("/"+fileName);
        return readTextFromFile(fullPath);
    }

    /*
     * This method is for reading files in WEB-INF folder
     * @return String
     */
    @GET
    @Path("webinf")
    public String readWebInfFile(@Context ServletContext context,@QueryParam("file") String fileName) {
        String fullPath = context.getRealPath("/WEB-INF/"+fileName);
        return readTextFromFile(fullPath);
    }
    
    /*
     * Read text from file
     * @param fileName
     * @return String
     */
    public String readTextFromFile(String fileName) {
        String result = null;
        String line = null;
        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            String text = "";
            while((line = bufferedReader.readLine()) != null) {
                text += "\n"+line;
            }   

            result = text;
            // Always close files.
            bufferedReader.close();
        }
        catch(FileNotFoundException ex) {
            result = "Unable to open file";                
        }
        catch(IOException ex) {
            result = "Error reading file";
        }
        return result;
    }
}

7 comments:

  1. Now its the time when you must come here and read all the web content files. And also come and read the webinf file due to its assignment help website and hence that thing were best for that time. We think more for that time where we can do many things for that reading file.

    ReplyDelete
  2. If you happen to look for a writing help then it's better to apply to the essay writing service for help the difference between apa and mla

    ReplyDelete
  3. Much appreciate the blog! Great site! It looks extremely good! Good job!| you are best man…! Yuri Shafranik
    I am looking for some good blog sites for studying. I was searching over search engines and found your blog site
    Yuri Shafranik
    I am glad that I found your blog. It is very informative and you put your maximum into it.
    Yuri Shafranik
    I found your website via Google whilst searching for a similar subject, your website came up, it looks good. I’ve bookmarked it in my google bookmarks.
    Yuri Shafranik

    ReplyDelete
  4. Hey there, You have done a fantastic job. I’ll definitely dig it and personally suggest it to my friends. I am confident they will be benefited from this web site
    Yuri Shafranik
    It is my pleasure to say something about the author. He did deep a awesome job
    Yuri Shafranik
    I’m usually blogging and I actually appreciate your content. The article has genuinely peaks my interest. I’m going to bookmark your web page and maintain checking for new details. I am looking for some good blog sites for studying. I was searching over search engines and found your blog site. Well, I like your high-quality blog site design plus your posting abilities. Keep doing it.
    Yuri Shafranik
    Great post. Articles that have meaningful and insightful comments are more enjoyable, at least to me. It’s interesting to read what other people thought and how it relates to them or their clients, as their perspective could possibly help you in the future.
    Yuri Shafranik

    ReplyDelete
  5. I think this is one of the most significant pieces of information for me. And I’m glad read your article. Thank you for sharing!
    Yuri Shafranik
    This article have all information that someone can easily grab Yuri Shafranik
    I have never seen such a great article in my life. Good work Yuri Shafranik
    You prescribe the subject in very well manners Yuri Shafranik

    ReplyDelete
  6. While most employers believe they have a responsibility for the health and wellbeing of their workforce, 4 in 10 employers do not feel this responsibility extends to their employees’ MAURICE ROUSSETY FINANCIAL financial wellbeing.

    ReplyDelete
  7. On a day-to-day level, the top drivers of poor MAURICE FINANCIAL financial health include worries about the everyday costs of living, like paying for groceries and utilities; having or getting into debt; threats to job security,

    ReplyDelete

Blogroll

Popular Posts