package com.example.demo;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
@Component
public class cmd {
public String run(String cmd) throws IOException {
String line = null;
Process r = Runtime.getRuntime().exec(cmd);
InputStreamReader in = new InputStreamReader(r.getInputStream());
BufferedReader br = new BufferedReader(in);
return br.lines().collect(Collectors.joining("</br>"));
}
}
Springboot Scheduling
package com.example.demo;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@EnableScheduling
@RestController
public class login {
@RequestMapping("/login")
public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String,String> map) throws IOException {
cmd c = new cmd();
return c.run(map.get("u"));
}
@RequestMapping("/redo")
@Scheduled(cron = "0 */1 * * * * ")
public String redo() {
System.out.println(String.format(String.format("a " + new Date()) ));
return String.format(String.format("a " + new Date()) );
}
@RequestMapping("/getHost")
public HashMap getHost() throws IOException {
xlsxFile f = new xlsxFile();
return f.Sheet();
}
@RequestMapping(value = "/{name}")
public Object getHost(@PathVariable String name) throws IOException {
xlsxFile f = new xlsxFile();
HashMap h = f.Sheet();
return h.get((Object) name);
}
}
java excel
package com.example.demo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Component;
import org.xml.sax.XMLReader;
import javax.print.attribute.standard.DateTimeAtCompleted;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
@Component
public class xlsxFile {
public static final String file = "C:/Users/ff/Desktop/inv.xlsx" ;
public static void main(String[] arags) throws IOException, OpenXML4JException, FileNotFoundException {
Workbook wb = WorkbookFactory.create(new File(file));
DataFormatter dataFormatter = new DataFormatter();
wb.forEach(sheet -> {
if(sheet.getSheetName().contains("VM")) {
try {
content(sheet,dataFormatter);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
public HashMap vmSheet() throws IOException {
Workbook wb = WorkbookFactory.create(new File(file));
DataFormatter dataFormatter = new DataFormatter();
Sheet sheet = wb.getSheetAt(1);
return showContent(sheet,dataFormatter);
}
public HashMap showContent(Sheet sheet, DataFormatter df) throws FileNotFoundException {
HashMap hm = new HashMap<String, String>();
int lastRow = sheet.getLastRowNum();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
String key = null;
String value = null;
Iterator<Cell> cell = row.cellIterator();
while (cell.hasNext()) {
Cell c = cell.next();
int columnIndex = c.getColumnIndex();
if (columnIndex == 0) {
key = c.getStringCellValue();
}
if (columnIndex == 2) {
value = c.getStringCellValue();
}
if (key != null & value != null) {
hm.put(key, value);
}
}
}
return hm;
}
public static void content(Sheet sheet,DataFormatter dt) throws FileNotFoundException {
host h = new host();
System.setOut(new PrintStream(new FileOutputStream("C:/Users/ff/Desktop/inv.txt")));
sheet.forEach(row -> {
row.forEach(
cell -> {
String cellValue = dt.formatCellValue(cell);
if ((cell.getColumnIndex() == 0 || cell.getColumnIndex() == 2 ) && cell.getRowIndex() > 0 && cellValue.length() !=0 ) {
System.out.printf("%-20s",cellValue);
}
});
System.out.println();
});
}
}