当前位置:编程学习 > JAVA >>

深入体验javaWeb开发内幕——问件的上传与下载案例实现

 遍历上传目录下的所有文件显示给用户,并允许用户完成下载。

 

(读取某一个文件夹下的所有的文件,存到集合里面List,再存到request作用域范围中)。

一个表的相关字段:

    CREATETABLE `upfile` (

  `id` varchar(50) NOT NULL,

  `uuidname` varchar(255) NOT NULL,

  `realname` varchar(255) NOT NULL,

  `savepath` varchar(255) NOT NULL,

  `uptime` datetime NOT NULL,

  `description` varchar(255) DEFAULT NULL,

  `username` varchar(40) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULTCHARSET=utf8;

 

一个实体类:

[java]
  publicclass UploadFile { 
 
  
 
    privateString id ; 
 
    private String uuidname ; 
 
    private String realname; 
 
    private String savepath ; 
 
    private Date uptime ; 
 
    private String description ; 
 
    private String username ; 
 
    
 
    public String getId() { 
 
       returnid; 
 
    } 
 
    publicvoid setId(String id) { 
 
       this.id = id; 
 
    } 
 
    public StringgetUuidname() { 
 
       returnuuidname; 
 
    } 
 
    publicvoid setUuidname(String uuidname) { 
 
       this.uuidname = uuidname; 
 
    } 
 
    public StringgetRealname() { 
 
       returnrealname; 
 
    } 
 
    publicvoid setRealname(String realname) { 
 
       this.realname = realname; 
 
    } 
 
    public StringgetSavepath() { 
 
       returnsavepath; 
 
    } 
 
    publicvoid setSavepath(String savepath) { 
 
       this.savepath = savepath; 
 
    } 
 
    public Date getUptime(){ 
 
       returnuptime; 
 
    } 
 
    publicvoid setUptime(Date uptime) { 
 
       this.uptime = uptime; 
 
    } 
 
    public StringgetDescription() { 
 
       returndescription; 
 
    } 
 
    publicvoid setDescription(String description) { 
 
       this.description = description; 
 
    } 
 
    public StringgetUsername() { 
 
       returnusername; 
 
    } 
 
    publicvoid setUsername(String username) { 
 
       this.username = username; 
 
    } 
 


一个dao接口:

[java]
 public  inte易做图ce UploadFileDao { 
 
  publicvoid insert(UploadFile f); 
 
  
 
  public UploadFilegetUploadFile(String id); 
 
  
 
  publicList<UploadFile> getUploadFiles(); 
 


该接口的实现类:

 

[java] 
publicclass UploadFileDaoImpl implements UploadFileDao{ 
 
  
 
    @Override 
 
    public UploadFilegetUploadFile(String id) { 
 
       QueryRunner qr = new QueryRunner(DBManager.getDataSource()); 
 
       String sql = "selectid,uuidname,realname,savepath,uptime,description,username from  uploadfile where id=?"; 
 
       try { 
 
           UploadFile uf = qr.query(sql,id,new BeanHandler(UploadFile.class)); 
 
           return uf; 
 
       } catch(SQLException e) { 
 
           thrownew RuntimeException(e); 
 
       } 
 
    } 
 
  
 
    @Override 
 
    publicList<UploadFile> getUploadFiles() { 
 
       QueryRunner qr = new QueryRunner(DBManager.getDataSource()); 
 
       String sql = "selectid,uuidname,realname,savepath,uptime,description,username from  uploadfile order by uptime desc"; 
 
       try { 
 
           List<UploadFile> list = qr.query(sql,new BeanListHandler(UploadFile.class)); 
 
           return list; 
 
       } catch(SQLException e) { 
 
           thrownew RuntimeException(e); 
 
       } 
 
    } 
 
  
 
    @Override 
 
    publicvoid insert(UploadFile f) { 
 
       
 
       QueryRunner qr = new QueryRunner(DBManager.getDataSource()); 
 
       String sql = "insert into uploadfile(id,uuidname,realname,savepath,uptime,description,username)value(?,?,?,?,?,?,?)"; 
 
       Object []obj={f.getId(),f.getUuidname(),f.getRealname(),f.getSavepath(),f.getUptime(),f.getDescription(),f.getUsername()}; 
 
       try { 
 
           qr.update(sql,obj); 
 
       } catch(SQLException e)

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,