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

时间:2010-06-24 11:34:26

标签: java java-ee glassfish

是否有任何方法可以通过GET访问未在war文件中打包的文件夹。

可能在web.xml中设置了什么内容?

2 个答案:

答案 0 :(得分:4)

是的,您可以使用alternatedocroot属性(在Glassfish中)来提供战争以外的文件(如图片)。

  

此属性可以是a的子元素   sun-web.xml中的sun-web-app元素   文件或虚拟服务器元素   domain.xml文件

见这里: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 :(得分: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" );
      }
    }
相关问题