怎么在jsp中把图片存入数据库呀?

ttoc 2004-11-22 02:19:57
有这样一个个表单
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<br>
<br>
<input type="text" name="textfield">
<br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
提交后我怎么能把file里的东西存到sqlserver数据库中的image类型的字段中?又怎么能把text里的东西取出来呢?
麻烦大家帮忙~~~
...全文
279 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjg827678274 2010-06-01
  • 打赏
  • 举报
回复
jsp
java
ttoc 2004-11-23
  • 打赏
  • 举报
回复
to lijunjiejava(不眠之夜)

非常感谢~~

我还想问一下,在hibernate中image类型的字段的映射文件要写成什么类型的呀?
lijunjiejava 2004-11-22
  • 打赏
  • 举报
回复
向Mssql数据库写入image代码的程序,所以我在这在写一下用jsp怎样向数据库写入图像文件。大家先在数据库建这样一张表,我下面的这些代码对任何数据库都通用,只要支持blob类型的
  只要大家将连接数据库的参数改一下就可以了。
SQL>create table image(id int,content varchar(200),image blob);
  如果在sqlserver2000的数据库中,可以将blob字段换为image类型,这在SqlServer2000中是新增的。
  testimage.html文件内容如下:
<HTML>
<HEAD>
<TITLE>Image File </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<FORM METHOD=POST ACTION="testimage.jsp">
<INPUT TYPE="text" NAME="content"><BR>
<INPUT TYPE="file" NAME="image"><BR>
<INPUT TYPE="submit"></FORM>
<BODY>
</BODY>
</HTML>

  我们在Form的action里定义了一个动作testimage.jsp,它的内容如下:
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%> 
    <html> 
    <body> 
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    String url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1"; 
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改 
    Connection conn= DriverManager.getConnection(url); 
String content=request.getParameter("content");
String filename=request.getParameter("image");
FileInputStream str=new FileInputStream(filename);
String sql="insert into test(id,content,image) values(1,?,?)";
PreparedStatement pstmt=dbconn.conn.prepareStatement(sql);
pstmt.setString(1,content);
    pstmt.setBinaryStream(2,str,str.available());
  pstmt.execute();
  out.println("Success,You Have Insert an Image Successfully");
%>

下面我写一个测试image输出的例子看我们上面程序写的对不对,testimageout.jsp的内容如下:
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%> 
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    String url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1"; 
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改 
    Connection conn= DriverManager.getConnection(url); 
String sql = "select image from test where id=1";
   Statement stmt=null;
   ResultSet rs=null;
   try{
    stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);
   }catch(SQLException e){}
   try {
   while(rs.next()) {
    res.setContentType("image/jpeg");
    ServletOutputStream sout = response.getOutputStream();
    InputStream in = rs.getBinaryStream(1);
    byte b[] = new byte[0x7a120];
    for(int i = in.read(b); i != -1;)
      {
        sout.write(b);
        in.read(b);
      }
      sout.flush();
      sout.close();
    }
   }
   catch(Exception e){System.out.println(e);}
  %>
  </body>
  </html>
  你运行这个程序,你就会看到刚才你写入美丽的图片就会显示在你面前。怎么样,用jsp来试试。
  这种方法把图片写到数据库中会使数据库在短时间内容量飞涨,会影响性能的,另外一种做法将图片存上传到服务器上,
  在数据库里只存放图片的路径,这是一个很好的方法。我建议大家采取后面一种方法。
皮鲁 2004-11-22
  • 打赏
  • 举报
回复
smartUpload 里边都支持直接存入数据库字段的。
//-----------------------------------------------------------------

<%@ page language="java" import="com.jspsmart.upload.*" contentType="text/html; charset=gb2312" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<HTML>
<BODY BGCOLOR="white">

<H1>jspSmartUpload : Sample 4</H1>
<HR>

<%


// Variables
int count=0;

// Connect to the database
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test");

// SQL Request
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM TFILES WHERE ID=1");

// if the resultset is not null
if (rs.next()){

// Initialization
mySmartUpload.init(config);
mySmartUpload.service(request,response);

// Upload
mySmartUpload.upload();

// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){

try {

rs.updateString("FILENAME",mySmartUpload.getFiles().getFile(0).getFileName());

// Add the current file in the DB field
mySmartUpload.getFiles().getFile(0).fileToField(rs,"FILE");

// Update
rs.updateRow();
count++;

} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}

}

}


// Display the number of files uploaded
out.println(count + " file(s) uploaded in the database.");

rs.close();
stmt.close();
con.close();

%>

</BODY>
</HTML>
ttoc 2004-11-22
  • 打赏
  • 举报
回复
我也用了呀,关键是怎么写到数据库中,并且我用了hibernate
zhuzhixian 2004-11-22
  • 打赏
  • 举报
回复
你可以使用jspsmartUpload组件,很好用的
ttoc 2004-11-22
  • 打赏
  • 举报
回复
大家帮帮忙呀?真的很急,我就剩下这么多分了,哪怕只说说text里的值怎么取出来也行

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧