使用JavaFX写的一个简单的工具类,可以处理一些简单的文件切割、合并、PDF转换;redis、数据库连接测试。shp文件投影、shp文件入库及导入导出操作。如涉及矢量数据操作,需要安装gdal环境。工具包已上转,资源地址https://download.youkuaiyun.com/download/jackspring2010/12926646
主要代码如下:
一、 FXML文件
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import javafx.collections.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <?import javafx.collections.FXCollections?> <?import java.lang.String?> <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.piesat.java.AppController"> <tabs> <Tab text="空间数据处理"> <content> <Accordion> <panes> <TitledPane animated="false" text="EsriJson转GeoJson"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="28.0" layoutY="31.0" text="请选择json文件" /> <TextField fx:id="jsonFile" disable="true" editable="false" layoutX="163.0" layoutY="26.0" prefHeight="30.0" prefWidth="487.0" promptText="请选择Json文件路径" /> <Button layoutX="661.0" layoutY="26.0" mnemonicParsing="false" onAction="#handleGeoJSONSelectAction" text="选择" /> <Button layoutX="726.0" layoutY="26.0" mnemonicParsing="false" onAction="#handleGeoJSONTransformAction" text="转换" /> </children> </AnchorPane> </content> </TitledPane> <TitledPane animated="false" text="PostGis数据导入导出"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="546.0" layoutY="45.0" text="PostGreSQL参数配置"> <font><Font size="18.0" /></font> </Label> <Label layoutX="494.0" layoutY="117.0" text="地址" /> <TextField fx:id="postIp" layoutX="560.0" layoutY="112.0" promptText="请输入数据库地址" text="127.0.0.1" /> <Label layoutX="494.0" layoutY="167.0" text="端口" /> <TextField fx:id="postPort" layoutX="560.0" layoutY="162.0" promptText="请输入数据库端口" text="5432" /> <Label layoutX="494.0" layoutY="217.0" text="用户名" /> <TextField fx:id="postUsername" layoutX="560.0" layoutY="214.0" promptText="请输入数据库用户名" text="postgres" /> <Label layoutX="494.0" layoutY="267.0" text="密码" /> <TextField fx:id="postPassword" layoutX="560.0" layoutY="267.0" promptText="请输入数据库用密码" /> <Label layoutX="494.0" layoutY="317.0" text="数据库" /> <TextField fx:id="postDatabase" layoutX="560.0" layoutY="317.0" promptText="请输入数据库名称" /> <Separator layoutX="472.0" orientation="VERTICAL" prefHeight="452.0" prefWidth="0.0" /> <Label layoutX="30.0" layoutY="76.0" text="请选择Shp文件" /> <TextField fx:id="postShpFile" disable="true" editable="false" layoutX="157.0" layoutY="71.0" /> <Button layoutX="386.0" layoutY="71.0" mnemonicParsing="false" onAction="#handleShpFileSelect" text="选择" /> <Label layoutX="30.0" layoutY="115.0" text="请输入新建表名称" /> <TextField fx:id="postImportTable" layoutX="157.0" layoutY="110.0" /> <Label layoutX="30.0" layoutY="158.0" text="请选择EPSG编号" /> <ChoiceBox fx:id="postImportShpEpsg" layoutX="157.0" layoutY="153.0" prefWidth="200.0"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="4326" /> <String fx:value="3857" /> <String fx:value="4490" /> <String fx:value="4524" /> </FXCollections> </items> </ChoiceBox> <Label layoutX="14.0" layoutY="21.0" text="数据导入" wrapText="true"> <font><Font size="18.0" /></font> </Label> <Button layoutX="215.0" layoutY="199.0" mnemonicParsing="false" onAction="#handleShpFileImport" text="导入" /> <Label layoutX="14.0" layoutY="240.0" text="数据导出" wrapText="true"> <font><Font size="18.0" /></font> </Label> <Label layoutX="30.0" layoutY="280.0" text="请输入导出表名称" /> <TextField fx:id="postExportTable" layoutX="164.0" layoutY="280.0" /> <Label layoutX="30.0" layoutY="320.0" text="请选择EPSG编号" /> <ChoiceBox fx:id="postExportShpEpsg" layoutX="164.0" layoutY="320.0" prefWidth="200.0"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="4326" /> <String fx:value="3857" /> <String fx:value="4490" /> <String fx:value="4524" /> </FXCollections> </items> </ChoiceBox> <Label layoutX="30.0" layoutY="360.0" text="请选择Shp文件" /> <TextField fx:id="postShpDir" disable="true" editable="false" layoutX="163.0" layoutY="360.0" /> <Button layoutX="386.0" layoutY="360.0" mnemonicParsing="false" onAction="#handleShpExportDirSelect" text="选择" /> <Button layoutX="215.0" layoutY="400.0" mnemonicParsing="false" onAction="#handleShpFileExport" text="导出" /> </children> </AnchorPane> </content> </TitledPane> <TitledPane animated="false" text="Shp投影转换"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="38.0" layoutY="47.0" text="请选择Shp文件" /> <TextField fx:id="projShpFile" disable="true" editable="false" layoutX="177.0" layoutY="42.0" prefHeight="30.0" prefWidth="300.0" promptText="请选择需要投影的shp文件" /> <Button layoutX="480.0" layoutY="42.0" mnemonicParsing="false" onAction="#handleProjShpFileSelect" text="选择" /> <Label layoutX="38.0" layoutY="96.0" text="请选择EPSG编号" /> <ChoiceBox fx:id="shpProjEpsg" layoutX="176.0" layoutY="91.0" prefWidth="300.0"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="4326" /> <String fx:value="3857" /> <String fx:value="4490" /> <String fx:value="4524" /> </FXCollections> </items> </ChoiceBox> <Button layoutX="365.0" layoutY="161.0" mnemonicParsing="false" onAction="#handleShpProjTransform" text="转换" /> </children></AnchorPane> </content> </TitledPane> </panes> </Accordion> </content> </Tab> <Tab text="文件处理"> <content> <Accordion> <panes> <TitledPane animated="false" text="文件分割"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="14.0" layoutY="41.0" text="请选择要切割的文件" /> <TextField fx:id="splitFile" disable="true" editable="false" layoutX="176.0" layoutY="36.0" prefHeight="30.0" prefWidth="471.0" promptText="请选择要切割的文件" /> <Button layoutX="690.0" layoutY="36.0" mnemonicParsing="false" onAction="#handleSplitFileSelect" text="选择" /> <Label layoutX="13.0" layoutY="84.0" text="请输入切割文件大小" /> <TextField fx:id="splitSize" layoutX="175.0" layoutY="80.0" prefHeight="30.0" prefWidth="471.0" promptText="请输入切割文件大小" /> <Label layoutX="13.0" layoutY="130.0" text="请选择输出路径" /> <TextField fx:id="splitOutDir" disable="true" editable="false" layoutX="175.0" layoutY="125.0" prefHeight="30.0" prefWidth="471.0" promptText="请选择分割文件存储路径" /> <Button layoutX="689.0" layoutY="125.0" mnemonicParsing="false" onAction="#handleSplitOutDirSelect" text="选择" /> <Button layoutX="374.0" layoutY="187.0" mnemonicParsing="false" onAction="#handleSplitFile" text="处理" /> </children> </AnchorPane> </content> </TitledPane> <TitledPane animated="false" text="文件合并"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="41.0" layoutY="37.0" text="请选择要合并的文件夹" /> <TextField fx:id="combDir" disable="true" editable="false" layoutX="209.0" layoutY="32.0" prefHeight="30.0" prefWidth="423.0" /> <Button layoutX="655.0" layoutY="32.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="68.0" text="选择" onAction="#handleCombDirSelect"/> <Label layoutX="41.0" layoutY="78.0" text="请选择输出的路径" /> <TextField fx:id="combFileOutDir" disable="true" editable="false" layoutX="209.0" layoutY="73.0" prefHeight="30.0" prefWidth="423.0" /> <Button layoutX="655.0" layoutY="73.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="68.0" text="选择" onAction="#handleCombFileOutDirSelect"/> <Button layoutX="353.0" layoutY="131.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="68.0" text="处理" onAction="#handleDirFileCombine"/> </children> </AnchorPane> </content> </TitledPane> <TitledPane animated="false" layoutX="10.0" layoutY="70.0" text="文件转PDF"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="37.0" layoutY="32.0" text="请选择要转换的文件" /> <TextField fx:id="officeFile" disable="true" editable="false" layoutX="213.0" layoutY="27.0" prefHeight="30.0" prefWidth="396.0" promptText="请选择要转换的文件" /> <Button layoutX="677.0" layoutY="27.0" mnemonicParsing="false" text="选择" onAction="#handleOfficeFileSelect"/> <Button layoutX="342.0" layoutY="85.0" mnemonicParsing="false" text="转换" onAction="#handleConvert2PDF"/> </children> </AnchorPane> </content> </TitledPane> </panes> </Accordion> </content> </Tab> <Tab text="网络测试"> <content> <Accordion> <panes> <TitledPane animated="false" text="Redis连接测试"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="65.0" layoutY="35.0" text="请输入网络地址" /> <TextField fx:id="redisIp" layoutX="211.0" layoutY="30.0" prefHeight="30.0" prefWidth="396.0" text="127.0.0.1"/> <Label layoutX="65.0" layoutY="86.0" text="请输入端口" /> <TextField fx:id="redisPort" layoutX="211.0" layoutY="81.0" prefHeight="30.0" prefWidth="395.0" text="6379"/> <Label layoutX="65.0" layoutY="143.0" text="请输入密码(如有)" /> <TextField fx:id="redisPassword" layoutX="211.0" layoutY="138.0" prefHeight="30.0" prefWidth="395.0" /> <Button layoutX="340.0" layoutY="198.0" mnemonicParsing="false" text="测试" onAction="#handleRedisConnect"/> </children> </AnchorPane> </content> </TitledPane> <TitledPane animated="false" text="数据库连接测试"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="194.0" layoutY="52.0" text="请选择数据库类型" /> <ChoiceBox fx:id="databaseType" layoutX="345.0" layoutY="52.0" prefWidth="260.0"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="ORACLE" /> <String fx:value="SQLSERVER" /> <String fx:value="MYSQL5" /> <String fx:value="MYSQL8" /> <String fx:value="POSTGRESQL" /> </FXCollections> </items> </ChoiceBox> <Label layoutX="194.0" layoutY="100.0" text="请输入数据库地址" /> <TextField fx:id="databaseIp" layoutX="345.0" layoutY="100.0" prefHeight="30.0" prefWidth="260.0" /> <Label layoutX="194.0" layoutY="150.0" text="请输入数据库端口" /> <TextField fx:id="databasePort" layoutX="345.0" layoutY="150.0" prefHeight="30.0" prefWidth="260.0" /> <Label layoutX="194.0" layoutY="200.0" text="请输入数据库名称" /> <TextField fx:id="databaseName" layoutX="345.0" layoutY="200.0" prefHeight="30.0" prefWidth="260.0" /> <Label layoutX="194.0" layoutY="250.0" text="请输入数据库用户名" /> <TextField fx:id="databaseUsername" layoutX="345.0" layoutY="250.0" prefHeight="30.0" prefWidth="260.0" /> <Label layoutX="194.0" layoutY="300.0" text="请输入数据库密码" /> <TextField fx:id="databasePassword" layoutX="345.0" layoutY="300.0" prefHeight="30.0" prefWidth="260.0" /> <Button layoutX="365.0" layoutY="360.0" mnemonicParsing="false" text="测试" onAction="#handleDataBaseConnect"/> </children> </AnchorPane> </content> </TitledPane> </panes> </Accordion> </content> </Tab> </tabs> </TabPane>
二、控制类
package cn.piesat.java; import cn.piesat.java.utils.*; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextField; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.Stage; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import redis.clients.jedis.Jedis; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.sql.Connection; import java.util.Arrays; import java.util.List; import java.util.Map; /** * @author jackspring */ public class AppController { /** * Json文件 */ @FXML private TextField jsonFile; /** * shp文件 */ @FXML private TextField postShpFile; /** * PostGreSQL地址 */ @FXML private TextField postIp; /** * PostGreSQL端口 */ @FXML private TextField postPort; /** * PostGreSQL用户名 */ @FXML private TextField postUsername; /** * PostGreSQL密码 */ @FXML private TextField postPassword; /** * PostGreSQL数据库 */ @FXML private TextField postDatabase; /** * PostGreSQL导入表 */ @FXML private TextField postImportTable; /** * PostGreSQL导入shp文件EPSG */ @FXML private ChoiceBox<String> postImportShpEpsg; /** * PostGreSQL导出表 */ @FXML private TextField postExportTable; /** * PostGreSQL导出表 */ @FXML private ChoiceBox<String> postExportShpEpsg; /** * PostGreSQL导出文件夹 */ @FXML private TextField postShpDir; /** * 需要投用转换的shp文件 */ @FXML private TextField projShpFile; /** * shp投影转换Epsg */ @FXML private ChoiceBox<String> shpProjEpsg; /** * 需要分割的文件 */ @FXML private TextField splitFile; /** * 文件分割大小 */ @FXML private TextField splitSize; /** * 切割文件输出路径 */ @FXML private TextField splitOutDir; /** * 需要合并的文件夹 */ @FXML private TextField combDir; /** * 合并文件的保存路径 */ @FXML private TextField combFileOutDir; /** * 需要转换PDF的Office文件 */ @FXML private TextField officeFile; /** * Redis地址 */ @FXML private TextField redisIp; /** * Redis端口 */ @FXML private TextField redisPort; /** * Redis密码 */ @FXML private TextField redisPassword; /** * 数据库类型 */ @FXML private ChoiceBox<String> databaseType; /** * 数据库Ip */ @FXML private TextField databaseIp; /** * 数据库端口 */ @FXML private TextField databasePort; /** * 数据库名称 */ @FXML private TextField databaseName; /** * 数据库用户名 */ @FXML private TextField databaseUsername; /** * 数据库密码 */ @FXML private TextField databasePassword; /////////////////////////////////////////////////// 功能点一: Json格式转换 //////////////////////////////////////////////////////////// /** * 选择文件 * * @param event */ @FXML protected void handleGeoJSONSelectAction(ActionEvent event) { fileChooserSelect(jsonFile, "选择Json文件", "json", ".shp"); } /** * JSON格式转换 * * @param event */ @FXML protected void handleGeoJSONTransformAction(ActionEvent event) { String jsonFilePath = jsonFile.getText().trim(); if (StringUtils.isEmpty(jsonFilePath)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "未选择Json文件"); } String jsonStr = AppUtil.readFile(new File(jsonFilePath)); String utilFilePath = this.getClass().getResource("/cn/piesat/resource/arcgis-to-geojson-utils.js").getPath(); String transformJson = ""; if (jsonStr.contains("rings")) { transformJson = AppUtil.esri2geoJson(utilFilePath, jsonStr); } else { transformJson = AppUtil.geo2esriJson(utilFilePath, jsonStr); } Object json = FastJsonUtils.textToJson(transformJson); String resultJsonFilePath = jsonFilePath.replace(".json", "_transform.json"); AppUtil.writeFile(resultJsonFilePath, FastJsonUtils.toJSONNoFeatures(json).replaceAll("\\\\", "")); AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "Json文件转换成功"); } /////////////////////////////////////////////////// 功能点一: Json格式转换 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点二: 数据导入导出 //////////////////////////////////////////////////////////// @FXML protected void handleShpFileSelect(ActionEvent actionEvent) { fileChooserSelect(postShpFile, "选择Json文件", "json", ".shp"); } @FXML protected void handleShpFileImport(ActionEvent actionEvent) { String postShpFileStr = postShpFile.getText().trim(); if (StringUtils.isEmpty(postShpFileStr)) { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "请选择要导入的Shp文件!"); return; } if (checkPostGreSQLConfig()) { return; } String postImportTableStr = postImportTable.getText().trim(); if (StringUtils.isEmpty(postImportTableStr)) { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "请填写要导入的库表信息"); return; } String postImportShpEpsgStr = postImportShpEpsg.getValue(); if (StringUtils.isEmpty(postImportShpEpsgStr)) { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "请选择导入文件的EPSG信息"); return; } Connection connection = JdbcUtil.getConnection("POSTGRESQL", postIp.getText().trim(), postPort.getText().trim(), postDatabase.getText().trim(), postUsername.getText().trim(), postPassword.getText().trim()); if (null != connection) { ShpJdbcBean shpJdbcBean = ShpUtil.parseShpFile(postShpFileStr); // 1, 删除数据库表 JdbcUtil.dropTable(connection, postImportTableStr); // 2, 创建数据库表 JdbcUtil.createTable(connection, postImportTableStr, shpJdbcBean.getFieldList()); // 3, 插入数据 JdbcUtil.insertTable(connection, postImportTableStr, shpJdbcBean); JdbcUtil.close(connection); } else { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "数据库连接失败"); } } /** * 选择导出shp文件的路径 * * @param event */ @FXML protected void handleShpExportDirSelect(ActionEvent event) { dirChooserSelect(postShpDir, "请选择导出shp文件的路径!"); } /** * 导出shp文件 * * @param event */ @FXML protected void handleShpFileExport(ActionEvent event) { String postExportTableStr = postExportTable.getText().trim(); if (!checkPostGreSQLConfig()) { return; } if (StringUtils.isEmpty(postExportTableStr)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请输入需要导出的数据库表!"); return; } if (StringUtils.isEmpty(postShpDir.getText().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择要导出shp文件的位置!"); return; } Connection connection = JdbcUtil.getConnection("POSTGRESQL", postIp.getText().trim(), postPort.getText().trim(), postDatabase.getText().trim(), postUsername.getText().trim(), postPassword.getText().trim()); if (null != connection) { // 1 获取数据库表字段结构 List<JdbcBean> columns = JdbcUtil.getTableField(connection, postExportTableStr); // 2 获取数据库数据 if (null != columns && !columns.isEmpty()) { StringBuilder querySql = new StringBuilder("select "); for (int i = 0; i < columns.size(); i++) { querySql.append(columns.get(i).getName()).append(","); } querySql.append("st_asgeojson(geom) geom from " + postExportTableStr); List<Map<String, Object>> dataList = JdbcUtil.query(connection, querySql.toString(), null); if (null != dataList && !dataList.isEmpty()) { // 3, 导出Shp文件 ShpUtil.createShpFile(postShpDir.getText().trim(), postExportTableStr, columns, dataList); } } JdbcUtil.close(connection); } else { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "数据库连接失败"); } } public boolean checkPostGreSQLConfig() { String postIpStr = postIp.getText().trim(); String postPortStr = postPort.getText().trim(); String postUsernameStr = postUsername.getText().trim(); String postPasswordStr = postPassword.getText().trim(); String postDatabaseStr = postDatabase.getText().trim(); if (StringUtils.isEmpty(postIpStr) || StringUtils.isEmpty(postPortStr) || StringUtils.isEmpty(postUsernameStr) || StringUtils.isEmpty(postPasswordStr) || StringUtils.isEmpty(postDatabaseStr)) { AppUtil.showAlertDialog(Alert.AlertType.WARNING, "请填写PostGreSQL数据库配置信息!"); return false; } else { return true; } } /////////////////////////////////////////////////// 功能点二: 数据导入导出 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点三: Shp文件投影 //////////////////////////////////////////////////////////// /** * 选择需要转换的shp文件 * * @param actionEvent */ @FXML protected void handleProjShpFileSelect(ActionEvent actionEvent) { fileChooserSelect(projShpFile, "选择Json文件", "json", ".shp"); } /** * shp文件投影转换 * * @param actionEvent */ @FXML protected void handleShpProjTransform(ActionEvent actionEvent) { if (StringUtils.isEmpty(projShpFile.getText().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择需要转换的shp文件"); return; } if (null == shpProjEpsg || null == shpProjEpsg.getValue() || StringUtils.isEmpty(shpProjEpsg.getValue().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择需要转换的EPSG编号"); return; } String projShpFilePath = projShpFile.getText().trim(); String projShpName = new File(projShpFilePath).getName().replace(".shp", ""); String resultProjShpFilePath = projShpFile.getText().trim().replace(".shp", "_transform.shp"); String resultProjShpName = "transform_" + projShpName; ShpUtil.shpTransform(projShpFilePath, resultProjShpFilePath, resultProjShpName, shpProjEpsg.getValue()); AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "Shp文件转换成功!"); } /////////////////////////////////////////////////// 功能点三: Shp文件投影 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点四: 文件分割 //////////////////////////////////////////////////////////// @FXML protected void handleSplitFileSelect(ActionEvent actionEvent) { fileChooserSelect(splitFile, "请选择要分割的文件"); } @FXML protected void handleSplitOutDirSelect(ActionEvent actionEvent) { dirChooserSelect(splitOutDir, "请选择文件分割后的存储路径"); } @FXML protected void handleSplitFile(ActionEvent actionEvent) { if (StringUtils.isEmpty(splitFile.getText().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择要分割的文件!"); return; } if (StringUtils.isEmpty(splitSize.getText().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择单个文件的大小!"); return; } if (StringUtils.isEmpty(splitOutDir.getText().trim())) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择分割文件保存的路径!"); return; } File oldFile = new File(splitFile.getText().trim()); try (FileInputStream inputStream = FileUtils.openInputStream(oldFile);) { File temp = null; byte[] buffer = new byte[Integer.parseInt(splitSize.getText())]; int len = 0; // IOUtils.read(inputStream, buffer) 读取完后,再读取则返回值为0 for (int i = 0; (len = IOUtils.read(inputStream, buffer)) > 0; i++) { temp = FileUtils.getFile(splitOutDir.getText().trim(), oldFile.getName() + "_" + String.valueOf(i)); FileUtils.writeByteArrayToFile(temp, buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "文件切割完成!"); } /////////////////////////////////////////////////// 功能点四: 文件分割 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点五: 文件合并 //////////////////////////////////////////////////////////// @FXML protected void handleCombDirSelect(ActionEvent actionEvent) { dirChooserSelect(combDir, "请选择要合并的文件"); } @FXML protected void handleCombFileOutDirSelect(ActionEvent actionEvent) { dirChooserSelect(combFileOutDir, "请选择文件合并后的存储路径"); } @FXML protected void handleDirFileCombine(ActionEvent actionEvent) { String combDirStr = combDir.getText().trim(); String combFileOutDirStr = combFileOutDir.getText().trim(); if (StringUtils.isEmpty(combDirStr)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择要合并的文件"); return; } if (StringUtils.isEmpty(combFileOutDirStr)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择文件合并后的存储路径"); return; } File inputPath = FileUtils.getFile(combDirStr); String[] files = inputPath.list(); File destFile = new File(combFileOutDirStr + File.separator + "combine"); try (FileOutputStream outputStream = FileUtils.openOutputStream(destFile);) { // 按文件名排序 Arrays.sort(files); for (String fileName : files) { File file = FileUtils.getFile(inputPath, fileName); byte[] data = FileUtils.readFileToByteArray(file); IOUtils.write(data, outputStream); } } catch (Exception e) { e.printStackTrace(); } AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "文件合并成功"); } /////////////////////////////////////////////////// 功能点五: 文件合并 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点六: 文件转PDF //////////////////////////////////////////////////////////// @FXML protected void handleOfficeFileSelect(ActionEvent actionEvent) { fileChooserSelect(officeFile, "请选择需要转换的文件"); } @FXML protected void handleConvert2PDF(ActionEvent actionEvent) { String officeFileStr = officeFile.getText().trim(); if (StringUtils.isEmpty(officeFileStr)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请选择需要转换的文件"); return; } File file = new File(officeFileStr); String fileName = file.getName(); boolean officeFlag = fileName.toLowerCase().contains(".xls") || fileName.toLowerCase().contains(".xlsx") || fileName.toLowerCase().contains(".doc") || fileName.toLowerCase().contains(".docx") || fileName.toLowerCase().contains(".ppt") || fileName.toLowerCase().contains(".pptx"); if (!officeFlag) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "文件格式有误"); return; } String pdfFilePath = file.getParent() + File.separator + "covert.pdf"; try { if (fileName.toLowerCase().contains(".xls") || fileName.toLowerCase().contains(".xlsx")) { com.aspose.cells.Workbook cells = new com.aspose.cells.Workbook(officeFileStr); cells.save(pdfFilePath, com.aspose.cells.SaveFormat.PDF); } else if (fileName.toLowerCase().contains(".doc") || fileName.toLowerCase().contains(".docx")) { com.aspose.words.Document words = new com.aspose.words.Document(officeFileStr); words.save(pdfFilePath, com.aspose.words.SaveFormat.PDF); } else if (fileName.toLowerCase().contains(".ppt") || fileName.toLowerCase().contains(".pptx")) { com.aspose.slides.Presentation pptx = new com.aspose.slides.Presentation(officeFileStr); pptx.save(pdfFilePath, com.aspose.slides.SaveFormat.Pdf); } } catch (Exception e) { e.printStackTrace(); AppUtil.showAlertDialog(Alert.AlertType.ERROR, "文件转化失败!"); return; } AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "文件转换成功"); } /////////////////////////////////////////////////// 功能点六: 文件转PDF //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点七: Redis连接测试 //////////////////////////////////////////////////////////// @FXML protected void handleRedisConnect(ActionEvent actionEvent) { String redisIpStr = redisIp.getText().trim(); String redisPortStr = redisPort.getText().trim(); String redisPasswordStr = redisPassword.getText().trim(); if (StringUtils.isEmpty(redisIpStr) || StringUtils.isEmpty(redisPortStr)) { AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请填写Redis配置信息!"); return; } Jedis jedis = new Jedis(redisIpStr, Integer.parseInt(redisPortStr)); if (StringUtils.isNotEmpty(redisPasswordStr)) { jedis.auth(redisPasswordStr); } if (jedis != null) { try { jedis.select(0); AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "Redis连接成功!"); } catch (Exception e) { AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "Redis连接失败!"); } } else { AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "Redis连接失败!"); } } /////////////////////////////////////////////////// 功能点七: Redis连接测试 //////////////////////////////////////////////////////////// /////////////////////////////////////////////////// 功能点八: 数据库连接测试 //////////////////////////////////////////////////////////// @FXML protected void handleDataBaseConnect(ActionEvent actionEvent) { String databaseTypeStr = databaseType.getValue(); String databaseIpStr = databaseIp.getText().trim(); String databasePortStr = databasePort.getText().trim(); String databaseNameStr = databaseName.getText().trim(); String databaseUsernameStr = databaseUsername.getText().trim(); String databasePasswordStr = databasePassword.getText().trim(); if(StringUtils.isEmpty(databaseTypeStr) || StringUtils.isEmpty(databaseIpStr) || StringUtils.isEmpty(databasePortStr) || StringUtils.isEmpty(databaseUsernameStr) || StringUtils.isEmpty(databasePasswordStr)){ AppUtil.showAlertDialog(Alert.AlertType.ERROR, "请将数据库配置信息填写完整!"); return; } Connection connection = JdbcUtil.getConnection(databaseTypeStr, databaseIpStr, databasePortStr, databaseNameStr, databaseUsernameStr, databasePasswordStr); if(null != connection){ AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "数据库连接成功!"); }else{ AppUtil.showAlertDialog(Alert.AlertType.INFORMATION, "数据库连接失败!"); } } /////////////////////////////////////////////////// 功能点八: 数据库连接测试 //////////////////////////////////////////////////////////// /** * 选择文件 * * @param textField * @param title */ public static void fileChooserSelect(TextField textField, String title) { FileChooser shpFileChooser = new FileChooser(); shpFileChooser.setTitle(title); Stage stage = (Stage) textField.getScene().getWindow(); File file = shpFileChooser.showOpenDialog(stage); if (null != file) { textField.setText(file.getPath()); } else { textField.setText(""); } } /** * 选择文件 * * @param textField * @param title * @param desc * @param ext */ public static void fileChooserSelect(TextField textField, String title, String desc, String ext) { FileChooser shpFileChooser = new FileChooser(); shpFileChooser.setTitle(title); shpFileChooser.getExtensionFilters().add( new FileChooser.ExtensionFilter(desc, ext) ); Stage stage = (Stage) textField.getScene().getWindow(); File file = shpFileChooser.showOpenDialog(stage); if (null != file) { textField.setText(file.getPath()); } else { textField.setText(""); } } /** * 选择文件夹 * * @param textField * @param title */ public static void dirChooserSelect(TextField textField, String title) { DirectoryChooser shpExportDirChooser = new DirectoryChooser(); shpExportDirChooser.setTitle("选择Shp导出文件夹"); Stage stage = (Stage) textField.getScene().getWindow(); File file = shpExportDirChooser.showDialog(stage); if (null != file) { textField.setText(file.getPath().trim()); } else { textField.setText(""); } } }