SoFunction
Updated on 2025-03-06

How to convert java remote file url to input stream

java remote file url to input stream

URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection)();
//Set the timeout to 3 seconds(3*1000);
//Prevent blocking program from crawling and returning 403 error("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//Get input streamInputStream inputStream = ();
public static AjaxModel parseExcelForInfo(InputStream inputStream, String fileName, int taskId) {
    try {
        //Create a workbook object        Workbook workbook = null;
        if ((".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if ((".xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else {
            return (-1, "Incorrect file type");
        }
        //Get the first sheet        Sheet sheetAt = (0);
        if (sheetAt != null) {
            // TODO checks excel header            Row headRow = (0);
            for (int i = 0; i < BusinessSettlementConstants.TEMPLATE_COULMN.length; i++) {
                if (!((i)).trim().equals(BusinessSettlementConstants.TEMPLATE_COULMN[i])) {
                    ("parseExcelForInfo excelThe header information order is incorrect,getCellFormatValue((i)):{}," +
                                    "BusinessSettlementConstants.TEMPLATE_COULMN[i]:{},taskId:{}", ((i)),
                            BusinessSettlementConstants.TEMPLATE_COULMN[i], taskId);
                    return ("The excel header order is incorrect:" + ((i)));
                }
            }
            int startRowNum = () + 1;
            int lastRowNum = ();
 
            ("AnalysisexcelstarttaskId:{},from【{}】行start,Arrive【{}】End of line", taskId, startRowNum, lastRowNum);
            List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
            for (int rowNum = startRowNum; rowNum <= lastRowNum; rowNum++) {
                // Each row of data                Row row = (rowNum);
                Map<String, String> map = new HashMap<>();
                ("parseExcelForInfo row:{}", row);
                if (row != null && (0) != null && ((0).getStringCellValue()) && (2) != null && (4) != null) {
                    ("parseExcelForInfo row:{},cell:{}", row, (0));
                    // Name                    ("userName", ((0)));
                    
                    (map);
                }
            }
            ("--------------Analysis完成 dataList:{}", dataList);
            if (() <= 0) {
                return (-1, "The parsed table data is empty");
            }
            AjaxModel success = ();
            ().put("dataList", dataList);
            return success;
        } else {
            ("sheet content is empty");
            return (-1, "The table content is empty");
        }
    } catch (Exception e) {
        ("parseExcelForInfo parsing exception", e);
    }
    return (-1, "Analysis table exception");
}
public static String getCellFormatValue(Cell cell) {
    ();
    return ();
}

Get input stream according to URL URL

Method 1

//File access pathString url = "";
InputStream intstream = new URL(url).openStream();

Method 2

public InputStream getInputStreamByUrl(String strUrl) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) ();
            ("GET");
            (20 * 1000);
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            ((), output);
            return new ByteArrayInputStream(());
        } catch (Exception e) {
            ("getInputStreamByUrl abnormal,exception is {}", e);
        } finally {
            try {
                if (conn != null) {
                    ();
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

The above is personal experience. I hope you can give you a reference and I hope you can support me more.