jsp存取图片.docx

上传人:b****2 文档编号:2257144 上传时间:2022-10-28 格式:DOCX 页数:8 大小:18.48KB
下载 相关 举报
jsp存取图片.docx_第1页
第1页 / 共8页
jsp存取图片.docx_第2页
第2页 / 共8页
jsp存取图片.docx_第3页
第3页 / 共8页
jsp存取图片.docx_第4页
第4页 / 共8页
jsp存取图片.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

jsp存取图片.docx

《jsp存取图片.docx》由会员分享,可在线阅读,更多相关《jsp存取图片.docx(8页珍藏版)》请在冰豆网上搜索。

jsp存取图片.docx

jsp存取图片

该资源来自网上经测试好用,分享给大家!

 

在写一个程序时我遇到了要将图片存入数据库的操作,在网上也没找见好的解决方法,最后请教了同事才解决.我想有的人可能用得到,就把它写出来.我只写一些核心的与图片操作相关的代码.

图片存入数据库及取出显示在网页上:

存入及取出显示都需要用到上传文件时的jspsmart.jar包。

在页面上加入一些方法即可实现。

需要注意的是:

存取

数据库的时要用PreparedStatement而不能用Statement,否则的话图片是存不进去的,你去找原因可能得发

不少时间哦。

<%@pagecontentType="text/html;charset=GBK"%>

<%@page  java.io.*"%>

<%@pageimport="com.jspsmart.upload.*"%>

<%

SmartUploadsu=newSmartUpload();

  //上传初始化

  su.initialize(pageContext);

  //设定上传限制

  //1.限制每个上传文件的最大长度。

  //su.setMaxFileSize(10000);

  //2.限制总上传数据的长度。

  //su.setTotalMaxFileSize(20000);

  //3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。

  //su.setAllowedFilesList("jpg,jpeg");

  //4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。

  //su.setDeniedFilesList("exe,bat,jsp,htm,html,,");

  //上传文件

  //System.out.println("test1");

  su.upload();

  //System.out.println("test2");

  byte[]fileBytes=null;

  for(inti=0;i

    //System.out.println("test3");

    com.jspsmart.upload.FileuploadFile=su.getFiles().getFile(i);

    if(!

uploadFile.isMissing()){

      //System.out.println("test4");

      //xmlFileName=uploadFile.getFilePathName();

      fileBytes=newbyte[uploadFile.getSize()];

      for(intj=0;j

        //System.out.println("test5");

        fileBytes[j]=uploadFile.getBinaryData(j);

      }break;

    }

  }

%>

fileBytes就是上传图片的的字节数组,得到了这个我们就能在java代码中存入数据库了。

显示图片,这里作为一个单独的jsp页面getImage.jsp.

另一个页面info.jsp里在显示图片的地方

id=1"  >

id根据情况要做以改变,我这只是说明问题.

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.io.*"%>

<%@page  import="com.Info"%>

useBeanid='dbex'scope='page'class='com.Dbex'type="com.Dbex"/>

useBeanid='fi'scope='page'class='com.Info'  />

      

<%String  id=fi.getGbk(request.getParameter("id"));

      Infoinfo=dbex.findById(id);

      out.clearBuffer();

      response.setContentType("image/jpg");

      byte[]photo=info.getPhoto();//photo为从数据库里取出的图片的字节数组

      ByteArrayOutputStreamimageStream=newByteArrayOutputStream();

          if(photo!

=null){

      imageStream.write(photo);

          }

      ServletOutputStreamstreamOut=response.getOutputStream();

      imageStream.writeTo(streamOut);

      streamOut.close();

%>

 我在这里只是大概说了说,有兴趣的朋友可以自己试试.

 

 java.sql.Connection lConn = null; 

     java.sql.Statement lStat = null; 

     java.sql.ResultSet lRs = null; 

     try 

     {       

       DBConnectionManager lDB = DBConnectionManager.getInstance(); 

       lConn =  lDB.getConnection("ORACLE"); 

       lStat = lConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 

       lStat.executeUpdate("insert id,EMPTY_BLOB() from tab where id=2"); 

       lRs = lStat.executeQuery("select pic from tab where id=2"); 

       if(lRs.next()) 

       { 

         oracle.sql.BLOB blob = (oracle.sql.BLOB)lRs.getBlob("pic"); 

         OutputStream outstream = blob.getBinaryOutputStream(); 

         byte[] bAtt = "data".getBytes(); 

         outstream.write(bAtt,0,bAtt.length); 

         outstream.close(); 

         lRs.updateBlob("pic",blob); 

       } 

        

     } 

     catch(Exception e) 

     { 

       e.printStackTrace();        

     } 

     finally 

     { 

       try{lRs.close();}catch(Exception e){}   

       try{lStat.close();}catch(Exception e){}   

       try{lConn.close();}catch(Exception e){}   

     }Top

4楼olvest4000(奥维斯)回复于2002-08-2011:

54:

29得分0

File files = new File("d:

\\saint.jpg"); 

         FileInputStream fis=new FileInputStream(files); 

         PreparedStatement ps = conn.prepareStatement("INSERT INTO Image (id,name,data) VALUES (?

?

?

)"); 

         ps.setInt(1,4); 

         ps.setString(2, files.getName()); 

         ps.setBinaryStream (3,fis,(int)files.length()); 

         ps.executeUpdate();

 

三.<%@ page contentType="text/html; charset=gb2312" language="java"  

 import="java.util.*,com.jspsmart.upload.*" errorPage="" %> 

  

  

 文件上传处理页面 

  

  

  

  

 <% 

 // 新建一个SmartUpload对象 

 SmartUpload su = new SmartUpload(); 

 // 上传初始化 

 su.initialize(pageContext); 

 // 设定上传限制 

 // 1.限制每个上传文件的最大长度。

 

 // su.setMaxFileSize(10000); 

 // 2.限制总上传数据的长度。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 中职中专

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1