简体   繁体   中英

Accessing folders outside application package (.ear/.war file)

Is there any way of making a folder that is NOT packaged in the war file accesible via GET.

Probably setting something in web.xml ?

Yes, you can use the alternatedocroot property (in Glassfish) to serve files (like images) from outside the war.

This property can be a subelement of a sun-web-app element in the sun-web.xml file or a virtual-server element in the domain.xml file

See here: http://docs.sun.com/app/docs/doc/820-4496/geqpl?l=en&a=view

example:

<property name="alternatedocroot_1" value="from=/images/* dir=/usr/gifs"/>

You could add a servlet to your application which reads the file.

Example (needs error handling)

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" );
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM