获取数据库中所有包含图片的表,查询图片url并导出text、excel、下载text

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39556804/article/details/80578321
package com.demodashi.ExportImgURL;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: 
 * @Date:Create in 11:34 2018/6/4
 * @Description: 导出数据库中的图片url
 */

@Controller
public class ExportImgURL {
    private final Logger LOGGER = LoggerFactory.getLogger(ExportImgURL.class);
    /**可以换成main方法,测试使用,导出到文件夹**/
    @RequestMapping(value = "/down.do")
    public void down(HttpServletResponse response) throws Exception {
        LOGGER.info("导出开始");
        List<String> urls = getAllUrl();
        //        exproExcel(urls, "E:\\导出\\url.xls");
        //        exportText(urls, "C:/Users/Administrator/Desktop/图片url.txt");
        writeText(response, urls, "url");
        LOGGER.info("导出结束");
    }

    //下载
    private void writeText(HttpServletResponse response, List<String> urls, String fileName) {
        BufferedOutputStream buff = null;
        ServletOutputStream outStr = null;
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName + ".txt");
        try {
            outStr = response.getOutputStream();
            buff = new BufferedOutputStream(outStr);
            for (String url : urls) {
                buff.write(url.getBytes("UTF-8"));
                buff.write('\n');
            }
            buff.flush();
            buff.close();
        } catch (Exception e) {
            LOGGER.error("导出文件文件出错,e:{}", e);
        } finally {
            try {
                buff.close();
                outStr.close();
            } catch (Exception e) {
                LOGGER.error("关闭流对象出错 e:{}", e);
            }
        }
    }

    //导出
    private void exportText(List<String> urls, String path) {
        FileWriter fw;
        BufferedWriter bw = null;
        try {
            File file = new File(path);
            fw = new FileWriter(file);
            bw = new BufferedWriter(fw);

            if (!file.exists()) {
                file.createNewFile();
            }

            for (String url : urls) {
                bw.write(url);
                bw.newLine();
            }
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                LOGGER.info("输出完毕 !");
            } catch (IOException e) {
                LOGGER.info(e.toString() + e);
            }
        }
    }

    //导出excel
    private void exportExcel(List<String> urls, String path) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("图片url");
        HSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("img url");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            for (int i = 1; i <= urls.size(); i++) {
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(urls.get(i));
            }
            wb.write(os);
        } catch (IOException e) {
            LOGGER.error("导出错误 》》" + e);
        }
        byte[] content = os.toByteArray();
        File file = new File(path);
        OutputStream fos;
        try {
            fos = new FileOutputStream(file);
            fos.write(content);
            os.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<String> getAllUrl() throws Exception {
        Connection conn = null;
        List<String> urls = new ArrayList<>();
        try {
            conn = getCon();
            Set<String[]> tabls = getTabls();
            for (String[] tab : tabls) {
                for (int i = 0; i < tab.length; i++) {
                    if (i == tab.length - 1) {
                        continue;
                    }
                    List<String> allUrl = getUrls(tab[0], tab[i + 1], conn);
                    for (String url : allUrl) {
                        if (url.indexOf(",") > 0) {
                            String[] urlArry = url.split(",");
                            for (String oneUrl : urlArry) {
                                oneUrl = oneUrl.replaceAll("\"", "").replaceAll("[\\[\\]]", "");替换url中的引号,和中括号,可以修改成不需要的符号
                                if (oneUrl.startsWith("http")) {
                                    continue;
                                }
                                urls.add(oneUrl);
                            }
                        } else {
                            url = url.replaceAll("\"", "").replaceAll("[\\[\\]]", "");
                            if (url.startsWith("http")) {
                                continue;
                            }
                            urls.add(url);
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("查询出错" + e);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        LOGGER.info("总共数量:" + urls.size());
        return urls;
    }

    private List<String> getUrls(String tab, String url, Connection conn) {
        QueryRunner runner = new QueryRunner();
        String sql = "SELECT " + url + " FROM " + tab;
        List<String> list = new ArrayList<>();
        try {
            LOGGER.info(sql);
            list = runner.query(conn, sql, new ColumnListHandler<String>());
            LOGGER.info(tab + " url:" + url + "查询数据:" + list);
        } catch (SQLException e) {
            LOGGER.error("查询错误 》》 表:" + tab + sql + e);
        }
        return list;
    }

    
    private Set<String[]> getTabls() {
        Set<String[]> tabls = new HashSet<>();
      //如果数据库表太多,需要导出表名和字段少,就手写表和字段
        tabls.add(new String[] { "tab1", "icon" });
        tabls.add(new String[] { "tab2", "url" ,"字段2","字段3"});
        tabls.add(new String[] { "tab3", "icon" ,"字段2"});
        tabls.add(new String[] { "tab4", "url" });
        
        return tabls;
    }

    //获取数据库中所有字段包含url的表和字段
    private List<Map<String, Object>> getTabls(Connection conn) {
        String sql = "SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT FROM INFORMATION_SCHEMA. COLUMNS WHERE TABLE_SCHEMA = '表名' AND COLUMN_NAME LIKE '%url%'";
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            QueryRunner runner = new QueryRunner();
            list = runner.query(conn, sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private Connection getCon() {
        String username = "username";
        String password = "password";
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_39556804/article/details/80578321