title: Accessing local/shared drive with Spring MVC
body: <p>I'm pretty new to Spring MVC and need to display contents of local folder (could be NAS too) in browser. The folder has PDF files, which I want to list as URL's. On clicking any of them I should be able to view the file in question in PDF reader. My application server is Tomcat.</p>
  
  <p>On running the app I can see the files as hyperlinks, but on clicking it gives HTTP 404. I think I'm missing out on some configuration somewhere. I've tried all possible settings and even searched for quite some time, but haven't got it to work yet.</p>
  
  <p>Can someone kindly let me know where I'm going wrong or what I could be missing?</p>
  
  <p>My dispatcher-servlet.xml looks like this:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.springapp.mvc"/&gt;
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/pages/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>index.jsp:</p>
  
  <pre><code>&lt;c:if test="${not empty dir}"&gt;
  &lt;ul&gt;
      &lt;c:forEach var="listValue" items="${dir}"&gt;
          &lt;%--&lt;li&gt;${listValue}&lt;/li&gt;--%&gt;
          &lt;%--&lt;li&gt;&lt;a href="${listValue}"&gt;${listValue}&lt;/a&gt;&lt;/li&gt;--%&gt;
          &lt;li&gt;&lt;a href="&lt;c:url value="${listValue}"/&gt;"&gt;${listValue}&lt;/a&gt;&lt;/li&gt;
      &lt;/c:forEach&gt;
  &lt;/ul&gt;
  &lt;/c:if&gt;
  </code></pre>
  
  <p>and the controller class:</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class DirListController extends AbstractController {
      private File[] getPdfFileListing() {
          File dir = new File("//Users//username//Documents"); // current directory
  
          return dir.listFiles(new FilenameFilter() {
              @Override
              public boolean accept(File dir, String name) {
                  return name.toLowerCase().endsWith(".pdf");
              }
          });
      }
  
      @Override
      @RequestMapping(method = RequestMethod.GET)
      protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
          //return back to index.jsp
          File[] files = getPdfFileListing();
          ModelAndView model = new ModelAndView("index");
          model.addObject("dir", files);
  
          return model;
      }
  }
  </code></pre>