1. 程式人生 > >JSP—實現圖片上傳到資料庫

JSP—實現圖片上傳到資料庫

檔案目錄結構

資料庫

CREATE  TABLE `user`.`images` (  
`id` INT NOT NULL ,  
`content` VARCHAR(100) NULL ,  
`image` BLOB NULL ,  
PRIMARY KEY (`id`) );
INSERT INTO `user`.`images` (`id`, `content`) VALUES (0, '無');


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<form method="post" action="image.jsp">
		<input type="file" name="image" /><br /> 相片描述<input type="text"
			name="content" /><br /> <input type="submit" />
	</form>
</body>
</html>

介面:


image.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>圖片上傳</title>
</head>
<body>
	<jsp:useBean id="conn4" class="com.conDBMS.Conn" scope="session" />
	<%
		request.setCharacterEncoding("utf-8");
		String content = request.getParameter("content");
		String filename = request.getParameter("image");
		System.out.println(filename);
		//ServletInputStream str=request.getInputStream();  
		InputStream str = new FileInputStream(filename);
		//byte b[]=new byte[str.available()];
		//str.read(b);x
		//ByteArrayInputStream bi=new ByteArrayInputStream(b);
		String sql1 = "select * from images";
		Statement stmt = conn4.getConnection().createStatement();
		System.out.println("連線成功");
		ResultSet rs = stmt.executeQuery(sql1);
		rs.last();
		int id = rs.getInt("id") + 1;
		String sql = "insert into images(id,content,image) values(" + id
				+ ",?,?)";
		PreparedStatement pstmt = conn4.getConnection().prepareStatement(
				sql);
		pstmt.setString(1, content);
		long l = (long) str.available();
		pstmt.setBinaryStream(2, str, l);
		//out.print(bi.available());
		try {
			pstmt.executeUpdate();
		} catch (Exception e) {
			//out.print(sql);
			out.print(e.getMessage());
		}
		out.println("Success,You Have Insert an Image Successfully");
	%>
</body>
</html>

Conn.java

package com.conDBMS;

import java.sql.Connection;
import java.sql.DriverManager;

public class Conn {
	final String MYSQLDBDRIVER = "com.mysql.jdbc.Driver";
	final String MYSQLDBURL = "jdbc:mysql://localhost/user";
	final String MYSQLDBUSER = "root";
	final String MYSQLDBUSERPASS = "root";

	public Connection getConnection() {
		try {
			Class.forName(MYSQLDBDRIVER);
			Connection con = DriverManager.getConnection(MYSQLDBURL,
					MYSQLDBUSER, MYSQLDBUSERPASS);
			return con;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

執行結果: