• 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.GAserverdefaultdeploy and here jboss-5.0.1.GAserverdefaultworkjboss.weblocalhost 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_SERVERserverdefaultdeployYourDeployedProject.warmyfolder C:usersYOURUSERmy_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

  • 相关阅读:
    Ext.window.Window在IE有部分半透明的问题
    匹配用户名的正则表达式
    PHP超时处理全面总结
    Extjs4 actioncolumn只能显示图标,不能显示文字的暂时解决方法
    SQL语句
    SQL Server 日期和时间函数
    asp.net 2.0中一次性更新所有GRIDVIEW的记录
    如何生成静态页面的五种方案[转]
    SQL IF..ELSE..在存储过程的使用规范
    数据库操作语句小结
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3849052.html
Copyright © 2020-2023  润新知