HttpServlet返回错误的文件名但正确的文件

时间:2016-07-11 00:17:23

标签: java servlets gwt

我有一个运行下载servlet的GWT应用程序,它扩展了HttpServlet,以便为用户请求的文件提供服务。当我在本地以dev模式运行时,这很有效。但是,当我部署到远程服务器时,servlet总是用servlet的名称替换文件名。

例如,我想下载test.mp3文件。在开发模式下,servlet会查看请求的文件名,找到该文件,并在具有正确文件名的响应中将其提供回来。部署时,servlet在请求中看到自己的名称,设法找到正确的文件(以某种方式),并使用错误的文件名将其提供回来。

我承认我对网络开发比较陌生,但在8年的编程中我从未见过这样的东西。欢迎任何想法或解决方案,完整代码和web.xml。

DownloadService.java

public class DownloadService extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileName = req.getParameter("fileInfo1");
        System.out.format(req.getRequestURL().toString()); // WRONG HERE, always "downloadService", the name of the service in my web.xml
        System.out.format("Requested file " + fileName); // SAME WRONG STRING HERE
        File file = new File(fileName);
        System.out.format(file.getName()); // STILL THE SAME WRONG STRING

        int BUFFER = 1024 * 100;
        resp.setContentType("application/octet-stream");
        resp.setHeader("Content-Disposition", "attachment; filename=" + "\"" + file.getName() + "\""); //STILL WRONG!
        ServletOutputStream outputStream = resp.getOutputStream();
        resp.setContentLength(Long.valueOf(file.length()).intValue());
        resp.setBufferSize(BUFFER);

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream out = new BufferedOutputStream(outputStream);

        int count;
        byte[] buffer = new byte[BUFFER];
        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }

        out.flush();
        out.close();
        outputStream.flush();
        outputStream.close();
        System.out.flush(); // BUT I GET THE RIGHT FILE. HOW!?
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <servlet>
        <servlet-name>ServiceImpl</servlet-name>
        <servlet-class>
            com.frozeninferno.server.Impl
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServiceImpl</servlet-name>
        <url-pattern>/app/Service</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>downloadService</servlet-name>
        <servlet-class>com.frozeninferno.server.DownloadService</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>downloadService</servlet-name>
        <url-pattern>/app/downloadService</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>app</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
        <!--<user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>-->
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>app</realm-name>
    </login-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>
</web-app>

0 个答案:

没有答案