4

I'm coding a Spring MVC 3.0 application with Tomcat as the web server.

Our requirement is to let the user upload an image. I'm thinking of storing this image on the disk file system and store the reference path in MySQL instead of storing all the file info in MySQL database as BLOB (I was told storing in MySQL is not best practice).

Can any one recommend how to do this in Spring MVC?

Cheers

Charles
  • 50,943
  • 13
  • 104
  • 142
prashanth T
  • 91
  • 2
  • 2
  • 5
  • 1
    Do you want to use some other frameworks? Hibernate? Velocity? JSP? Your question is too general. Please let us know what you have already done. Give us some code snippets. Nobody will do the reasearch for you or write the whole functionality. – Adam Sznajder Dec 29 '12 at 20:19
  • There is no single solution, it depends on the current and future requirements. If you use a database for storing images, you can cluster your application (when load goes high) without any problem. Although some database maintenance difficulties will be introduced. – Amir Pashazadeh Dec 29 '12 at 20:37
  • It could be the best practice if you can manage and utilize the loaded data. With the file system it seems will be the big problema. – Roman C Dec 29 '12 at 21:19

1 Answers1

13

Storing in disk and storing in MySQL has its on caveats. Here is good discussion about it.

To store it in file system you can use Commons File Upload. Here is a sample

pom.xml

     <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>${release.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${release.version}</version>
    </dependency>

JSP

<h2>Spring MVC file upload example</h2>

<form method="POST" action="<c:url value='/upload' />"
    enctype="multipart/form-data">


    Please select a file to upload : <input type="file" name="file" />
    <input type="submit" value="upload" />

</form>

Controller

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFormUpload( 
    @RequestParam("file") MultipartFile file) throws IOException{
if (!file.isEmpty()) {
 BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
 File destination = new File("File directory with file name") // something like C:/Users/tom/Documents/nameBasedOnSomeId.png
 ImageIO.write(src, "png", destination);
 //Save the id you have used to create the file name in the DB. You can retrieve the image in future with the ID.
 }  
}

And define this in your application context

 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

I hope this helps.

Community
  • 1
  • 1
shazinltc
  • 3,616
  • 7
  • 34
  • 49
  • But when you save the file in C:/userDir, this image can just be found when the webapp is opened from this computer.. Or do i miss here sth? In a webshop-app where admins add new products with their images, the images must be visible for every customer. Do i not have to save images in DB in this case? – akcasoy Dec 27 '14 at 09:52
  • when I say c:/userDir, I mean the folder in the server, not in the local machine. Regarding where to store (on the machine or the DB) is altogether a different topic to discuss. You can find lot of posts on SO on that. – shazinltc Dec 30 '14 at 06:05