package com.example.client.utils; import com.example.server.progressTrack.model.DjJdgzNetworkLevel3; import com.example.server.progressTrack.model.DjJdgzTrackRecord; import com.example.server.sysOss.model.SysOss; import com.example.server.sysOss.service.OssEncryptService; import com.example.server.sysOss.service.SysOssService; import org.apache.commons.lang3.StringUtils; 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.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class UploadFile { private File[] selectedFiles; @Autowired private SysOssService sysOssService; @Autowired private OssEncryptService ossEncryptService; private List ossList; private SysOss templet; @Value("${zt.oss.local-path}") private String localPath; private static final String key = "zhpt-key#%W"; public JPanel uploadFile(JFrame frame, Integer width, Integer heigth, SysOss sysOssTemplet) { JPanel jPanel = new JPanel(); jPanel.setPreferredSize(new Dimension(width, heigth)); templet = sysOssTemplet; if (sysOssTemplet.getBusiId() != null) { ossList = sysOssService.getList(sysOssTemplet.getBusiId(), sysOssTemplet.getBusiType()); } else { ossList = new ArrayList<>(); } JTable table = new JTable(); table.setRowHeight(10); DefaultTableModel model = new DefaultTableModel(); String[] columnIdentifiers = {"文件地址", "操作"};//表头 JScrollPane scrolltable = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrolltable.setPreferredSize(new Dimension(width, heigth - 100)); scrolltable.setViewportView(table); scrolltable.getViewport().setBackground(Color.WHITE); JButton browseButton = new JButton("选择文件"); jPanel.add(browseButton); jPanel.add(scrolltable); if (ossList != null && ossList.size() > 0) { String[][] data = new String[ossList.size()][2]; for (int i = 0; i < ossList.size(); i++) { data[i][0] = ossList.get(i).getName(); } model.setDataVector(data, columnIdentifiers); table.setModel(model); table.getColumnModel().getColumn(0).setPreferredWidth(width - 120); table.getColumnModel().getColumn(1).setPreferredWidth(120); table.getColumnModel().getColumn(0).setCellRenderer(new TableViewRenderer()); table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRendererButton()); table.getColumnModel().getColumn(1).setCellEditor(new TableCellEditorButton(ossList, sysOssService)); } final boolean[] historyCalled = {false}; table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (historyCalled[0]) return; // 获取点击的行和列 int row = table.rowAtPoint(e.getPoint()); int column = table.columnAtPoint(e.getPoint()); // 检查是否点击了有效单元格 if (row >= 0 && column >= 0) { if (column == 0) { // 列索引从0开始 String path = ossList.get(row).getPath(); String name = ossList.get(row).getName(); File file = new File(localPath + path); InputStream is = null; try { is = ossEncryptService.decryptStream(file, ossEncryptService.getKey(key)); // 创建文件选择器并设置初始文件名 JFileChooser fileChooser = new JFileChooser(); fileChooser.setSelectedFile(new File(name)); // 设置默认文件名 int result = fileChooser.showSaveDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); // 将 InputStream 的内容写入到 selectedFile try (InputStream input = is; OutputStream output = new FileOutputStream(selectedFile)) { byte[] buffer = new byte[8192]; // 缓冲区大小 int bytesRead; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } // 文件保存成功 JOptionPane.showMessageDialog(frame, "文件下载成功"); } catch (IOException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(frame, "文件保存失败: " + ex.getMessage()); } } else { JOptionPane.showMessageDialog(frame, "文件下载取消"); } } catch (Exception exception) { exception.printStackTrace(); } finally { // 确保 InputStream 被关闭,避免资源泄露 if (is != null) { try { is.close(); } catch (IOException ec) { ec.printStackTrace(); } } } } } } }); 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.getSelectedFiles(); Integer oldNum = model.getRowCount(); String[][] data = new String[oldNum + selectedFiles.length][2]; for (int i = 0; i < oldNum; i++) { data[i][0] = model.getValueAt(i, 0).toString(); } for (int i = 0; i < selectedFiles.length; i++) { data[i + oldNum][0] = selectedFiles[i].getPath(); String suffix = selectedFiles[i].getName().substring(selectedFiles[i].getName().lastIndexOf(".")); SysOss oss = null; try { oss = templet.clone(); } catch (CloneNotSupportedException cloneNotSupportedException) { cloneNotSupportedException.printStackTrace(); } oss.setBusiType(suffix); oss.setPath(selectedFiles[i].getPath()); oss.setName(selectedFiles[i].getName()); ossList.add(oss); } model.setDataVector(data, columnIdentifiers); table.setModel(model); table.getColumnModel().getColumn(0).setPreferredWidth(width - 120); table.getColumnModel().getColumn(1).setPreferredWidth(120); table.getColumnModel().getColumn(0).setCellRenderer(new TableViewRenderer()); table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRendererButton()); table.getColumnModel().getColumn(1).setCellEditor(new TableCellEditorButton(ossList, sysOssService)); } 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 { 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++) { data[i + oldNum][0] = filepathArr[i]; SysOss oss = null; try { oss = templet.clone(); } catch (CloneNotSupportedException cloneNotSupportedException) { cloneNotSupportedException.printStackTrace(); } int lastSlashIndex = filepathArr[i].lastIndexOf('/'); String fullFileName = ""; if (lastSlashIndex != -1) { fullFileName = filepathArr[i].substring(lastSlashIndex + 1); } oss.setPath(filepathArr[i]); oss.setName(fullFileName); ossList.add(oss); } model.setDataVector(data, columnIdentifiers); table.setModel(model); table.getColumnModel().getColumn(0).setPreferredWidth(width - 120); table.getColumnModel().getColumn(1).setPreferredWidth(120); table.getColumnModel().getColumn(0).setCellRenderer(new TableViewRenderer()); table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRendererButton()); table.getColumnModel().getColumn(1).setCellEditor(new TableCellEditorButton(ossList, sysOssService)); 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 void save(Long id) { if (ossList != null && ossList.size() > 0) { for (SysOss oss : ossList) { if (oss.getBusiId()==null){ oss.setBusiId(id); } if (oss.getId() == null) { sysOssService.upload(oss); } } } } }