文件上传下载:通过java方法把附件保存到clob字段中

该博客介绍了一种使用Java从数据库中的CLOB字段读取并下载附件的方法,以及如何将文件保存到CLOB字段。通过`downloadFromDb`方法实现下载,利用`BufferedInputStream`和`BufferedOutputStream`进行数据流处理。同时,`testBlob`方法展示了如何将文件内容保存到CLOB字段,包括读取文件、创建Blob对象和写入数据到数据库。

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

  /**
   * 下载:从数据库中读出blob字段的内容,并下载
   * 
@param response
   * 
@param file_name
   
*/
  
  
public   static   void  downloadFromDb(
          HttpServletResponse response,
          
long  accessoryId                     // 附件ID
          ) {
        BufferedInputStream in  
= null;
        BufferedOutputStream out
= null;

        
try{
          CustomAccessoryBase cab
=CustomAccessoryBase.getObjectById(accessoryId);
          java.sql.Blob blob 
=cab .getContent();       //附件内容
        String cname=cab.getAccessoryName();         //附件名称
          cname = new String(cname.getBytes("GBK"),"iso8859-1");
          response.reset();
          response.setHeader(
"Content-Disposition""attachment;"+" filename="+cname);
               
          in  
= new BufferedInputStream(  blob.getBinaryStream());
          out 
= new BufferedOutputStream(response.getOutputStream()); 

          
byte[] buffer =new byte[1024];
          
int len = 0 ;
          
while ((len = in.read(buffer))!=-1)
          
{
            out.write(buffer,
0,len);
          }

          out.flush();
        }
catch(Exception e){
          e.printStackTrace();
        }
finally{
          
try{
            
if ( in != null )
              in.close();
          }
catch(Exception e){
          }

          
try{
            
if ( out != null )
              out.close();
          }
catch(Exception e){
          }
      
        }

      }

  
/**
 *   把附件保存到表中的方法
 * 
@param iFilePath

 
*/

    
public   static   void  testBlob(String iFilePath)  {
        Session s 
= null;
        
byte[] buffer = new byte[1];
        buffer[
0= 1;
        
try {
            SessionFactory sf 
= ZAutoLoadServlet.getSessionFactory();
            s 
= sf.openSession();
            Transaction tx 
= s.beginTransaction();
          CustomAccessoryBase cab
=new CustomAccessoryBase();    
          cab.setContent(Hibernate.createBlob(buffer));
            s.save(cab);
            s.flush();
            s.refresh(cab, LockMode.UPGRADE);
            BLOB blob 
= (BLOB) cab.getContent();
            OutputStream out 
= blob.getBinaryOutputStream();
            
            File f 
= new File(FileUpload.getStorageHome()+iFilePath);
    
            FileInputStream fin 
= new FileInputStream(f);
            
int count = -1, total = 0;
            
byte[] data = new byte[(int) fin.available()];
            fin.read(data);
            out.write(data);
            fin.close();
            out.close();
            s.flush();
            tx.commit();

        }
 catch (Exception e) {
            System.out.println(e.getMessage());
        }
 finally {
            
if (s != null)
                
try {
                    s.close();
                }
 catch (Exception e) {
                }

        }

    }

     
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值