⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 用java实现附件的上传和下载.txt

📁 用java实现附件的上传和下载,可以测试使用
💻 TXT
字号:
用java实现附件的上传和下载(struts)
发表时间:2007年7月4日 17时47分53秒        本文链接:http://user.qzone.qq.com/326855173/blog/2评论/阅读(1/23)

用java实现附件的上传和下载(struts)
我用的是struts组件来实现附件的上传,把附件保存到oracle数据库中
建表的SQL语句是
-- Create table
create table TMEP_ABC
(
  ID        VARCHAR2(30)  primary key,
  LR1       BLOB,
  FILENAME1 VARCHAR2(300)
)

把附件保存的表中的方法
public void save(FormFile file1)
{
  Connection cn=null;
  Statement stat=null;
  ResultSet rs=null;
  String name1=file1.getFileName();
  
  long id=getMaxId();
  try{
   cn=getConnection();
   cn.setAutoCommit(false);
   stat=cn.createStatement();
   
   String sql="insert into tmep_abc(id,filename1,lr1) values('"+id+"','"+name1+"',EMPTY_BLOB())";
   stat.executeUpdate(sql);
   rs = stat.executeQuery("SELECT lr1 FROM tmep_abc WHERE ID='"+id+"' FOR UPDATE");
   if (rs.next()) { 
    /* 取出此BLOB对象 */ 
    oracle.sql.BLOB blob1 = (oracle.sql.BLOB)rs.getBlob("lr1"); 
   
    /* 向BLOB对象中写入数据 */ 
    BufferedOutputStream out1 = new BufferedOutputStream(blob1.getBinaryOutputStream()); 
    out1.write(file1.getFileData());
    out1.close();
   
    
   } 
    /* 正式提交 */ 
   cn.commit(); 
   //cn.setAutoCommit(true);
  }
  catch(Exception ex){
   ex.printStackTrace();
   try {
   cn.rollback();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }}
  finally
  {
   if(rs!=null)
    try {
     rs.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(stat!=null)
    try {
     stat.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(cn!=null)
    try {
     cn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
}


下载附件的方法为:
id为表的主键
public void getDownFile(String id,HttpServletResponse response)
{
  String sql="SELECT lr1,filename1 FROM tmep_abc where id='"+id+"'";
  
  InputStream in=null;
  Connection cn=null;
  Statement stat=null;
  ResultSet rs=null;
  BufferedOutputStream output = null;
  BufferedInputStream input = null;
  String temp="";
  try{
   cn=getConnection();
   stat=cn.createStatement();
   rs=stat.executeQuery(sql);
   if(rs.next())
   {
    byte[] buffer = new byte[1024];
    temp=rs.getString("filename1");
    
    java.sql.Blob blob = rs.getBlob("lr1"); 
    in= blob.getBinaryStream(); 
    response.reset();
    response.setContentType("application/octet-stream; charset=gb2312");
    try {
    
     temp=URLEncoder.encode(temp, "UTF-8");
     response.setHeader("Content-disposition", "attachment;filename="
       + temp);
     System.out.println();
    } catch (UnsupportedEncodingException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
    output = new BufferedOutputStream(response.getOutputStream());
    input = new BufferedInputStream(in);
    int n = (-1);
    do {
     n = input.read(buffer, 0, buffer.length);
     if (n != (-1))
      output.write(buffer, 0, n);
    } while (n != (-1));
    response.flushBuffer();
   }
  }
  catch(Exception ex){}
  finally
  {
   if (input != null)
    try {
     input.close();
    } catch (IOException e) {
     
    }
   if (output != null)
    try {
     output.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     //e.printStackTrace();
    }
   if(rs!=null)
    try {
     rs.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(stat!=null)
    try {
     stat.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(cn!=null)
    try {
     cn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
  
  
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -