简体   繁体   English

Spring 3 mvc如何将图像上传到网络应用资源

[英]Spring 3 mvc how to upload an image to web app resources

I have a problem with an image upload in spring 3 mvc. 我在春天3 mvc上映像上传有问题。 I search a couple of hours but can't find any satisfying solution. 我搜索了几个小时但找不到任何令人满意的解决方案。

What I want to do? 我想做的事?

I simply want to upload an Image to the server and store it in a location where I can easily access it from my jsp sites to show the image and so on. 我只是想将一个Image上传到服务器并将其存储在一个我可以从我的jsp站点轻松访问它以显示图像等的位置。

I don't know, if use the wrong keyword but I can't find a good solution for it and I'm to new on spring to figure it out by my self ... 我不知道,如果使用错误的关键字,但我无法找到一个好的解决方案,我会在春天找到新的东西来解决它...

So the problem is not to store the file on the server, that problem I solved already but till now I can only save it to the ServletContext#getRealPath() and there I think I can't access the image in the jsp site. 所以问题不是将文件存储在服务器上,我已经解决了这个问题,但到目前为止我只能将它保存到ServletContext#getRealPath()中,我觉得我无法访问jsp站点中的图像。

Did have anyone a good idea how I can solve that or any hints to find a solution ? 有没有人知道如何解决这个或任何提示找到解决方案?

very thanks for your help and very sorry for my english ... it is quite late in germany -.- 非常感谢你的帮助,非常抱歉我的英语...在德国已经很晚了 - .-

According to me when you try to access the image in your jsp file it will give you 404 error. 据我说,当你尝试访问jsp文件中的图像时,它会给你404错误。

You need to include the following line of code in your servlet.xml file. 您需要在servlet.xml文件中包含以下代码行。

<mvc:resources location="/assets/" mapping="/assets/**" />

You just need to replace the path you have stored your uploaded images in. And then everything will work. 您只需要替换已存储上传图像的路径。然后一切都会正常工作。

Hope this helps you. 希望这对你有所帮助。

Cheers. 干杯。

thx for your help, my problem is how to get this path for saving my file to it. 为了你的帮助,我的问题是如何获得这个路径来保存我的文件。

here is a code snippet how I do it now (I found this way in several tuts, but i do not know, if this is a good way to save images) 这是一个代码片段,我现在是怎么做的(我在几个方面找到了这种方式,但我不知道,如果这是保存图像的好方法)

ServletContext context = RequestContextUtils.getWebApplicationContext(request).getServletContext();
    try {
        if(!image.isEmpty()) {
            validateImage(image);
            sizeChart.setImgPath(saveImage(context.getRealPath("/") + "/images/" + image.getOriginalFilename(), image));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM