使用Spring MVC访问本地/共享驱动器 [英] Accessing local/shared drive with Spring MVC

查看:52
本文介绍了使用Spring MVC访问本地/共享驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Spring MVC还是很陌生,需要在浏览器中显示本地文件夹的内容(也可能是NAS).该文件夹包含PDF文件,我想将其列为URL.单击它们中的任何一个,我都应该能够在PDF阅读器中查看有问题的文件.我的应用程序服务器是Tomcat.

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.

在运行该应用程序时,我可以将文件视为超链接,但是单击该文件将显示HTTP404.我认为我在某些地方缺少某些配置.我已经尝试了所有可能的设置,甚至搜索了一段时间,但还没有开始工作.

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.

有人可以让我知道我要去哪里或我可能会错过什么吗?

Can someone kindly let me know where I'm going wrong or what I could be missing?

我的dispatcher-servlet.xml看起来像这样:

My dispatcher-servlet.xml looks like this:

<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

Web.xml:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

index.jsp:

index.jsp:

<c:if test="${not empty dir}">
<ul>
    <c:forEach var="listValue" items="${dir}">
        <%--<li>${listValue}</li>--%>
        <%--<li><a href="${listValue}">${listValue}</a></li>--%>
        <li><a href="<c:url value="${listValue}"/>">${listValue}</a></li>
    </c:forEach>
</ul>
</c:if>

和控制器类:

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

推荐答案

一次访问整个目录可能无法正常工作.但是您可以做的是,如果使用ResponseEntity作为返回类型,则每个链接以byte数组的形式返回单个文件.然后,您可以在PDF查看器中打开它.

Accessing the whole directory at once probably isn't going to work. But what you can do, is return a single file per link as a byte array, if you use ResponseEntity as a return type. Then you can open it up in a PDF viewer.

在Spring 3.2.5,Tomcat 7上对此进行了测试,并添加了 Apache Commons IO 1.3.2 作为依赖项(用于FileUtils类).

Tested this with Spring 3.2.5, Tomcat 7 and added Apache Commons IO 1.3.2 as a dependency (for the FileUtils class).

private static final String DIRECTORY = "/Users/username/Documents/";

@RequestMapping(method = RequestMethod.GET, params = "fileName")
@ResponseBody
protected ResponseEntity<byte[]> handleFileRequest(HttpServletRequest request, HttpServletResponse response,
                                                   @RequestParam String fileName) throws Exception {
    // Change below to proper logger, if you have one configured.
    System.out.println("Trying to find file: " + fileName +
                       " from directory: " + DIRECTORY + ".");

    File pdfFile = new File(DIRECTORY + fileName);
    byte[] bytes = FileUtils.readFileToByteArray(pdfFile);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.setContentLength(bytes.length);
    headers.setContentDispositionFormData("filename", fileName);

    return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}

因此,您必须在URL中的链接中添加一个fileName请求参数,您可以在其中定义要调用的文件.例如:

Thus you'd have to add a fileName request parameter to the link in your URL, where you define which file to call. For instance:

<a href="http://localhost:8080/myApp?fileName=test.pdf">Open test.pdf</a>

这篇关于使用Spring MVC访问本地/共享驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆