how to upload an image from a jsp page to a mysql database table using jspstrong text
Here is a jsp code that uploads an image into database.
1)page.jsp:
<%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST> <br><br><br> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center> <td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Send File"> </td> </tr> <table> </center> </FORM> </BODY> </HTML>
2)upload.jsp:
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <% String saveFile=""; String contentType = request.getContentType(); if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br><table border="2"><tr><td><b>You have successfully upload the file:</b> <%out.println(saveFile);%></td></tr></table> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/test"; ResultSet rs = null; PreparedStatement psmnt = null; FileInputStream fis; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "root"); File f = new File(saveFile); psmnt = connection.prepareStatement("insert into file(file_data) values(?)"); fis = new FileInputStream(f); psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); int s = psmnt.executeUpdate(); if(s>0){ System.out.println("Uploaded successfully !"); } else{ System.out.println("Error!"); } } catch(Exception e){e.printStackTrace();} } %>
thanks a lot........i got it...:) :) :) i really appreciate it.:)
request.getParameter is not working.
Hi. But how to get the values of other elements like textbox or checkbox, etc. in the upload.jsp file?
Am getting this type of Error please send me correct code my mail id. (using os windows 7). HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 38 35: int startPos = ((file.substring(0, pos)).getBytes()).length; 36: int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 37: File ff = new File(saveFile); 38: FileOutputStream fileOut = new FileOutputStream(ff); 39: fileOut.write(dataBytes, startPos, (endPos - startPos)); 40: fileOut.flush(); 41: fileOut.close(); Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) root cause java.io.FileNotFoundException: Accommodation Availability Enquiry.doc (Access is denied) java.io.FileOutputStream.open(Native Method) java.io.FileOutputStream.<init>(FileOutputStream.java:212) java.io.FileOutputStream.<init>(FileOutputStream.java:165) org.apache.jsp.upload_jsp._jspService(upload_jsp.java:98) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs. Apache Tomcat/7.0.27
Am getting this type of Error (using os windows 7).
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 38
35: int startPos = ((file.substring(0, pos)).getBytes()).length; 36: int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 37: File ff = new File(saveFile); 38: FileOutputStream fileOut = new FileOutputStream(ff); 39: fileOut.write(dataBytes, startPos, (endPos - startPos)); 40: fileOut.flush(); 41: fileOut.close();
Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.io.FileNotFoundException: Accommodation Availability Enquiry.doc (Access is denied)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
Apache Tomcat/7.0.27
please send correct code my id: [email protected]
Am getting this type of Error (using os windows 7).
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 38
35: int startPos = ((file.substring(0, pos)).getBytes()).length; 36: int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 37: File ff = new File(saveFile); 38: FileOutputStream fileOut = new FileOutputStream(ff); 39: fileOut.write(dataBytes, startPos, (endPos - startPos)); 40: fileOut.flush(); 41: fileOut.close();
Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.io.FileNotFoundException: Accommodation Availability Enquiry.doc (Access is denied)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
Apache Tomcat/7.0.27
please send correct code my id: [email protected]
Ads