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;
    }
}

18 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. It’s good to share information with others. I am also sharing something very important for students seeking MATLAB Assignment Help. Visit MatlabSolutions and get best assistance in your MATLAB, Simulink Assignments and Projects.

    You can also get CDR Report Engineers Australia if you look to immigrate to Australia.

    ReplyDelete
  3. Sharing information with others is good. I also want to share some good links for students all across the world. There is a team providing assignment help , which includes writing help service in different subjects. Like as Mechanical Engineering Assignment Help , Electronics Engineering Assignment Help, MATLAB Assignment Help , Civil Engineering Assignment Help , Computer Science Assignment Help

    ReplyDelete
  4. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Training in Chennai

    JavaScript Training in Chennai

    ReplyDelete
  5. 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
  6. This post is quite informative. I love reading your blogs. Quite helpful. Are you also searching for Academic content writing services we are the best solution for you. We are best known for delivering the best services to students without having to break the bank.

    ReplyDelete
  7. Impeccable.It is soo informative. Are you also searching for cheap assignment writers we are the best solution for you. We are best known for delivering the best services to students without having to break the bank

    ReplyDelete
  8. I was searching for many blogs site and now finally I have got this cool place with lots of information
    Father George Rutler

    ReplyDelete
  9. Thanks for sharing information with us…satisfied after visiting your blog…. Learn the latest trends & their practical application by experts with us…you can contact us click on
    Father George Rutler

    ReplyDelete
  10. Thanks for your great post. This post obviously very helpful. Already I'm bookmark this website. Again Thanks .please visit
    Father George Rutler

    ReplyDelete
  11. Wow, Excellent post. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative. please visit
    Father George Rutler

    ReplyDelete
  12. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post.
    Father George Rutler

    ReplyDelete
  13. This is a brilliant blog! I'm very happy with the comments!..
    Father George Rutler

    ReplyDelete
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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