postgresql数据库保存和下载二进制文件的几种方式

           前序:如标题所示,主要是贴核心代码。基本能用。都可以用的,我亲测过了。

//方式一:将文件转成字节数组,采用原生sql存到数据库
if(StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
	//获取file文件
	CommonsMultipartFile cmf = (CommonsMultipartFile) multipartFile;
	DiskFileItem dfi = (DiskFileItem)cmf.getFileItem();
	File file = dfi.getStoreLocation();
	//转成流
    InputStream ips = new FileInputStream(file);
    
	byte[] byteData = new byte[(int) file.length()];
	ips.read(byteData, 0, byteData.length);
	
    DataSource ds = this.jdbcTemplate.getDataSource();
	Connection conn = ds.getConnection();
	String sql = "update tpm_plan set attachment = ? where id = ?";
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setBytes(1, byteData);
	ps.setLong(2, 69);
    int i=ps.executeUpdate();
    if(i==0){
        Boolean flag=false;
        System.out.println(flag);
    }
	DataSourceUtils.releaseConnection(conn, ds);

}
//方式二:采用原生sql存二进制文件
if(StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
	//获取file文件
	CommonsMultipartFile cmf = (CommonsMultipartFile) multipartFile;
	DiskFileItem dfi = (DiskFileItem)cmf.getFileItem();
	File file = dfi.getStoreLocation();
	//转成流
    InputStream ips = new FileInputStream(file);
	
    DataSource ds = this.jdbcTemplate.getDataSource();
	Connection conn = ds.getConnection();
	String sql = "update tpm_plan set attachment = ? where id = ?";
	PreparedStatement ps = conn.prepareStatement(sql);
    ps.setBinaryStream(1, ips, file.length());
	ps.setLong(2, 69);
    int i=ps.executeUpdate();
    if(i==0){
        Boolean flag=false;
        System.out.println(flag);
    }
	DataSourceUtils.releaseConnection(conn, ds);

}
//方式三:通过实体存二进制数据
if(StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
	//获取file文件
	CommonsMultipartFile cmf = (CommonsMultipartFile) multipartFile;
	DiskFileItem dfi = (DiskFileItem)cmf.getFileItem();
	File file = dfi.getStoreLocation();
	//转成流
    InputStream ips = new FileInputStream(file);
    //转成字节数组
	byte[] byteData = new byte[(int) file.length()];
	ips.read(byteData, 0, byteData.length);
	
	PrePlan p = new PrePlan();
	p.setAttachment(byteData);
	//保存实体
	
}


//原生sql方式取二进制数据,转成文件
if(true) {
    DataSource ds = this.jdbcTemplate.getDataSource();
	Connection conn = ds.getConnection();
	Statement  stmt = conn.createStatement();
    String sql = "select attachment from tpm_plan where id = 40";
    ResultSet  rs = stmt.executeQuery(sql);
    while (rs.next()){
        OutputStream ops = null;
        InputStream ips = null;
        File file = new File("d:" + File.separator + "11.docx");
        try {
           ips = rs.getBinaryStream(1);
           byte[] buffer = new byte[ips.available()];//or other value like 1024
           ops = new FileOutputStream(file);
           for (int i; (i = ips.read(buffer)) > 0;)
           {
                ops.write(buffer, 0, i);
                ops.flush();
           }
        }
        catch (Exception ex){
            ex.printStackTrace(System.out);
        }
        finally {
            ips.close();
            ops.close();
        }
    }
}

//实体方式取二进制数据,转成文件
if(true) {
	String sql = "from tpm_plan where id='" + 69+"'";	
	PrePlan p = new PrePlan();
    OutputStream ops = null;
	try {
		p = getPrePlanById((long)69);
		File file = new File("d:" + File.separator + "12.docx");
        try {		                   
           byte[] buffer = new byte[1024];//or other value like 1024
           ops = new FileOutputStream(file);
           BufferedOutputStream  stream = new BufferedOutputStream(ops);
           stream.write(p.getAttachment()); 
        } catch (Exception ex) {
        ex.printStackTrace(System.out);
        } finally {
            ops.close();
        }
	} catch (Exception e) {
		e.printStackTrace();
	}
	
}


你可能感兴趣的:(JAVA)