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

3 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

Blogroll

Follow this blog by Email

Popular Posts