Jboss image upload and http access to show image--reference

question

I am uploading images to jboss server by getting the absolute path using the following code

getServletContext().getRealPath("");

The uploaded image is moved to the absolute path and I can access the image usinghttp://test.com:8080/image.jpg

My problem is the image is being uploaded to the tmp directory of jboss server, so i am losing the uploaded images in the next deployment. I tried uploading the image to various paths to make it work \jboss-5.0.1.GA\server\default\deploy and here \jboss-5.0.1.GA\server\default\work\jboss.web\localhost as well But fails, I cannot access the image using http://test.com:8080/image.jpg

 

Answer

You can add a new context to specify a path to access an external folder.

Steps for Jboss 4 and older versions:

  1. Open your file /YOURINSTANCE_JBOSS/deploy/jboss-web.deployer/server.xml.
  2. Define a new Context in the tag <Host name=”localhost” ...>

    Example:

    <Host name=”localhost ...>
    <Context path=”/myfolder docBase=”/home/username/my_images reloadable=”true”></Context>

    Where /myfolder will be the path that you are going to use to access your files, and/home/username/my_images the folder where you are going to upload your pictures.

  3. Restart JBoss

Now you will be able to access your files with the next path:

http://yourserver:yourport/myfolder/filename

Steps for Jboss 5:

  1. Create a new file named context.xml into your WEB-INF folder with the next content:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context allowLinking="true" cookies="true" crossContext="true" override="true">
        <Resources allowLinking="true" className="YOUR_PACKAGE.MyResources" homeDir="/home/username/my_images" />
    </Context>

    Where className is the java class that will access the resources and homeDir your external directory.

  2. According to this link create a new class to access your resources defined in the file context.xml

    Example:

    public class MyResources extends FileDirContext {
    
    }

Now you will be able to access your files with the next function:

request.getServletContext().getResourceAsStream(uri);

Steps for Jboss 5 and older versions:

  1. Create a new file named context.xml into your WEB-INF folder with the next content:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context allowLinking="true" cookies="true" crossContext="true" override="true">  
        <Resources allowLinking="true" homeDir="/home/username/my_images" />  
    </Context>

    Where homeDir is your external directory.

  2. Create a symbolic link: YourDeployedProject.war/myfolder linked to /home/username/my_images

    Windows:

    mklink /D C:\YOUR_JBOSS_SERVER\server\default\deploy\YourDeployedProject.war\myfolder C:\users\YOURUSER\my_images

    Linux:

    YourDeployedProject.war# ln -s /home/username/my_images myfolder

Now you will be able to access your files with the next path:

http://localhost:8080/DeployedProject/myfolder/filename

Steps for Jboss 7:

JBoss 7 doesn‘t allow any of the methods for the previous JBoss versions, so the easiest solution is to implement a Servlet to access your files like in the next link.

 

来源:http://stackoverflow.com/questions/17359038/jboss-image-upload-and-http-access-to-show-image

 

 /**
* Servlet to serve files from a static directory
*/
public class FileServingServlet extends DefaultServlet
{
 
    public void init() throws ServletException {
 
        super.init();
      
        final Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(ProxyDirContext.HOST, resources.getHostName());
        env.put(ProxyDirContext.CONTEXT, resources.getContextName());
      
        final String docBaseProperty = getServletConfig().getInitParameter("docBaseProperty");
        if (docBaseProperty == null || docBaseProperty.trim().equals("")) {
                 throw new RuntimeException("docBaseProperty parameter must not be blank");
       }
      
       final String docBase = System.getProperty(docBaseProperty);
       if (docBase == null || docBase.trim().equals("")) {
                 throw new RuntimeException("docBase property " + docBaseProperty + " must be set");
       }
      
       final FileDirContext context = new FileDirContext(env);
       context.setDocBase(docBase);
      
       // Load the proxy dir context.
       resources = new ProxyDirContext(env, context);
 
 
       if (super.debug > 0) {
          log("FileServingServlet:  docBase=" + docBase);
      }
      
   }
   
}
 
 
 
Which I use like this in the web.xml
 
   <servlet>
        <servlet-name>fileServing</servlet-name>
        <servlet-class>xxxx.FileServingServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>docBaseProperty</param-name>
            <!-- Name of the system property containg the base dir -->
            <param-value>my.basedir.directory</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
 
    <servlet-mapping>
        <servlet-name>fileServing</servlet-name>
        <url-pattern>/directory/*</url-pattern>
    </servlet-mapping>
 
 
It maps to /directory the content of the local directory specified in the System property my.basedir.directory. I use such a property because I did not want to hard code the local directory in the web.xml as it can be different in various deployement context.

来源:https://community.jboss.org/thread/169647#639271

Jboss image upload and http access to show image--reference,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。