package com.example.client.service;
|
|
import com.example.client.dto.ColumnDto;
|
import com.example.client.utils.*;
|
import com.example.server.DataSync.service.DataSyncService;
|
import com.example.server.progressTrack.model.DjJdgzNetworkLevel2List;
|
import com.example.server.progressTrack.service.DjJdgzNetworkLevel2ListService;
|
import com.mxgraph.view.mxGraph;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import javax.swing.*;
|
import javax.swing.table.DefaultTableModel;
|
import java.awt.*;
|
import java.awt.datatransfer.DataFlavor;
|
import java.awt.datatransfer.Transferable;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.io.*;
|
import java.nio.charset.Charset;
|
import java.util.Date;
|
import java.util.Enumeration;
|
import java.util.List;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
|
|
@Service
|
public class ImportDataService {
|
@Value("${data.imgDir}")
|
private String imgPath;
|
private File selectedFiles;
|
private DefaultTableModel model;
|
@Value("${zt.oss.Unzip-path}")
|
private String UnzipPath;
|
@Autowired
|
private DataSyncService dataSyncService;
|
|
public JPanel FileUpload(JFrame frame, Integer width, Integer heigth) {
|
JPanel jPanel = new JPanel();
|
jPanel.setPreferredSize(new Dimension(width, heigth));
|
|
JTable table = new JTable();
|
table.setRowHeight(40);
|
model = new DefaultTableModel();
|
String[] columnIdentifiers = {"文件地址", "操作"};//表头
|
|
|
JScrollPane scrolltable = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
|
|
scrolltable.setViewportView(table);
|
scrolltable.getViewport().setBackground(Color.WHITE);
|
|
JButton browseButton = new JButton("选择文件");
|
jPanel.add(browseButton);
|
jPanel.add(scrolltable);
|
|
browseButton.addActionListener(new ActionListener() {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
Boolean flag = true;
|
// 保存当前的外观设置
|
LookAndFeel savedLookAndFeel = UIManager.getLookAndFeel();
|
|
// 设置文件选择器外观
|
try {
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
fileChooser.setDialogTitle("选择文件");
|
fileChooser.setPreferredSize(new Dimension(800, 500));
|
|
|
fileChooser.setMultiSelectionEnabled(true);
|
|
//恢复外观,避免改变所有组件外观
|
try {
|
UIManager.setLookAndFeel(savedLookAndFeel);
|
} catch (UnsupportedLookAndFeelException ed) {
|
ed.printStackTrace();
|
}
|
|
int result = fileChooser.showOpenDialog(frame);
|
if (result == JFileChooser.FILES_ONLY) {
|
selectedFiles = fileChooser.getSelectedFile();
|
|
Integer oldNum = model.getRowCount();
|
String[][] data = new String[oldNum + 1][2];
|
for (int i = 0; i < oldNum; i++) {
|
data[i][0] = model.getValueAt(i, 0).toString();
|
}
|
|
String suffix = selectedFiles.getName().substring(selectedFiles.getName().lastIndexOf("."));
|
if (suffix.equals(".zip")) {
|
data[oldNum][0] = selectedFiles.getPath();
|
} else {
|
flag = false;
|
|
}
|
|
if (!flag) {
|
JOptionPane.showMessageDialog(null, "只能导入压缩包,且不超过2G", "提示", JOptionPane.WARNING_MESSAGE);
|
return;
|
}
|
|
model.setDataVector(data, columnIdentifiers);
|
table.setModel(model);
|
table.getColumnModel().getColumn(0).setPreferredWidth(600);
|
table.getColumnModel().getColumn(1).setPreferredWidth(100);
|
|
|
table.getColumnModel().getColumn(0).setCellRenderer(new TableViewRenderer());
|
table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRendererButton());
|
table.getColumnModel().getColumn(1).setCellEditor(new TableCellEditorButton(null, null));
|
|
} else {
|
Object[] options = {"OK ", "CANCEL "};
|
JOptionPane.showOptionDialog(null, "选择的文件不正确 ", "提示", JOptionPane.DEFAULT_OPTION,
|
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
|
}
|
}
|
});
|
|
scrolltable.setTransferHandler(new TransferHandler() {
|
private static final long serialVersionUID = 1L;
|
|
@Override
|
public boolean importData(JComponent comp, Transferable t) {
|
try {
|
Boolean flag = true;
|
Object o = t.getTransferData(DataFlavor.javaFileListFlavor);
|
String filepath = o.toString();
|
if (filepath.startsWith("[")) {
|
filepath = filepath.substring(1);
|
}
|
if (filepath.endsWith("]")) {
|
filepath = filepath.substring(0, filepath.length() - 1);
|
}
|
String[] filepathArr = filepath.split(", ");
|
Integer oldNum = model.getRowCount();
|
String[][] data = new String[oldNum + filepathArr.length][2];
|
for (int i = 0; i < oldNum; i++) {
|
data[i][0] = model.getValueAt(i, 0).toString();
|
}
|
for (int i = 0; i < filepathArr.length; i++) {
|
if (filepathArr[i].contains(".zip")) {
|
data[i + oldNum][0] = filepathArr[i];
|
} else {
|
flag = false;
|
break;
|
}
|
}
|
if (!flag) {
|
JOptionPane.showMessageDialog(null, "只能导入压缩包文件,且不超过2G\"", "提示", JOptionPane.WARNING_MESSAGE);
|
return flag;
|
}
|
model.setDataVector(data, columnIdentifiers);
|
table.setModel(model);
|
table.getColumnModel().getColumn(0).setPreferredWidth(650);
|
table.getColumnModel().getColumn(1).setPreferredWidth(80);
|
|
table.getColumnModel().getColumn(0).setCellRenderer(new TableViewRenderer());
|
table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRendererButton());
|
table.getColumnModel().getColumn(1).setCellEditor(new TableCellEditorButton(null, null));
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
@Override
|
public boolean canImport(JComponent comp, DataFlavor[] flavors) {
|
for (int i = 0; i < flavors.length; i++) {
|
if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
|
return true;
|
}
|
}
|
return false;
|
}
|
});
|
return jPanel;
|
}
|
public String UnzipFile(){
|
String filePath = model.getValueAt(0, 0).toString();
|
String outputDirectory = UnzipPath;
|
try {
|
|
File zipFile = new File(filePath);
|
if (!zipFile.exists()) {
|
throw new FileNotFoundException("ZIP文件不存在:" + filePath);
|
}
|
if (!zipFile.canRead()) {
|
System.err.println("无法读取ZIP文件,请检查文件权限:" + filePath);
|
return "false";
|
}
|
|
// 创建目标目录
|
File outputDir = new File(outputDirectory);
|
if (!outputDir.exists()) {
|
outputDir.mkdirs();
|
}
|
|
// 使用ZipFile读取ZIP文件
|
try (ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"))) {
|
Enumeration<? extends ZipEntry> entries = zip.entries();
|
|
while (entries.hasMoreElements()) {
|
ZipEntry entry = entries.nextElement();
|
String entryName = entry.getName();
|
|
// 创建目标文件或目录
|
File entryFile = new File(outputDirectory, entryName);
|
File entryDir = entryFile.getParentFile();
|
|
// 如果是目录,创建目录
|
if (!entryDir.exists()) {
|
entryDir.mkdirs();
|
}
|
|
// 如果是文件,提取文件内容
|
if (!entry.isDirectory()) {
|
try (InputStream is = zip.getInputStream(entry);
|
FileOutputStream fos = new FileOutputStream(entryFile)) {
|
byte[] buffer = new byte[1024];
|
int length;
|
while ((length = is.read(buffer)) >= 0) {
|
fos.write(buffer, 0, length);
|
}
|
}
|
}
|
}
|
}
|
System.out.println("文件解压完成,目标目录:" + outputDirectory);
|
dataSyncService.importData();
|
} catch (IOException e) {
|
e.printStackTrace();
|
return "false";
|
}
|
return "true";
|
}
|
}
|