访问应用程序包(.ear/.war文件)外部的文件夹

3
有没有办法使未被打包在war文件中的文件夹可以通过GET请求访问?可能需要在web.xml文件中进行设置。
2个回答

4
是的,您可以使用alternatedocroot属性(在Glassfish中)来从war外部提供文件(如图像)。

此属性可以是sun-web.xml文件中sun-web-app元素的子元素,也可以是domain.xml文件中virtual-server元素的子元素。

请参见:http://docs.sun.com/app/docs/doc/820-4496/geqpl?l=en&a=view 例如:
<property name="alternatedocroot_1" value="from=/images/* dir=/usr/gifs"/>

1
您可以向应用程序添加一个servlet来读取文件。
示例(需要错误处理)
public class FileDownloadServlet extends HttpServlet {

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String filename = request.getParameter( "filename" );
        InputStream is;
        try {
            is = FileUtils.openInputStream( new File( filename ) );
            byte[] buf = new byte[ 8192 ];
            int bytesRead;
            while ( ( bytesRead = is.read( buf ) ) != -1 )
                os.write( buf, 0, bytesRead );
        }
        catch( ... ) {
        }
        finally {
            is.close();
            os.close();
        }
        response.setContentType( "application/octet-stream" );
      }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接