Struts2:如何在Webapp外部存储图像并将其路径保存到db? [英] Struts2: How to store images outside of the webapp and save its path to the db?

查看:75
本文介绍了Struts2:如何在Webapp外部存储图像并将其路径保存到db?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我确实将图像保存到webapp目录中,并将其保存到数据库中.

Until now I did saving image into the webapp directory and its path into database.

但是现在我正在尝试将图像保存在webapp之外,这样,如果我部署新的war文件,则不会删除旧的files文件夹.

But now am trying to save the image outside of the webapp so that if I deploy my new war files then my old files folder will not be deleted.

从下面的代码中,我的图像文件已正确保存到webapp外部的指定文件夹中,但是我不知道如何将图像检索到我的jsp页面中.

From my below code my image file is correctly saving into the specified folder outside of the webapp but i don't know how to retrieve that image into my jsp page.

我这样尝试过

<img src="www.myproject.com/struts2project/files/smile.jpg/>"

但这是错误的.我没有将图像显示在我的jsp页面中.

but this is wrong. I am not getting my image to be display into my jsp page.

下面的代码可以很好地将图像上传到绝对路径,但是我的问题是如何检索该图像?

Below code is working fine for uploading image into absolute path but my problem is how to retrieve that image?

   `fileSystemPath= "/files"; 
         try{                
             File destFile  = new File(fileSystemPath, thempicFileName);
                 FileUtils.copyFile(thempic, destFile);
                 String path=fileSystemPath+"/"+thempicFileName; 
                 theme=dao.getThemeById(themId); 
                 theme.setThemeScreenshot(path);
                 theme.setThemeName(theme.getThemeName());
                 theme.setThemeCaption(theme.getThemeCaption());
                 dao.saveOrUpdateTheme(theme);

         }catch(IOException e){
            e.printStackTrace();
            return INPUT;
         }`

请帮助我...

希望我能清楚我的需要,如果不是的话,请告诉我,我会尝试用另一种方式解释.

I hope I'm being clear on what I need, let me know if I am not and I'll try to explain in another way.

推荐答案

如您所说. . . 这个问题描述您需要执行的操作.我想您需要了解的是如何通过Struts 2最佳地实现这一目标.这是正在发生的事情.

As you say . . . this question describes what you need to do. I guess what you need to know is how to best achieve this with struts 2. Here's what's going on.

在您的标签中:

该URL被路由到您的struts 2应用程序.正确的?上下文是"struts2project".
所提到的问题提供的解决方案之一是使用Tomcat的功能来处理静态请求,并配置tomcat来了解保存图像的其他文档根目录.我认为这是一个很好的解决方案.

That url is being routed to your struts 2 application. Correct? The context is "struts2project".
One of the solutions offered by the referenced question is to use Tomcat's ability to serve static requests and configure tomcat to know about this other document root that holds your images. I think this is a great solution.

如果您想将其保留在struts2内,我认为您最好的选择是使用专用的从其他地方进行图像流传输"操作,该操作将输入流传输到图像,然后使用Struts2 流结果结果类型.该结果类型使您可以指定临时输入流.它还可以帮助您设置适当的标题.请注意,该文档页面上的标题值用于下载文件,因此您不需要这些值.我认为,它们将迫使浏览器打开图像的另存为对话框.

If you want to keep it inside of struts2, I think you're best option is to use a dedicated "image streaming from that other place" action that get's an InputStream to the image, then uses the Struts2 Stream Result result type. That result type lets you specify an adhoc InputStream. It also helps you set the appropriate headers. Note, the header values on that documentation page are for downloading the file, so you don't want those values. They would force the browser to open a save as dialog for the image, I think.

这篇关于Struts2:如何在Webapp外部存储图像并将其路径保存到db?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆