java实现上传文件二进制存入并读取数据库

首先数据库字段我们要设置为longblob
如下:
在这里插入图片描述
实体类字段设置为 byte[]类型

private byte[] byteName;

接下来我们写存入数据库的代码:

	@PostMapping(path = "/testFile")
    @ResponseBody
    public void testFile() throws FileNotFoundException, ClassNotFoundException, SQLException {
        //Connection db = null;
        File file = new File("D:\\Windchill.zip");
        //int length = (int) file.length();
        FileInputStream fi = null;
        fi = new FileInputStream(file);
        lenovoService.testFile(fi);
        /*Class.forName("com.mysql.jdbc.Driver");
        db = DriverManager.getConnection("jdbc:mysql://service.earth.xpaas.lenovo.com:33051/plmss?useUnicode=true&characterEncoding=utf-8","scmadmin","VcmU1SAlet4fv4bJ");
        System.out.println(db);
        PreparedStatement preparedStatement = db.prepareStatement("update orderinfo set filesname=? where id = ?");
        preparedStatement.setBinaryStream(1,fi,length);
        preparedStatement.setInt(2,1);
        preparedStatement.execute();
        db.close();*/
    }

取出数据库:

	@PostMapping(path = "/testFileTwo")
    @ResponseBody
    public String testFileTwo() throws FileNotFoundException, ClassNotFoundException, SQLException {
        OrderInfo orderInfo = lenovoService.testFileTwo();
        byte[] filesname = orderInfo.getByteName();
        ByteToFileUtil.getFile(filesname,"D:\\backup","321.zip");
        return "true";
    }

ByteToFileUtil(将二进制流转换为文件的工具类):

package com.*.utils;

import java.io.*;

public class ByteToFileUtil {
    /**
     * 获得指定文件的byte数组
     */
    private byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 根据byte数组,生成文件
     */
    public static void getFile(byte[] bfile, String filePath,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+"\\"+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(java实现上传文件二进制存入并读取数据库)