File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed
main/java/io/excel/object/mapper/resource
test/java/io/excel/object/mapper/resource Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .excel .object .mapper .resource ;
2+
3+ import org .apache .poi .ss .usermodel .Workbook ;
4+ import org .apache .poi .ss .usermodel .WorkbookFactory ;
5+
6+ import java .io .IOException ;
7+ import java .io .InputStream ;
8+ import java .net .HttpURLConnection ;
9+ import java .net .URL ;
10+ import java .util .Objects ;
11+
12+ /**
13+ * @author woniper
14+ */
15+ public class HttpFileExcelResource implements ExcelResource {
16+
17+ @ Override
18+ public Workbook getResource (String resource ) throws IOException {
19+ URL url = new URL (resource );
20+ HttpURLConnection connection = (HttpURLConnection )url .openConnection ();
21+ connection .setRequestMethod ("GET" );
22+
23+ try (InputStream inputStream = connection .getInputStream ()) {
24+ if (Objects .nonNull (inputStream )) {
25+ return WorkbookFactory .create (inputStream );
26+ }
27+ } catch (Exception e ) {
28+ e .printStackTrace ();
29+ }
30+
31+ return WorkbookFactory .create (true );
32+ }
33+
34+ }
Original file line number Diff line number Diff line change 1+ package io .excel .object .mapper .resource ;
2+
3+ import org .apache .poi .ss .usermodel .Workbook ;
4+ import org .junit .Test ;
5+
6+ import java .io .IOException ;
7+
8+ import static org .assertj .core .api .Assertions .assertThat ;
9+
10+ /**
11+ * @author woniper
12+ */
13+ public class ExcelResourceTest {
14+
15+ @ Test
16+ public void excel_파일을_내려받아_Workbook으로_반환 () throws IOException {
17+ // given
18+ String url = "http://download.microsoft.com/download/1/4/E/14EDED28-6C58-4055-A65C-23B4DA81C4DE/Financial%20Sample.xlsx" ;
19+ ExcelResource resource = new HttpFileExcelResource ();
20+
21+ // when
22+ Workbook workbook = resource .getResource (url );
23+
24+ // then
25+ assertThat (workbook .getNumberOfSheets ()).isEqualTo (1 );
26+ assertThat (workbook .getSheetAt (0 ).getPhysicalNumberOfRows ()).isEqualTo (701 );
27+ }
28+
29+ @ Test
30+ public void resources에_있는_excel_파일을_Workbook으로_반환 () throws IOException {
31+ // given
32+ String fileName = "company.xlsx" ;
33+ ExcelResource excelResource = new ResourcesExcelResource ();
34+
35+ // when
36+ Workbook workbook = excelResource .getResource (fileName );
37+
38+ // then
39+ assertThat (workbook .getNumberOfSheets ()).isEqualTo (1 );
40+ assertThat (workbook .getSheetAt (0 ).getPhysicalNumberOfRows ()).isEqualTo (4 );
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments