<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class RemoteFileUtils {
public static String getTextByFileUrl(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3 * 60 * 1000);
InputStream inputStream = conn.getInputStream();
byte[] getData = null ;
try{
getData = readInputStream(inputStream);
}catch (Exception e){
throw new IOException(e);
}finally {
inputStream.close();
}
return new String(getData, "utf-8");
}
public static List<Integer> getPoiIdsByFileUrl(String fileUrl) throws IOException {
List<Integer> allRows = new ArrayList<Integer>();
InputStream is = null;
Workbook wb = null;
try {
URL url = new URL(fileUrl);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3 * 60 * 1000);
is = conn.getInputStream();
wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheetAt(0);
int maxRowNum = sheet.getLastRowNum();
int minRowNum = sheet.getFirstRowNum();
for (int i = minRowNum; i <= maxRowNum; i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
Cell cell = row.getCell(0);
if (cell == null) {
continue;
}
cell.setCellType(Cell.CELL_TYPE_STRING);
String str = cell.getStringCellValue() ;
if((str != null && StringUtils.isNotEmpty(str.trim()) ) && isNumber(str)){
allRows.add(Integer.parseInt(str)) ;
}
}
} catch (Exception e) {
throw new IOException(e);
} finally {
if (is != null) {
is.close();
}
if (wb != null && wb instanceof SXSSFWorkbook) {
SXSSFWorkbook xssfwb = (SXSSFWorkbook) wb;
xssfwb.dispose();
}
}
return allRows;
}
public static boolean checkShopIdFile(InputStream inputStream) throws IOException{
Workbook workbook = null ;
try {
workbook = WorkbookFactory.create(inputStream) ;
Sheet sheet = workbook.getSheetAt(0) ;
int minRowNum = sheet.getFirstRowNum() ;
int maxRowNum = sheet.getLastRowNum() ;
for(int $i = minRowNum ; $i <= maxRowNum ; $i++){
Row row = sheet.getRow($i) ;
if(row == null){
continue ;
}
Cell cell = row.getCell(0);
if (cell == null) {
continue;
}
cell.setCellType(Cell.CELL_TYPE_STRING);
String str = cell.getStringCellValue() ;
if((str != null && StringUtils.isNotEmpty(str.trim()) ) && !isNumber(str)){
return false ;
}
}
} catch (Exception e) {
throw new IOException(e);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (workbook != null && workbook instanceof SXSSFWorkbook) {
SXSSFWorkbook xssfwb = (SXSSFWorkbook) workbook;
xssfwb.dispose();
}
}
return true ;
}
private static boolean isNumber(String str){
for(char c : str.toCharArray()){
if(! Character.isDigit(c)){
return false ;
}
}
return true ;
}
private static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
private static List<Integer> readLine(Row row) {
short minColNum = row.getFirstCellNum();
short maxColNum = row.getLastCellNum();
List<Integer> dataList = new ArrayList<Integer>();
for (short colIndex = minColNum; colIndex < maxColNum; colIndex++) {
Cell cell = row.getCell(colIndex);
if (cell == null) {
continue;
}
int cellType = cell.getCellType();
Integer value = null;
if (Cell.CELL_TYPE_NUMERIC == cellType) {
value = (int) cell.getNumericCellValue();
} else if (Cell.CELL_TYPE_STRING == cellType && NumberUtils.isNumber(cell.getStringCellValue())) {
value = Integer.valueOf(cell.getStringCellValue());
}
if(value != null){
dataList.add(value);
}
}
return dataList;
}
}
MultipartFile file;
InputStream inputStream = file.getInputStream();