jinlin
2025-03-21 77d58298d00c11ade8862ca8acb0fdef5a45322e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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();
        /*GridBagLayout layout = new GridBagLayout();
        jPanel.setLayout(layout);*/
        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.setPreferredSize(new Dimension(width-150,heigth));
        scrolltable.setViewportView(table);
        scrolltable.getViewport().setBackground(Color.WHITE);
 
        JButton browseButton = new JButton("选择文件");
 
        jPanel.add(scrolltable);
        jPanel.add(browseButton);
 
 
        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(JTextArea tips){
        String massage = "";
        String filePath = model.getValueAt(0, 0).toString();
        String outputDirectory = UnzipPath;
        try {
 
            File zipFile = new File(filePath);
            if (!zipFile.exists()) {
                massage = "ZIP文件不存在:" + filePath;
                tips.setText(tips.getText() + massage);
                throw new FileNotFoundException("ZIP文件不存在:" + filePath);
            }
            if (!zipFile.canRead()) {
                massage = "无法读取ZIP文件,请检查文件权限:" + filePath;
                tips.setText(tips.getText() + massage);
                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);
                            }
                        }
                    }
                }
            }
            massage = "文件解压完成,目标目录:" + outputDirectory;
            tips.setText(tips.getText() + massage);
            dataSyncService.importData(tips);
        } catch (IOException e) {
            e.printStackTrace();
            return "false";
        }
        return "true";
    }
}