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
Post a Comment