Saving Image in Folder and path in database using Java
Here is the code to get the image from the user and store it in a folder and store the image path in Database.
JSP and Servlet
MySql Database
Tested with Netbeans 8.0
Database – create table with name and photourl as fields with datatype varchar().
JSP: (Upload Image)
<form action="UploadServlet" method="post" enctype="multipart/form-data"> <br><br> <table> <tr> <td>UserName: </td> <td width='10px'></td> <td><input type="text" name="unname"/></td> </tr> <tr> <td>Upload: </td> <td width='10px'></td> <td><input type="file" name="filecover" value="Upload"/></td> </tr> <tr> <td><input type="submit" value="submit"></td> </tr> </table> </form>
Servlet:
@MultipartConfig(fileSizeThreshold=1024*1024*2, maxFileSize=1024*1024*10, maxRequestSize=1024*1024*50) public class UploadServlet extends HttpServlet { private String getFileName(final Part part) { final String partHeader = part.getHeader("content-disposition"); for (String content : part.getHeader("content-disposition").split(";")) { if (content.trim().startsWith("filename")) { return content.substring( content.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out1 = response.getWriter()) { HttpSession session=request.getSession(); String name=request.getParameter("unname"); Part filePart = request.getPart("filecover"); String photo=""; String path="path here"; File file=new File(path); file.mkdir(); String fileName = getFileName(filePart); OutputStream out = null; InputStream filecontent = null; PrintWriter writer = response.getWriter(); try { out = new FileOutputStream(new File(path + File.separator + fileName)); filecontent = filePart.getInputStream(); int read = 0; final byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes)) != -1) { out.write(bytes, 0, read); photo=path+"/"+fileName; } Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("URL", "USERNAME", "PASSWORD"); Statement stmt=con.createStatement(); stmt.executeUpdate("insert into name(Name,photourl) values('"+name+"','"+photo+"')"); } catch(Exception e) { } out1.println ("<html><body><script>alert('Sucessfully Saved! Check the database and the path!');</script></body></html>"); } }
how can i do the same thing in spring hibernate…?
Hello, Sorry for late response..
I haven’t worked out on that yet, I will write a code and post it soon
hi, can you tell me how to update the image path
It can be done from database level, here we have used the query “Insert into” for updating the path you can use update query
if u have code can plz share with me
Hi thanks it worked.
But now how to retrieve this image from db and display it??
Hi Mansi,
You just retrieve the path like normal database retrieval, Once path is retrieved if you put the path within
it will get displayed as image
within Tag img src /Tag
can you show your table?
Thanks…
Sir I have used your code as mentioned above but at the time of retrieving image it wont display image on my jsp page….so please give me the solution….
here is my code for retrieving the image:
/*retrive.jsp */
Insert title here
<img src="” width=”300″ height=”300″ />
img src=”” –> you should include your path that is stored in database. (Example: C:/Photos/Image.jpg).
HTTP Status 405 – HTTP method POST is not supported by this URL
It says POST is not supported, the request is not called in the Servlet. Please post your code.
Do we have t give the file name also while setting the path.. ie path :// c:/image.jpg ?/?
C:/image.jpg path is enough
thanks sir it’s working
but only one confusion which folder image saved
means where is folder location of image
String path=”path here”, path can be mentioned here