java - Upload and display image in Javaweb -


my simple website using spring mvc. user uploads product images , thewebsite display image (with product name , price, etc..). database store details of image, , store image in directory. problem is, not sure image store should be.

// creating directory store file             file dir = new file(imagedirectory);             if (!dir.exists())                 dir.mkdirs();              // create file on server             file serverfile = new file(dir.getabsolutepath()                     + file.separator + name); 

for example, when value of variable imagedirectory "\temp", application store image in "d:\temp" , don know how image url show on website

when value of variable imagedirectory "temp", application store image in "d:\soft\sts-bundle\sts-3.5.1.release\temp" , dont know how image url show on website

so should store upload image in , how uploaded image url (stored in database) ?

in order show image end user, must store relative web app.

the ideal approach have dedicated folder in webapp storing uplaods , sending relative url folder end user.

e.g.

  //request instance of httpservletrequest    file uploaddirectory = new file(request.getsession().getservletcontext().getrealpath("/uploads"));    if (!uploaddirectory.exists()) {       uploaddirectory.mkdirs();   }    file reportfile = new file(reportdirectory.getabsolutepath(), filename);    string requesturl = request.getrequesturl().tostring();   requesturl = requesturl.substring(0, requesturl.lastindexof("/") + 1) + "uploads/" + filename;   return requesturl; 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -