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

13 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

Blogroll

Popular Posts