eclipse 中的 Maven 为 data.csv 文件提供 filenotfound 异常

maven in eclipse giving filenotfound exception for data.csv file

我正在尝试通过在此处推荐一个短视频来尝试一个简单的电影推荐器 - https://www.youtube.com/watch?v=63k560Livmg . What I am trying diff is instead of console , I want recommendations to be displayed on browser so I am using servlets. But the problem is when i input a value(user id ) in my form, blank screen is shown . Please help what I am doing wrong. Dataset from here- https://mahout.apache.org/users/recommender/userbased-5-minutes.html。请参阅随附的 web.xml 和层次结构的屏幕截图。

index.jsp

    <html>
    <body>
     <h2>Hello World!</h2>
     <form action="demo" method="post">  
     loginID:<input type="text" name="name"/><br/>
     <input type="submit" value="login"/> 

    </form>
    </body>
    </html>

demo.java- 用于收集用户值并显示结果的servlet。

  package com.issac;

  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.ArrayList;
   import java.util.List;

  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;


import org.apache.mahout.cf.taste.recommender.RecommendedItem;

/**
* Servlet implementation class demo
 */
   public class demo extends HttpServlet {
   private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
      public demo() {
      super();
    // TODO Auto-generated constructor stub
    }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
      response.getWriter().append("Served at:   ").append(request.getContextPath());
   }

/**
 * @throws IOException 
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO Auto-generated method stub

        String n = request.getParameter("name");
        int k = Integer.parseInt(n);
        PrintWriter out = response.getWriter();
        List<RecommendedItem> recommendations = new ArrayList<RecommendedItem>  ();

       try {
          recommendations = App.getRecommend(k);
        } catch (Exception e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }

          for (RecommendedItem recommendation : recommendations) {
          out.println(recommendation);





  }

 }}

app.java - 生成推荐的逻辑

      package com.issac;

      import java.io.File;
      import java.util.List;

      import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
      import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
      import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
      import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
      import org.apache.mahout.cf.taste.model.DataModel;
      import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
      import org.apache.mahout.cf.taste.recommender.RecommendedItem;
      import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
      import org.apache.mahout.cf.taste.similarity.UserSimilarity;


      public class App 
 {
    public static List<RecommendedItem> getRecommend(int k) throws Exception
 {
    DataModel model = new FileDataModel(new File("data/dataset.csv"));
    UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
    UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1,  similarity, model);
 UserBasedRecommender recommender = new      GenericUserBasedRecommender(model, neighborhood, similarity);
      List<RecommendedItem> recommendations = recommender.recommend(k, 3);
      return recommendations;
     }
  }

eclipse 控制台的堆栈跟踪-

         INFO: Server startup in 15288 ms
          SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
          SLF4J: Defaulting to no-operation (NOP) logger implementation
      SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
      java.io.FileNotFoundException: data\dataset.csv
      at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init>   (FileDataModel.java:182)
      at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init>(FileDataModel.java:169)
      at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init>(FileDataModel.java:149)
      at com.issac.App.getRecommend(App.java:24)
      at com.issac.demo.doPost(demo.java:51)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt erChain.java:292)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

在 JEE 应用程序中,应通过 getResourceAsStream 方法读取文件:

try(InputStream input=getClass().getClassLoader().getResourceAsStream("data/dataset.csv")){
    ...
}

并且 data 文件夹应立即放置在 src/main/resources 内。

在您的情况下,还存在一个复杂问题:由于缺少来自 InputStream 的 DataModel 构造函数,我们需要先将 inputStream 的内容保存到一个(临时)文件中:

try(InputStream input=getClass().getClassLoader().getResourceAsStream("data/dataset.csv")){
    // Copy the contents of the inputStream to a File, to fix the constructor FileDataModel(file):
    File tmp=File.createTempFile("model-", "");
    tmp.deleteOnExit();
    InputStream input=new ByteArrayInputStream("enero".getBytes("ISO-8859-1"));
    Files.copy(input, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
    DataModel model = new FileDataModel(new File("data/dataset.csv"));
    ...
}

这就是我的结果。在得到 FileNotFound 异常后,我尝试了这个->

 ClassLoader classLoader = App.class.getClassLoader();
 DataModel model = new FileDataModel(new File(classLoader.getResource("data/dataset.csv").getFile()));

而不是

 DataModel model = new FileDataModel(new File("data/dataset.csv"));

我不完全了解它为什么起作用只是在互联网上找到它,与 class FileDataModel 的参数有关。欢迎任何人解释。