1

i want to upload image in spring mvc in file system. i can do it but when redeploy the project all image remove from directory. now my question is how upload image permanently?i want this in real application.

Emil
  • 423
  • 1
  • 12
  • 34

2 Answers2

0

You probably uploaded your images under your project directories/war. Every time you deploy/build your project/war, the images got deleted.

You need to save the image files outside of your project/war.

For real application, I suggest you at least save/serve your uploaded images in a separate server. Amazon S3 is a pretty good one. You can just store the object/file name relative to S3 base url in your database. They have java APIs for you to upload the files too.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
  • thanks for your answer. don't have other solution for this? i want like this, suppose my address is `www.example.com` so i want to have a directory like to this `www.example.com/upload/` and save all uploaded images there. – Emil Nov 03 '16 at 07:18
0

As mentioned, in order to persist these uploaded images it is best to save them outside of your project/.WAR file. The reason for this is as you've already experienced, each time you redeploy your application you will loose anything (i.e. your uploaded images) that had been written to the previous project/.WAR when it was deployed.

The provided solution of utilizing an Amazon S3 bucket to save these images is a good solution and you could definitely accomplish what you desire (having a URL of www.example.com/upload/ showing all these images). With springMVC within your controller you can set up a method and assign the @RequestMapping annotation like so:

@Controller
@RequestMapping("/") //this can map to your www.example.com
public class MainController(){

    @RequestMapping("/upload") //this will then map to www.example.com/upload
    public String showUploads(){        
        return "redirect:http://pathToAmazonS3Bucket"
    }
}

In SpringMVC the redirect allows you to redirect to an absolute URL path. See docs

But as already mentioned you still have to host your images somewhere outside of your project path/WAR file. Amazon S3 works, but since you asked for another solution here is one.

You can save all the images to a file on your PC, then run Python3 or Node as HTTP servers. This solution though requires you to utilize your computer and your internet connection to host your images on the web. This assumes you are good with leaving your PC running non-stop or have an old one laying around that you will use as your webserver. It also assumes your ISP is okay with you hosting a webserver on your network. Lastly you can obtain your own unique URL from numerous services online (some free and some for small fees), this way people dont have to type in your IP address.

I am running a similar setup above on my network with a free domain name from No-IP.com.

Also, how do you plan to host your spring web application on the web? Will you be doing this via an online hosting service or hosting yourself? If hosting yourself, will you use Apache Tomcat or Glassfish or another container/application server?

Community
  • 1
  • 1