1

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)
    ....

1 Answers1

1

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 (i.e. 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, e.g. 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");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ok..so i will edit to show the exact method i used to get the files (i did not directly use FileInputStream. Anyway... my wonder is why this method worked for a simple Java project (not web, but desktop). what i mean is that i tested the DataManager class in a simple desktop java project then just added it to my source package in the web proj. – claudia alexa Aug 25 '12 at 06:29
  • i will try what you suggested and my bet is that it will work :) . I still wish to understand the reason why that first one didn't, but the important thing is to get it working. – claudia alexa Aug 25 '12 at 06:43
  • okay the reading part worked just fine :). i need to get to the writing part (which of course fails right now (it uses File file = new File(filename); try { ImageIO.write(sheet, "png", file); – claudia alexa Aug 25 '12 at 07:08
  • (continuing.. since i am not alowed to edit comments after 5 min have passed).But I guess it's fixable in the same manner. ...Still why is it working on a desktop app (and not a web) ? and how do I write them to be seen by the client browser? (I'll start some googling anyway) – claudia alexa Aug 25 '12 at 07:16
  • You didn't mention that you need to be able to write to it. That drastically changes the answer. You should not put it in the classpath, but on a fixed location in the local disk file system. You can then use the `FileInputStream`/`FileOutputStream` the usual way (but with an absolute path!!). See also http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#2308388 To serve it to the world wide web, just add the location as virtual host to the server config, or create a servlet which streams it. – BalusC Aug 25 '12 at 12:22
  • thanks for the answer (btw.. i did mention i also need writing to some files, and that I am using a servlet for that: check the paragraph before "thanks in advance" :) ). – claudia alexa Aug 27 '12 at 06:28