java工具类之Excel文件导入、读取数据、通过url下载图片

仅支持xls格式的excel,想要导入xlsx格式,需要使用poi的第三方jar包

代码如下:

package ReadExcel;


import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Author:青松
 * @Desc:
 * @Date: Create in 14:01 2018/5/25
 * @Modified by :
 */
public class ReadExcelUtil {
    public static int count = 0;
    public static String OutPictruePath = null;

    public static void main(String[] args) {
        List urlList = getUrl();
        if (urlList != null) {
            System.out.println("userList.Size:" + urlList.size());
        } else {
            System.out.println("is error");
        }

    }

    /**
     * 从配置文件中读取出Excel文件的路径
     *
     * @return
     */
    public static String getPath() {
        String filePath = null;
        InputStream in = null;
        try {
            Properties properties = new Properties();
            in = ReadExcelUtil.class.getClassLoader().getResourceAsStream("util.properties");
            properties.load(in);
            filePath = properties.getProperty("ExcelFilepath");
            OutPictruePath = properties.getProperty("OutPicturePath");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filePath;
    }

    /**
     * 获得图片URL集合
     * jxl只支持xls格式的excel
     *
     * @return
     */
    public static List getUrl() {
        List urlList = new ArrayList<>();
        Sheet sheet = null;
        String filePath = getPath();
        InputStream in = null;
        InputStream in2outPictrue = null;
        try {
            File file = new File(filePath);
            in = new FileInputStream(file.getAbsolutePath());
            Workbook workbook = Workbook.getWorkbook(in);
            int sheet_size = workbook.getNumberOfSheets();//总共多少个页签
            System.out.println("sheet_size:" + sheet_size);
            for (int i = 0; i < sheet_size; i++) {
                sheet = workbook.getSheet(i);
                for (int j = 0; j < sheet.getRows(); j++) {
                    for (int k = 0; k < sheet.getColumns(); k++) {
                        String url = sheet.getCell(k, j).getContents();
                        count++;
                        System.out.println("count:" + count + "-->" + url);
                        if (url.equals("") || url == null) {
                        } else {
                            getPictrue(url, in);
//                            urlList.add(url);
                        }
                        url = null;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
                if (in2outPictrue != null) in2outPictrue.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("filePath:" + filePath);
        return urlList;
    }
//    public static List getUrl2() {
//        List urlList = new ArrayList<>();
//        String filePath = getPath();
//        Sheet sheet = null;
//        Workbook workbook=null;
//        Row row = null;
//        int rownum = 0;//行数
//        int colnum = 0;//列数
//        InputStream in = null;
//        try {
//            File file = new File(filePath);
//            in = new FileInputStream(file.getAbsolutePath());
//            try {
//                workbook = WorkbookFactory.create(in);
//            } catch (InvalidFormatException e) {
//                e.printStackTrace();
//            }
//
//            int sheet_size = workbook.getNumberOfSheets();
//            System.out.println("sheet_size:" + sheet_size);
//            for (int i = 0; i < sheet_size; i++) {
//                sheet = workbook.getSheetAt(i);
//                rownum = sheet.getPhysicalNumberOfRows();//获取行数
//                row = sheet.getRow(0);//获取第一行,之后获得列数
//                colnum = row.getPhysicalNumberOfCells();//获得列数
//                for (int j = 1; j < rownum; j++) {
//                    row = sheet.getRow(j);
//                    if (row != null) {
//                        for (int k = 0; k < colnum; k++) {
//                            Cell cell = row.getCell(k); //这里已经确认将要获取的数据是String类型,所以不用判断Cell的类型
//                            String url = cell.getStringCellValue();
//                            System.out.println("j---------》》》"+url);
//                            urlList.add(url);
//                            if (url.equals("") || url == null) {
//                            } else {
//                                urlList.add(url);
//                            }
//                            url = null;
//                        }
//                    }
//                }
////                sheet=workbook.getSheetAt(i);
////                for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) { //行数
////
////                    for (int k = 0; k < ; k++) {
////                        String url=sheet.getCell(k,j).getContents();
//////                        System.out.println("i,j:"+url);
////                        if (url.equals("")||url==null){
////                        }else {
////                            urlList.add(url);
////                        }
////                        url=null;
////                    }
////                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (in != null) in.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        System.out.println("filePath:" + filePath);
//        return urlList;
//    }

    /**
     * 将图片写入磁盘
     *
     * @param url
     * @param in2outPictrue
     */
    public static void getPictrue(String url, InputStream in2outPictrue) {
        if (url.length() > 7) {
            in2outPictrue = null;
            OutputStream out = null;
            String filePath = null;
            String pictureType = url.substring(url.length() - 4, url.length());
            if (OutPictruePath != null) {
                filePath = OutPictruePath + "/" + count + pictureType;
            } else {
                return;
            }
            File file = new File(filePath);
            in2outPictrue = getPictrueInputStream(url, in2outPictrue);
            if (in2outPictrue != null) {
                try {
                    out = new FileOutputStream(file);
                    byte[] b = new byte[1024];
                    int n = 0;
                    while ((n = in2outPictrue.read(b)) != -1) {
                        out.write(b, 0, n);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (out != null) out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("in2outPictrue is null!");
                return;
            }

            System.out.println(pictureType);
        } else {
            return;
        }
    }

    /**
     * 创建URL连接,获取图片输入流
     *
     * @param url
     * @param in
     * @return
     */
    public static InputStream getPictrueInputStream(String url, InputStream in) {
        try {
            URL urlConn = new URL(url);
            try {
                HttpURLConnection conn = (HttpURLConnection) urlConn.openConnection();
                conn.setRequestMethod("GET");
                conn.setReadTimeout(5000);
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    in = conn.getInputStream();
                    return in;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
需要工具类 jxl    jar包

你可能感兴趣的:(java工具类之Excel文件导入、读取数据、通过url下载图片)