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

getUrl2() {

// ListurlList = 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值