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

Source: Internet
Author: User
Tags jboss jboss server

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 was 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.g A\server\default\work\jboss.web\localhost as well fails, I cannot access the image using HTTP://TEST.COM:8080/IMAGE.J Pg

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 in the Context tag<Host name=”localhost” ...>

    Example:

      <host  Name= "localhost Span class= "pun" >...><context path = "/myfolder"  Docbase= "/home/username/my_ Images " Reloadable=" true  "></context>        

    Where /myfolder 'll be is the path that is going to use to access your files, and/home/username/my_images< /c1> the folder where you were going to upload your pictures.

  3. Restart JBoss

Now you'll 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 into context.xml your WEB-INF folder with the next content:

    <?XML version="1.0"Encoding="UTF-8"?><Context Allowlinking= "true"  cookies = "true"  Crosscontext= Override= "true" >  <resources allowlinking = "true"  Classname= " Your_package. MyResources " Homedir="/home/username/my_images "  /></context< Span class= "pun" >>              

    Where ClassName is the Java class, that would access the resources and homedir your external directory.

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

    Example:

    public class MyResources extends FileDirContext {}

Now you'll 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 into context.xml your Web-inf folder with the next content:

    <?XML version="1.0"Encoding="UTF-8"?><context Allowlinking= "true"  Cookies=  "true"  Crosscontext= "true"  Override= "true" > Span class= "pun" ><resources Allowlinking=  "true"  Homedir= /> </ 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'll 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 in the previous JBoss versions, so the easiest solution are to implement a Servle T to access your files like in the next link.

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

 /*** Servlet to serve files from a static directory*/ Public classFileservingservletextendsdefaultservlet{ Public voidInit ()throwsservletexception {Super. Init (); Finalhashtable<string, object> env =NewHashtable<string, object>();        Env.put (Proxydircontext.host, Resources.gethostname ());              Env.put (Proxydircontext.context, Resources.getcontextname ()); FinalString Docbaseproperty = Getservletconfig (). Getinitparameter ("Docbaseproperty"); if(Docbaseproperty = =NULL|| Docbaseproperty.trim (). Equals ("")) {                 Throw NewRuntimeException ("Docbaseproperty parameter must not being blank"); }             FinalString docBase =System.getproperty (Docbaseproperty); if(DocBase = =NULL|| Docbase.trim (). Equals ("")) {                 Throw NewRuntimeException ("docBase" + Docbaseproperty + "must be set"); }             FinalFiledircontext context =NewFiledircontext (env);             Context.setdocbase (docBase); //Load the proxy dir context.Resources =NewProxydircontext (env, context); if(Super. Debug > 0) {log ("Fileservingservlet:docbase=" +docBase); }}} which I use like ThisIn the Web .<servlet> <servlet-name>fileServing</servlet-name> <servlet-class>xxxx. fileservingservlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</p aram-value> </init-param> <init-param> &LT;PARAM-NAME&GT;LISTINGS&LT;/PARAM-NAME&G            T <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&gt    ; </servlet> <servlet-mapping> <servlet-name>fileServing</servlet-name> <url-p Attern>/directory/*</url-pattern> </servlet-mapping> It maps to/directory The content of the local directory specified I n the System property my.basedir.directory. I use such a property because I do not want to hard code the local directory in the Web. As it can be different in Var IOUs deployement context.

Source: https://community.jboss.org/thread/169647#639271

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.