随笔-167  评论-65  文章-0  trackbacks-0

上次我介绍了mysql数据库中存取图片的方法,详见:

http://www.blogjava.net/fl1429/archive/2009/05/27/278212.html

正好这几天又用到了sql server 2000数据库,然而在sql 2000中存取图片和mysql中大部相同,但是思想是一样的,都是在数据库中存二进制,但是还有一种解决方案,就是可以再数据库中存取图片的路径,这样比较简单,我就不介绍了,为了安全起见,还是把图片保存到数据库中了.

首先注意在mysql中存的字段类型是 blob类型,而在sql 2000中则用image类型,具体步骤见下:

上传页:upload.jsp

         <form action="ImageServlet" method="post" name="form1">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>头像上传:</p>
    <table width="800" height="136" border="0">
        <tr>
        <td>姓名:</td>
        <td><input type="text" name="name">
        </td>
      </tr>
      <tr>
        <td>上传图片:</td>
        <td><input type="file" name="photo">
        (上传后不可修改)</td>
      </tr>
      <tr>
        <td><input type="submit" name="Submit" value="提交"></td>
          <td><input type="reset" name="Submit2" value="重置"></td>
      </tr>
    </table>
  </form>

处理的servlet:ImageServlet

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String path = request.getParameter("photo");
        
        int len;
        String query;
        PreparedStatement pstmt;
        Connection conn = null;
        
       try{
                   ConnectSqlserver connectSqlserver = new ConnectSqlserver();
                   conn = connectSqlserver.openConnection();
                File file = new File(path);
                FileInputStream fis = new FileInputStream(file);
                len = (int)file.length();

                query = ("insert into applys(name,photo) VALUES(?,?)");
                pstmt = conn.prepareStatement(query);
                pstmt.setString(1,name);      

                // Method used to insert a stream of bytes
                pstmt.setBinaryStream(2, fis, len); 
                pstmt.executeUpdate();
       }catch(Exception e){
           e.printStackTrace();
       }
        
        out.flush();
        out.close();
    }

显示图片的servlet:

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Connection conn = null;
        
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        //PrintWriter out = response.getWriter();
        
          byte[] fileBytes;
          String query = "";
          try
          {
              ConnectSqlserver connectSqlserver = new ConnectSqlserver();
              conn = connectSqlserver.openConnection();
                  query = "select photo from applys where name = 'feng' ";
                
                  Statement state = conn.createStatement();
                  ResultSet rs = state.executeQuery(query);
                  if (rs.next())
                 {
                           fileBytes = rs.getBytes("photo");
                           System.out.println(fileBytes);
                      //     OutputStream targetFile = new FileOutputStream("d://new.JPG");
                           ServletOutputStream targetFile = response.getOutputStream();
                           targetFile.write(fileBytes);
                           targetFile.close();
                 }        
                 
          }
          catch (Exception e)
          {
                  e.printStackTrace();
          }

        //out.flush();
        //out.close();
    }

显示图片的jsp:view.jsp

  <td width="152" rowspan="3"><img src="ShowServlet?name=feng" width="140" height="140"></td>

ref:

http://www.java-tips.org/other-api-tips/jdbc/how-to-store-retrieve-image-to-from-sqlserver.html
http://forums.sun.com/thread.jspa?threadID=5361151
http://java.ittoolbox.com/groups/technical-functional/java-l/insert-image-into-sqlserver-sample-program-157642
http://www.java.happycodings.com/JDBC/code3.html
http://www.cnblogs.com/long/articles/166546.html

write by feng
posted on 2009-06-02 15:10 fl1429 阅读(1350) 评论(1)  编辑  收藏 所属分类: Jsp/Servlet/Javabean

评论:
# re: java sql server 2000存取图片 2010-07-13 21:28 | as
谢谢你   回复  更多评论
  

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


网站导航:
 
已访问数:
free counters