简体   繁体   中英

How to save Image path in database

I am working on web application in which user can upload a image say at(D:\\media). I have added in host param of my server.xml of tomcat

<Context docBase="D:\media" path="/media" />

So now every Image at"D:\\media" can be viewed at "localhost:8080/media" as(take example of abc.png)

localhost:8080/media/abc.png

I am just showing image in jsp when required as

<img src="localhost:8080/media/abc.png">

my question is how and which imagepath to store in database when user uploads image

You can save the image right to the database from the servlet that process the post request. You must use this attribute in the form: enctype='multipart/form-data' and then get the Part with its parameter name like this:

//request in the HttpServletRequest.
Part uploadedFile = request.getPart("parameterName");
InputStream is = uploadedFile.getInputStream();
byte[] fileData = new byte[add a preferred size];
is.read(fileData);
//Save fileData in the database.

Here you can have more info with examples: http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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