您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

jsp中图片处理相关操作

来源:未知 责任编辑:责任编辑 发表时间:2013-08-27 11:20 点击:


<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*"%>
<html>
<body>
<%
/*
drop table imagetable;
create table imagetable
(
   nid                            int                            not null,
   image                          blob,
   Prima(最完善的虚拟主机管理系统)ry key (nid)
)
type = InnoDB;

*/

/*
//================ 一 、将文件写入到数据库的大字段中begin=====================
Class.forName("org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver").newInstance();
String url ="jdbc:MySQL(和PHP搭配之最佳组合)://localhost:3306/test?user=root&password=eastsoftweb";
Connection conn= DriverManager.getConnection(url);
java.io.File file = new File("d:/temp/1.jpg");
 FileInputStream is=new FileInputStream(file);
 PreparedStatement stmt = conn.prepareStatement(
          "INSERT INTO imagetable (nid,image)" +
"VALUES (?, ?)");  //预编译SQL语句
 stmt.setInt(1, 1);
 stmt.setBinaryStream(2, is,(int)file.length());
 stmt.executeUpdate();
 stmt.close();
 is.close();
 out.println("update end");
//===============将文件写入到数据库的大字段中end=========================
*/


/*
//====================== 二、jsp(SUN企业级应用的首选)显示服务器硬盘图片示例 begin==============

FileInputStream is=new FileInputStream("d:/temp/1.JPG");
response.reset();
response.setContentType("image/jpeg");        
ServletOutputStream sos = response.getOutputStream();     
byte[] buffer = new byte[1024];
int len=0;
while((len=is.read(buffer))>0){
  sos.write(buffer,0,len);

  sos.flush();     
  sos.close();

//=======================jsp(SUN企业级应用的首选)显示服务器硬盘图片示例 end===================
*/

 

//=====================  三、将数据库的大字段图片还原到本地,并在网页上显示begin==============
Class.forName("org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver").newInstance();
String url ="jdbc:MySQL(和PHP搭配之最佳组合)://localhost:3306/test?user=root&password=eastsoftweb";
Connection conn= DriverManager.getConnection(url);
java.io.File file = new File("d:/temp/db.jpg");
FileOutputStream os=new FileOutputStream(file);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select nid,image from imagetable where nid=1");
rs.next();
byte[] buffer=rs.getBytes(2);

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • JSP与Servlet
    • 自己动手写MiniBBS系列(基本篇)之用户登录
    • JSP取当前日期
    • JDBC 入门(一)
    • 打开一个jsp页面默认查询所有数据,调用action
    • 使用JSP标签库验证用户的输入(2)完
    • WIN98/2000下的jsp服务器
    • 自定义JSP标签(tag)浅议
    • Struts学习傻瓜式入门篇
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1