前序:如标题所示,主要是贴核心代码。基本能用。都可以用的,我亲测过了。
//方式一:将文件转成字节数组,采用原生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,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,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,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,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(); } }总结
以上是内存溢出为你收集整理的postgresql数据库保存和下载二进制文件的几种方式全部内容,希望文章能够帮你解决postgresql数据库保存和下载二进制文件的几种方式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)