Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件保存到在同一Tomcat实例中运行的另一个web应用程序(不工作)_Java_Tomcat_Servlets - Fatal编程技术网

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

Java 将文件保存到在同一Tomcat实例中运行的另一个web应用程序(不工作),java,tomcat,servlets,Java,Tomcat,Servlets,我在同一个tomcat实例上运行了3个web应用程序,其中一个应用程序(ZaapMartAdmin)用于管理员将文件上载到另一个应用程序的(ImageUploads)目录,而第三个应用程序(ROOT)只是读取上载到(ImageUploads)的文件并从那里显示图像文件。 我从、和那里得到了一些想法 以下是我的servlet中的相关代码(请注意:servlet正在ZaapMartAdmin上运行): /。。。 字符串文件名=generateFileName(请求)//生成图像文件名 字节[]图像字

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

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

/。。。
字符串文件名=generateFileName(请求)//生成图像文件名
字节[]图像字节=getByteArray(请求)//生成图像字节
//在何处保存图像部分
ServletContext adminContext=request.getServletContext()//ZaapMartAdmin上下文
ServletContext rootContext=adminContext.getContext(“/”)//根上下文
ServletContext uploadsContext=rootContext.getContext(“/ImageUploads”)//ImageUploads上下文
字符串absolutePath=uploadsContext.getRealPath(“”);
File imagesDirectory=新文件(绝对路径+File.separator+图像);
如果(!imagesDirectory.exists())
imagesDirectory.mkdir();
试试(FileOutputStream fos=newfileoutputstream(绝对路径+File.separator+“图像”+File.separator+文件名);)
{

fos.write(imageBytes);//在对about上下文做了更多的研究之后,我能够推断出问题的解决方案是在我的tomcat/conf/server.xml文件中的主机标记中添加一个新的上下文标记:

      <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>

www.admin.zaapmart.com
我将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 ...
}
//...
/。。。
String fileName=generateFileName(请求);//生成图像文件名
byte[]imageBytes=getByteArray(请求);//生成图像字节
//在何处保存图像部分
ServletContext adminContext=request.getServletContext();//ZaapMartAdmin context
ServletContext uploadsContext=adminContext.getContext(“/ImageUploads”);//ImageUploads context
字符串absolutePath=uploadsContext.getRealPath(“”);
File imagesDirectory=新文件(绝对路径+File.separator+图像);
如果(!imagesDirectory.exists())
imagesDirectory.mkdir();
试试(FileOutputStream fos=newfileoutputstream(绝对路径+File.separator+“图像”+File.separator+文件名);)
{

fos.write(imageBytes);//只需将文件保存在外部目录中。这样,如果重新部署应用程序,将不会删除文件。这将非常棒,但我如何在不通过应用程序的情况下执行此操作?您是否尝试过使用绝对路径,如:''new File(/var/tomcat/data/images);'?
//...
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 ...
}
//...