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:
- Open your file
/YOURINSTANCE_JBOSS/deploy/jboss-web.deployer/server.xml. Define a new
Contextin 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.
Restart JBoss
Now you will be able to access your files with the next path:
http://yourserver:yourport/myfolder/filename
Steps for Jboss 5:
Create a new file named
context.xmlinto yourWEB-INFfolder 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.
According to this link create a new class to access your resources defined in the file
context.xmlExample:
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:
Create a new file named
context.xmlinto 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.
Create a symbolic link:
YourDeployedProject.war/myfolderlinked to/home/username/my_imagesWindows:
mklink /D C:\YOUR_JBOSS_SERVER\server\default\deploy\YourDeployedProject.war\myfolder C:\users\YOURUSER\my_imagesLinux:
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的更多相关文章
- JBoss集群中启用HTTPS协议
Generate server certificate Note: If you already have certificate created then this section can be i ...
- jboss eap6.1(1)
最近决定把公司的项目从jboss3.x迁移出来,先试着摸索一下最新的jboss服务器,从jboss官网上下了一份jboss-eap-6.1,然后找资料准备学习,同时将此次迁移过程记录下来,以备后续复习 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- Redhat Linux FTP配置
文件传输协议(FTP:FileTransfer Protocol)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接 ...
- 如何在CentOS 7上使用vsftpd设置ftp服务器
一.前言介绍 FTP(文件传输协议)是一种标准的客户机-服务器网络协议,允许用户在远程网络之间传输文件. 有几个开源的FTP服务器可用于Linux.最受欢迎和广泛使用的是pureftpd.proftp ...
- ehcache注解全面解析---打酱油的日子
通过ehcache以编程方式使用缓存: 跟上面的方式相同,但是缓存通过ehcache去管理,当然比使用map有N多种好处,比如缓存太大了快达到上限之后,将哪一部分缓存清除出去.这种方式完全是通过代码的 ...
- [salesforce] URLFOR function finally
While developing your Visualforce pages you may need to be able to obtain the URL of certain actions ...
- HttpClient通过GET和POST获取网页内容
中国银行支付网关---银行回调的接口 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 /** * 中国银行支付网关---银行回调的接口 * @svncode svn://10. ...
- A quike guide teaching you how to use matlab to read netCDF file and plot a figure
1. Preparation 2. A brief introduce to netCDF. 4 3. Data Structure. 4 3.1 Attrib ...
随机推荐
- uboot启动linux的过程
一.概述 linux内核镜像常见到的有两种形式,zImage和uImage.这两种文件的格式稍有差别,所以启动这两种格式的内核镜像也会有所不同.目前,uboot只支持启动uImage类型的镜像,对zI ...
- LightOj_1274 Beating the Dataset
题目链接 题意: 给一个文档, 这个文档由yes .no 组成, 共有s个byte, 共有n个yes.no. 假设yes的个数为yes_num, no的个数为no_num. 将这n个数进行排列, 对于 ...
- seajs配合spm应用之四弹出框
前面描述了 seajs的弹出遮罩层, 还没讲到弹出框, 这里接着把那几个例子介绍完. 目前已经有的工作是, 点击toggle按钮,可以弹出一个背投一样的暗灰色遮罩层, 主要的作用就是遮住当前页面上所有 ...
- [BZOJ 1006] [HNOI2008] 神奇的国度 【弦图最小染色】
题目链接: BZOJ - 1006 题目分析 这道题是一个弦图最小染色数的裸的模型. 弦图的最小染色求法,先求出弦图的完美消除序列(MCS算法),再按照完美消除序列,从后向前倒着,给每个点染能染的最小 ...
- Java学习IO篇
来吧,同志们,为复习网络编程做准备-- 一.理论准备 流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以"流" ...
- Codeforces Round #206 (Div. 2)
只会做三个题: A:简单题,不解释: #include<cstdio> using namespace std; int k,d; int main() { scanf("%d% ...
- net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案
我使用的net.sf.json是json-lib-2.4-jdk15.jar,把json对象转换为普通java实体对象时候有个问题,josn对象转换为java对象之后,json串里面的那几个小数点的值 ...
- 有感,懂市场比懂产品重要,懂产品比懂技术重要——想起凡客诚品和YY语音了
一个创业公司,最好三样都要有,但应该CEO是懂市场,经理懂产品,程序员最好懂技术厉害一点-这还不算,销售也要厉害一点,不能守株待兔- 美工——有钱最好请个美工,最起码也要请人设计修改一下- 财务——不 ...
- 关于Android4.2后WebView的js方法需要加@JavascriptInterface
解读: targetSdkVersion>=17时,需要加上@JavascriptInterface,否则报错Uncaught TypeError: Object [object Object] ...
- hadoop2.0安装和配置
hadoop2与hadoop1的配置有些许不同,最主要的是hadoop1里的master变成了yarn 这篇文直接从hadoop的配置开始,因为系统环境和jdk和hadoop1都是一样的. hadoo ...