⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadapplet.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        files = addOverwrites(
                            files,
                            ((FileSelectionPanel)m_overwriteDialog.getControlPanel().getComponent(1)).getSelectedFiles());
                    } else {

                        fileNames = new ArrayList();
                        for (int i = 0; i < files.length; i++) {
                            getFilesInTree(files[i], fileNames);
                        }
                        files = (File[])fileNames.toArray(new File[fileNames.size()]);
                    }

                    m_resources = countResources(files);

                    // create the zipfile  
                    m_outputMode = 2;
                    if (files.length > 0) {
                        File targetFile = createZipFile(files);
                        // check the size of the zip files
                        if ((targetFile == null) || ((m_maxsize > 0) && (targetFile.length() > m_maxsize))) {
                            // show some details in the applet itself
                            m_outputMode = 4;
                            if (targetFile == null) {
                                m_message = m_messageOutputErrorZip;
                            } else {
                                m_message = m_messageOutputErrorSize + " " + targetFile.length() + " > " + m_maxsize;
                            }
                            m_action = m_actionOutputError;
                            repaint();
                            // show an error-alertbog
                            JOptionPane.showMessageDialog(this, m_message, m_action, JOptionPane.ERROR_MESSAGE);
                        } else {
                            m_outputMode = 3;
                            m_message = m_messageOutputUpload + " (" + targetFile.length() / 1024 + " kb)";
                            repaint();
                            // upload the zipfile
                            FileUploadThread uploadThreat = new FileUploadThread();

                            uploadThreat.init(this);
                            uploadThreat.start();

                            uploadZipFile(targetFile);
                            ok = false;
                        }
                    } else {
                        // zero files were selected for upload (might be that all potential overwrites were deselected)
                        ok = false;
                        getAppletContext().showDocument(new URL(m_redirectUrl), m_redirectTargetFrame);
                    }

                } else {
                    //the cancel button was used, so go back to the workplace
                    ok = false;
                    getAppletContext().showDocument(new URL(m_redirectUrl), m_redirectTargetFrame);
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** 
     * Displays the dialog that shows the list of files that will be overwritten on the server.
     * <p>
     * The user may uncheck the checkboxes in front of the relative paths to avoid overwriting. 
     * <p>
     * 
     * @param duplications 
     *      a list of Strings that are relative paths to the files that will be overwritten on the server
     *      
     * @return one of 
     */
    private int showDuplicationsDialog(List duplications) {

        int rtv = ModalDialog.ERROR_OPTION;
        try {

            JTextArea dialogIntroPanel = new JTextArea();
            dialogIntroPanel.setLineWrap(true);
            dialogIntroPanel.setWrapStyleWord(true);
            dialogIntroPanel.setText(m_overwriteDialogIntro);
            dialogIntroPanel.setEditable(false);
            dialogIntroPanel.setBackground(m_fileSelector.getBackground());
            dialogIntroPanel.setFont(m_font);

            FileSelectionPanel selectionPanel = new FileSelectionPanel(
                duplications,
                m_fileSelector.getCurrentDirectory().getAbsolutePath());

            JPanel stacker = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.gridheight = 1;
            gbc.gridwidth = 1;
            gbc.weightx = 1f;
            gbc.weighty = 0f;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(2, 2, 2, 2);

            stacker.add(dialogIntroPanel, gbc);

            gbc.weighty = 1f;
            gbc.gridy = 1;
            gbc.insets = new Insets(0, 2, 0, 2);
            stacker.add(selectionPanel, gbc);

            m_overwriteDialog = new ModalDialog(
                m_fileSelector,
                m_overwriteDialogTitle,
                m_overwriteDialogOk,
                m_overwriteDialogCancel,
                stacker);
            m_overwriteDialog.setSize(new Dimension(560, 280));

            //dialog.setResizable(false);
            m_overwriteDialog.showDialog();
            rtv = m_overwriteDialog.getReturnValue();

        } catch (Throwable f) {
            f.printStackTrace(System.err);
        }
        return rtv;
    }

    /**
     * @see java.applet.Applet#start()
     */
    public void start() {

        if (m_certificateAccepted) {
            // certificate was accepted, start upload thread
            m_runner = new Thread(this);
            m_runner.start();
        } else {
            // certificate was not accepted, show error message
            displayError();
        }
    }

    /**
     * @see java.applet.Applet#stop()
     */
    public void stop() {

        m_runner = null;
    }

    /**
     * Return all files that are found in the first argument except a matching path suffix is found in the list 
     * of Strings given by the 2nd argument. <p>
     * 
     * @param fileNames the list of paths to diminish by matching path suffixes of the 2nd argument. 
     * 
     * @param duplications a list of Strings that contains "relative" paths (without absolute location)
     * 
     * @return all files that are found in the first argument except a matching path suffix is found in the list 
     *      of Strings given by the 2nd argument
     */
    private File[] subtractDuplicates(List fileNames, List duplications) {

        // subtract: 
        String path;
        Iterator itDuplications = duplications.iterator();
        while (itDuplications.hasNext()) {
            path = (String)itDuplications.next();
            fileNames.remove(path);
        }

        // no recreate the subtractor list to files: 
        List result = new ArrayList();
        File rootPath = m_fileSelector.getCurrentDirectory();
        Iterator it = fileNames.iterator();
        while (it.hasNext()) {
            path = (String)it.next();
            result.add(new File(rootPath, path));
        }
        return (File[])result.toArray(new File[result.size()]);
    }

    /**
     * @see java.awt.Component#update(java.awt.Graphics)
     */
    public void update(Graphics g) {

        paint(g);
    }

    /**
     * Uploads the zipfile to the OpenCms.<p>
     * 
     * @param uploadFile the zipfile to upload
     */
    private void uploadZipFile(File uploadFile) {

        m_action = m_actionOutputUpload;
        repaint();

        PostMethod post = new PostMethod(m_targetUrl);

        try {
            Part[] parts = new Part[5];
            parts[0] = new FilePart(uploadFile.getName(), uploadFile);
            parts[1] = new StringPart("action", "submitform");
            parts[2] = new StringPart("unzipfile", "true");
            parts[3] = new StringPart("uploadfolder", m_uploadFolder);
            parts[4] = new StringPart("clientfolder", m_fileSelector.getCurrentDirectory().getAbsolutePath());

            HttpMethodParams methodParams = post.getParams();
            methodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            MultipartRequestEntity request = new MultipartRequestEntity(parts, methodParams);
            post.setRequestEntity(request);

            // add jsessionid query string
            String sessionId = getParameter("sessionId");
            String query = ";" + C_JSESSIONID.toLowerCase() + "=" + sessionId;
            post.setQueryString(query);
            post.addRequestHeader(C_JSESSIONID, sessionId);

            HttpClient client = new HttpClient();
            HttpConnectionParams connectionParams = client.getHttpConnectionManager().getParams();
            connectionParams.setConnectionTimeout(5000);

            // add the session cookie
            client.getState();
            client.getHostConfiguration().getHost();

            HttpState initialState = new HttpState();
            URI uri = new URI(m_targetUrl, false);
            Cookie sessionCookie = new Cookie(uri.getHost(), C_JSESSIONID, sessionId, "/", null, false);
            initialState.addCookie(sessionCookie);
            client.setState(initialState);

            // no execute the file upload
            int status = client.executeMethod(post);

            if (status == HttpStatus.SC_OK) {
                //return to the specified url and frame target
                getAppletContext().showDocument(new URL(m_redirectUrl), m_redirectTargetFrame);
            } else {
                // create the error text
                String error = m_errorLine1 + "\n" + post.getStatusLine();
                //JOptionPane.showMessageDialog(this, error, "Error!", JOptionPane.ERROR_MESSAGE);
                getAppletContext().showDocument(
                    new URL(m_errorUrl + "?action=showerror&uploaderror=" + error),
                    "explorer_files");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
            // finally delete the zipFile on the harddisc
            uploadFile.delete();
        }
    }

    /**
     * Writes the bytes of the file to the zip output stream.<p>
     *
     * @param file the name of the file to read
     * 
     * @param out the zip outputstream
     * 
     * @throws Exception if something goes wrong
     */
    private void writeFileBytes(File file, OutputStream out) throws Exception {

        byte[] buffer = new byte[2048];
        FileInputStream fileStream = null;
        int charsRead;
        int size;
        try {
            fileStream = new FileInputStream(file);
            charsRead = 0;
            size = new Long(file.length()).intValue();
            int readCount = 0;
            while ((charsRead < size) && (readCount != -1)) {
                readCount = fileStream.read(buffer);
                charsRead += readCount;
                out.write(buffer, 0, readCount);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (fileStream != null) {
                    fileStream.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -