欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于java实现图片上传

程序员文章站 2022-03-05 14:24:48
...
 //取到图片的名称xxx.jpg/png/
    public static List getPic(String path) {
        List list = new ArrayList();
        File file = new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            String fileName = file1.getName();
            String prefix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            if ("jpg".equals(prefix) || "png".equals(prefix)
                    || "bmp".equals(prefix) || "gif".equals(prefix)) {
                list.add(fileName);

            }
        }

        return list;
    }


    //字符串切割 按需求进行切割处理
    public static String[] getInfo(String fileName) {

        if (!Objects.isNull(fileName)) {
            String[] split = fileName.split("\\.");
            if (split.length == 2) {
                String name = split[0];
                return name.split("_");

            }
        }
        return new String[]{};
    }



    //图片上传
    public void UpLoad(String path) {
        File file = new File(path);
        //获取该Path下的所有文件
        String[] photoName = file.list();
        List list = getPic(path);
        String[][] strings = new String[list.size()][];

        Connection connection = null;
        PreparedStatement pstmt = null;

        for (int i = 0; i < list.size(); i++) {
            strings[i] = getInfo((String) list.get(i));
        }

        for (int i = 0; i < strings.length; i++) {
            String sql = "insert into ele_signature (orgCode,id,   owner,pictureB) values('" + strings[i][0] + "','" + strings[i][1] + "','" + strings[i][2] + "'," + "empty_blob())";
            System.out.println(sql);
            Connection conn = null;
            Statement stmt = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                conn = DriverManager.getConnection(url, userName, password);
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                int rt = stmt.executeUpdate(sql);
                if (rt > 0) {
                    System.out.println("initialization succeed!!");
                    String preCursor = "select orgCode,id,owner,pictureB from ele_signature where id='" + strings[i][1] + "' for update";
                    rs = stmt.executeQuery(preCursor);
                    rs.next();
                    Blob b = (Blob) rs.getBlob("pictureB");
                    BufferedOutputStream bos = new BufferedOutputStream(b.setBinaryStream(0L));
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(path + "\\" + (String) getPic(path).get(i))));
                    int bytes;
                    try {
                        while ((bytes = bis.read()) != -1) {
                            bos.write(bytes);
                        }
                        System.out.println("第" + i + "插入成功");
                        bis.close();
                        bos.close();
                        conn.commit();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            } catch (SQLException | FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    rs.close();
                    stmt.close();
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }