Uploading images to a database in a JSP

Source: Internet
Author: User

1 First step: Build a Database2CREATE TABLE Test_img (ID number (4), name varchar, imgLongRaw); 3 Step Two: (newimg.html)45<body>6Add Image <br>7<form method= "POST" action= "insertnews.jsp" >8Image id:<input Name= "id" size= "ten" >9<br>TenSelect Image: <input type= "file" name= "image" > One<br> A<input type= "Submit" value= "upload" name= "submit" size= ">" -<input type= "reset" value= "clears" name= "clear" size= ">" -<br> the</form> -</body> - Step Three: Insert Database (insertimg.jsp) -<%@ page language= "java"%> +<%@ page contenttype= "text/html;charset=gb2312"%> -<%@ pageImport= "Java.util.*"%> +<%@ pageImport= "Java.sql.*"%> A<%@ pageImport= "Java.text.*"%> at<%@ pageImport= "Java.io.*"%> -<% -Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");//Com.microsoft.jdbc.sqlserver.SQLserveDriver -String url= "Jdbc:oracle:thin: @lubin: 1521:b2bdb";//Jdbc:microsoft:sqlserver://127.0.0.1:1433; Jdbc:microsoft.sqlserver://127.0.0.1:1433 -Connection con=drivermanager.getconnection (URL, "Demo", "Demo");  - //Insert Database inString sql= "INSERT into test_img values (?,?,?)";  - //get the value of the pass ID toString id=request.getparameter ("id");  + //get the path to the image -String Kk=request.getparameter ("image");  the //convert to file format *File filename=NewFile (KK); $ //read the length of the file and convert it to a long typePanax Notoginseng Longl1=filename.length (); - intL2= (int) L1;  the //assign a value in a stream format +FileInputStream fis=Newfileinputstream (filename); APreparedStatement PS =con.preparestatement (SQL); thePs.setstring (1, id);  +Ps.setstring (2, Filename.getname ());  -Ps.setbinarystream (3, FIS,L2);  $ //Ps.setbinarystream (3,fis,fis.available ()); $ ps.executeupdate (); - //Ps.execute (); - ps.close (); the fis.close (); -OUT.PRINTLN ("OK!!!"); Wuyi%> the Fourth step: Show Picture (showimg.jsp) -<%@ page language= "Java"Import= "Java.sql.*"%> Wu<%@ pageImport= "Java.io.*"%> -<%@ page contenttype= "text/html;charset=gb2312"%> About<% $Class.forName ("Oracle.jdbc.driver.OracleDriver");  -String url= "Jdbc:oracle:thin: @lubin: 1521:b2bdb";  -String image_id = (string) request.getparameter ("id");  -Connection con=drivermanager.getconnection (URL, "Demo", "Demo");  APreparedStatement sql=con.preparestatement ("select * from test_img WHERE id =" +image_id);  +ResultSet rs =sql.executequery (); the Rs.next (); - //Enter the contents of the IMG field into the $InputStream in = Rs.getbinarystream ("img");  the //The following are any processing, such as output to the page: the Response.reset (); theResponse.setcontenttype ("Image/jpeg");  the byte[] B =New byte[1024];  - intLen; in  while((Len=in.read (b)) >0)  theResponse.getoutputstream (). Write (b,0, Len);  the in.close (); About rs.close (); the%>

Upload page must use post also to add an attribute enctype= "Multipart/form-data", go in to parse the request body to get

You write <file name= "filename"/> You on the server Request.getparameter ("filename"); is not going to get it.
Because this data and the uploaded file stream are encapsulated in the request body, to resolve the

If you want the file name: You can do this
<file name= "File"/>

<%--
Type enctype with Multipart/form-data, which can upload the data in the file as streaming data, regardless of the file type, can be uploaded.
--%>

Uploading images to a database in a JSP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.