java - 将文件保存到在同一 Tomcat 实例中运行的另一个 Web 应用程序(不工作)

标签 java tomcat servlets

我有 3 个网络应用程序在同一个 tomcat 实例上运行,一个应用程序 (ZaapMartAdmin) 用于管理员将文件上传到另一个应用程序的 (ImageUploads) 目录,而第三个应用程序 (ROOT) 只是读取上传到 (ImageUploads) 的文件并从那里显示图像文件。 我从 here 得到了一些想法, here , here , 和 here .

这是我的 servlet 中的相关代码(请注意:servlet 在 ZaapMartAdmin 上运行):

//...
String fileName = generateFileName(request);//Generate the image file name
byte[] imageBytes = getByteArray(request);//Generate the image bytes
//Where to save the image part
ServletContext adminContext = request.getServletContext();//ZaapMartAdmin context
ServletContext rootContext = adminContext.getContext("/");//ROOT context
ServletContext uploadsContext = rootContext.getContext("/ImageUploads");//ImageUploads context
String absolutePath = uploadsContext.getRealPath("");
File imagesDirectory = new File(absolutePath + File.separator + "images");
if(!imagesDirectory.exists())
            imagesDirectory.mkdir();
try(FileOutputStream fos = new FileOutputStream(absolutePath + File.separator + "images" + File.separator + fileName);)
{
      fos.write(imageBytes);//<-- store the image in the directory
      //... store file name to database ...
}
//...

从服务器端看我的目录结构是这样的:

enter image description here

现在的问题是,当我运行这个 servlet 时,文件将保存在“ZaapMartAdmin”目录中,而不是“ImageUploads”目录中。 而且它不会抛出任何异常。

我还在每个应用程序的 context.xml 中添加了 crossContext="true"

请问我这里做错了什么?

最佳答案

在对 tomcat documentation 做了更多研究之后关于 Contexts,我能够推断出问题的解决方案是在我的 tomcat/conf/server.xml 文件中的 Host 标签中添加一个新的 Context 标签:

      <Host name="admin.zaapmart.com"  appBase="webapps/zaapmart.com/ZaapMartAdmin"
        unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
        <Alias>www.admin.zaapmart.com</Alias>
        <Context path="" reloadable="true" docBase="/home/royalsee/tomcat/webapps/zaapmart.com/ZaapMartAdmin" crossContext="true"/>
        <!-- next line of code did the trick -->
        <Context path="/ImageUploads" reloadable="true" docBase="/home/royalsee/tomcat/webapps/zaapmart.com/ImageUploads" crossContext="true"/>
      </Host>

然后我将我的 servlet 代码修改为:

//...
String fileName = generateFileName(request);//Generate the image file name
byte[] imageBytes = getByteArray(request);//Generate the image bytes
//Where to save the image part
ServletContext adminContext = request.getServletContext();//ZaapMartAdmin context
ServletContext uploadsContext = adminContext.getContext("/ImageUploads");//ImageUploads context
String absolutePath = uploadsContext.getRealPath("");
File imagesDirectory = new File(absolutePath + File.separator + "images");
if(!imagesDirectory.exists())
    imagesDirectory.mkdir();
try(FileOutputStream fos = new FileOutputStream(absolutePath + File.separator + "images" + File.separator + fileName);)
{
      fos.write(imageBytes);//<-- store the image in the directory
      //... store file name to database ...
}
//...

之后我重新启动了 tomcat 服务器,一切都按照我想要的方式工作了!

关于java - 将文件保存到在同一 Tomcat 实例中运行的另一个 Web 应用程序(不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780257/

相关文章:

java - 使用 Maven、Jersey、Tomcat、@ApplicationPath、无 web.xml 调试 IncompatibleClassChangeError

java - 服务器运行java servlet

jsp - Servlet & Jsp - 谁能解释一下我的场景中发生了什么?

Java : What is the best way to check variable type in runtime?

java - Java中服务器断开连接

java - System.currentTimeMillis() 和 Date getTime() 的区别?

jsp - java servlet中的iText图像

java - Servlet 处理 applet 通信和 http 请求

java - 我已经安装了 Apache Tomcat 7,当我转到 http ://localhost:8080 in my browser, 时,我看到以下错误:HTTP Status 500

java - 如何从 Java 类访问 session