俊星的BLOG

APACHE Geronimo 试用之JDBC

1、通过控制台创建相应的数据库,PICTURES表以及数据库连接池:
其中表的创建语句如下:
create table pictures(name varchar(32not null primary key, pic blob(16M));
更多具体内容请参考:http://cwiki.apache.org/GMOxDOC21/web-application-for-jdbc-access.html

2、实现图片上传:
package jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class ImageUpload extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet 
{
    @Resource(name 
= "jdbc/usersds")
    
private DataSource ds;
    
static final long serialVersionUID = 1L;

    
public ImageUpload() {
        
super();
    }


    
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doProcess(request, response);
    }


    
protected void doProcess(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        Connection dbconnect 
= null;
        PreparedStatement stmnt 
= null;
        String pic 
= request.getParameter("ImageLoc");
        String name 
= request.getParameter("ImageName");
        
try {
            File f 
= new File(pic);
            FileInputStream fis 
= new FileInputStream(f);
            dbconnect 
= ds.getConnection();
            stmnt 
= dbconnect.prepareStatement("INSERT INTO PICTURES ("
                    
+ "NAME," + "PIC )" + " VALUES(?,?)");
            stmnt.setString(
1, name);
            stmnt.setBinaryStream(
2, fis, (int) f.length());
            stmnt.execute();
            PrintWriter out 
= response.getWriter();
            out.println(
"<html>");
            out.println(
"<body>");
            out
                    .println(
"Congratulations your image has been successfully uploaded");
            String html 
= "<a href='/WebJDBC/ImageDownload?ImageName=" + name
                    
+ ">view the image</a>";
            out.println(html);
            out.println(
"</body>");
            out.println(
"</html>");

        }
 catch (Exception e) {
            e.printStackTrace();
        }
 finally {
            
try {
                dbconnect.close();
                stmnt.close();
            }
 catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }


    
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doProcess(request, response);
    }

}

3、实现图片下载:
package jdbc;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class ImageDownload extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet 
{
    @Resource(name 
= "jdbc/usersds")
    
private DataSource ds;
    
static final long serialVersionUID = 1L;

    
public ImageDownload() {
        
super();
    }


    
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doProcess(request, response);
    }


    
protected void doProcess(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        Connection dbconnect 
= null;
        ResultSet rs 
= null;
        PreparedStatement stmnt 
= null;
        
try {
            dbconnect 
= ds.getConnection();
            String s 
= request.getParameter("ImageName");
            stmnt 
= dbconnect
                    .prepareStatement(
"SELECT PIC FROM PICTURES WHERE NAME=?");
            stmnt.setString(
1, s);
            rs 
= stmnt.executeQuery();
            
if (rs.next()) {
                
// Get as a BLOB
                Blob aBlob = rs.getBlob(1);
                
byte[] b = new byte[4096];
                java.io.InputStream ip 
= aBlob.getBinaryStream();
                OutputStream out 
= null;
                
int c = 0;
                out 
= response.getOutputStream();
                response.setContentType(
"image/jpeg");
                
while (c != -1{
                    c 
= ip.read(b);
                    out.write(b);
                    out.flush();
                }

                ip.close();

            }


        }
 catch (Exception e) {
            e.printStackTrace();
        }
 finally {
            
try {
                dbconnect.close();
                stmnt.close();
                rs.close();
            }
 catch (SQLException e) {
                e.printStackTrace();
            }

        }


    }


    
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doProcess(request, response);
    }

}

4、图片上传JSP页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"
%>
<!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>Image Upload</title>
</head>
<body>
<h2>Select a Image and Upload it</h2>
<form action="/WebJDBC/ImageUpload">
<table>
    
<tr>
        
<td>Location of the Image(full path)</td>
        
<td><Input type="text" name="ImageLoc"></td>
    
</tr>
    
<tr>
        
<td>Name of the Image(Unique Name)</td>
        
<td><Input type="text" name="ImageName"></td>
    
</tr>
    
<tr>
        
<td><Input type="submit" value="submit"></td>
    
<tr>
</table>
</form>
</body>
</html>

5、图片下载JSP页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"
%>
<!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>Download Image</title>
</head>
<body>
<h2>Name a Image to download</h2>
<form action="/WebJDBC/ImageDownload">
<table>
<tr>
<td>
Name of the Image
</td>
<td>
<Input type="text" name="ImageName">
</td>
</tr>
<tr>
<td>
<Input type="submit" value="submit">
</td>
<tr>
</table>
</form>
</body>
</html>

6、geronimo-web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"
    xmlns:nam
="http://geronimo.apache.org/xml/ns/naming-1.2" 
    xmlns:sec
="http://geronimo.apache.org/xml/ns/security-2.0"
    xmlns:sys
="http://geronimo.apache.org/xml/ns/deployment-1.2">
    
<sys:environment>
        
<sys:moduleId>
            
<sys:groupId>default</sys:groupId>
            
<sys:artifactId>WebJDBC</sys:artifactId>
            
<sys:version>1.0</sys:version>
            
<sys:type>car</sys:type>
        
</sys:moduleId>
        
<sys:dependencies>
            
<sys:dependency>
                
<sys:groupId>console.dbpool</sys:groupId>
                
<sys:artifactId>jdbc_usersds</sys:artifactId>
            
</sys:dependency>
        
</sys:dependencies>
    
</sys:environment>
    
<context-root>/WebJDBC</context-root>
    
<nam:resource-ref>
        
<nam:ref-name>jdbc/usersds</nam:ref-name>
        
<nam:pattern>
            
<nam:groupId>console.dbpool</nam:groupId>
            
<nam:artifactId>jdbc_usersds</nam:artifactId>
            
<nam:name>jdbc/usersds</nam:name>
        
</nam:pattern>
    
</nam:resource-ref>
</web-app>

7、WEB.XML文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns
="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id
="WebApp_ID" version="2.5">
    
<display-name>WebJDBC</display-name>
    
<welcome-file-list>
        
<welcome-file>index.jsp</welcome-file>
    
</welcome-file-list>
    
<servlet>
        
<description></description>
        
<display-name>ImageUpload</display-name>
        
<servlet-name>ImageUpload</servlet-name>
        
<servlet-class>jdbc.ImageUpload</servlet-class>
    
</servlet>
    
<servlet>
        
<description></description>
        
<display-name>ImageDownload</display-name>
        
<servlet-name>ImageDownload</servlet-name>
        
<servlet-class>jdbc.ImageDownload</servlet-class>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>ImageUpload</servlet-name>
        
<url-pattern>/ImageUpload</url-pattern>
    
</servlet-mapping>
    
<servlet-mapping>
        
<servlet-name>ImageDownload</servlet-name>
        
<url-pattern>/ImageDownload</url-pattern>
    
</servlet-mapping>
</web-app>

8、发布及测试:
将应用打包成WebJDBC.war,发布之后通过访问http://localhost:8080/WebJDBC/即可看到程序运行效果,附件请点击此处下载http://www.blogjava.net/Files/kinkding/WebJDBC.rar

posted on 2009-05-12 21:53 俊星 阅读(166) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: