简体   繁体   中英

can't access file resources in a servlet using Netbeans 7.1.2

I have Netbeans 7.1.2 running and I am trying to access some text files within a servlet:

package com.optimizations.cutting;

@WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
public class Servlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    System.out.println("in servlet "+System.currentTimeMillis());

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {

        DataManager dm = new DataManager();
        SheetInfo si = dm.loadSheetInfoCSV("sheetInfo.csv");
        ArrayList<Piece> pieces = dm.loadPiecesCSV("res/pieces4.csv");
....

I have placed the sheetInfo.csv and the pieces4.csv files everywhere i could think of, trying to acces them with a backslash ahead ( /sheetInfo.csv or /res/pieces4.csv )

when i say "everywhere i could think of" i mean : current directory (source packages), next to Servlet.java and all the other files I created (including DataManager.java which uses it). I also did the "Add folder" in the Properties window -> Sources -> Package Folder . (added 2 folders, just to make sure ). So my dear files are in 3 places all at once:

  • src/java/com/optimizations/cutting next to Servlet.java and DataManager.java

  • src/java/res

  • src/resources but i stll get the

     SEVERE: java.io.FileNotFoundException: resources/pieces4.csv (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:97) at java.io.FileReader.<init>(FileReader.java:58) at com.optimizations.cutting.DataManager.loadPiecesCSV(DataManager.java:98)` 

I have also restarted the server (Glassfish 3.1.2)

(maybe this seems silly but I also need to know where and how should i place my files so they can be accessed from both the client and the server - my servlet will create some images(.jpg) and store them (where?) and will send the filenames back to a .jsp which will then show them in a colorbox)

thanks in advance.

edit

added some more lines of the error and the call in DataManager.java:

public SheetInfo loadSheetInfoCSV(String filename){
    ....
    br = new BufferedReader( new FileReader(filename));
    String strLine = "";            

    //read comma separated file first line
    if ((strLine = br.readLine()) != null)
    ....

The exception suggests that you were using FileInputStream to get an InputStream of it. This is not the right way when the resource concerns a classpath resource (all places where you attempted to put it in are part of the classpath). You should get a classpath resource as a classpath resource using ClassLoader#getResourceAsStream() , not as a local disk file system resource using FileInputStream .

If the resource file foo.ext is placed in the same package as the class where you're trying to load the resource (ie the DataManager class), then you can just use get an InputStream of it by its sole filename as follows:

InputStream input = getClass().getResourceAsStream("foo.ext");

Or when you're inside the static context:

InputStream input = DataManager.class.getResourceAsStream("foo.ext");

If the resource foo.ext is placed in a different package than the class where you're trying to load the resource, eg the com.example package, then you can get an InputStream of it by its classpath-relative path as follows, where the leading slash / takes you to the root of the classpath:

InputStream input = getClass().getResourceAsStream("/com/example/foo.ext");

Also here, the getClass() can be substituted by an arbitraty Foo.class , as long as the class is loaded by the same ClassLoader which has access to the same package structure.

An alternative is to use the context ClassLoader as obtained from the current thread. It has access to everything . You only can't specify a classpath-relative path, it is always relative to the root of the classpath (so no leading slash / should be used):

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/example/foo.ext");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM