jspsmartupload 详细实例(2)

本文介绍了一种使用JSP实现文件上传至Oracle数据库并从中读取的方法。具体步骤包括使用jspSmartUpload组件处理文件上传、连接Oracle数据库进行文件存储及读取等。此方案适用于希望在Web应用中集成文件管理功能的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     上传:

  <%@page   contentType="text/html;charset=gb2312"%>  
  <%@   page   language="java"   import="java.sql.*,com.jspsmart.upload.*"%>  
  <jsp:useBean   id="mySmartUpload"   scope="page"   class="com.jspsmart.upload.SmartUpload"   />  
   
  <HTML>  
  <BODY   BGCOLOR="white">  
   
  <H1>jspSmartUpload   :   Sample   4</H1>  
  <HR>  
   
  <%  
  //   Connect   to   the   database  
   
  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();        
  String   url="jdbc:oracle:thin:@192.168.4.105:1521:dbName";    
  //orcl为你的数据库的SID    
  String   user="uNane";    
  String   password="pass";    
  Connection   conn=   DriverManager.getConnection(url,user,password);        
  Statement   stmt=conn.createStatement();        
   
  //   Initialization  
  mySmartUpload.initialize(pageContext);  
   
  //   Upload  
  mySmartUpload.upload();  
   
  //   upload   file   in   the   DB   if   this   file   is   not   missing  
  if   (!mySmartUpload.getFiles().getFile(0).isMissing()){  
   
  try   {  
  String   sFileName   =   mySmartUpload.getFiles().getFile(0).getFileName()   ;  
  mySmartUpload.save("/upload");  
  String   sPath   =   request.getRealPath("/upload/")+sFileName   ;  
  java.io.File   file   =   new   java.io.File(sPath);  
  java.io.FileInputStream   fis   =   new   java.io.FileInputStream(file);  
  PreparedStatement   ps   =    
  conn.prepareStatement("insert   into   tfile   values   (?,?,?)");  
  ps.setInt(1,3);  
  ps.setString(2,file.getName());  
  ps.setBinaryStream(3,fis,(int)file.length());  
  ps.executeUpdate();  
  ps.close();  
  fis.close();    
   
  }   catch(Exception   e)   {  
  out.println("An   error   occurs   :   "   +   e.toString());  
  }  
   
   
  }  
   
  stmt.close();  
  conn.close();  
   
  %>  
  </BODY></HTML>

 

 

读取:

 <%@   page   language="java"   import="java.sql.*,java.util.*"%>  
  <%  
        String   image_id   ="2"   ;  
        try    
                    {  
  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();        
  String   url="jdbc:oracle:thin:@192.168.4.105:1521:dbName";    
  //orcl为你的数据库的SID    
  String   user="uName";    
  String   password="pass";    
  Connection   conn=   DriverManager.getConnection(url,user,password);        
                      Statement   stmt   =   conn.createStatement();  
                  ResultSet   rs   =   stmt.executeQuery("SELECT   *   FROM   tfile   WHERE   id   =   "   +   image_id);  
                  if   (rs.next())  
                          {  
                  String   dim_image   =   rs.getString("filename");  
                  byte   []   blocco   =   rs.getBytes("tfile");  
                  response.setContentType("image/jpg");  
  javax.servlet.ServletOutputStream   op   =   response.getOutputStream();  
                  for(int   i=0;i<blocco.length;i++)  
                  {  
                          op.write(blocco[i]);  
                  }  
                  }    
    op.close()   ;  
              rs.close();  
              stmt.close();  
              conn.close();  
            }   catch(Exception   e)   {  
                  }                          
          }  
  %>    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值