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

📄 fileuploadapplet.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * 
     * @param folder the folder to count
     * @return number of resources
     */
    private int countSubresources(File folder) {

        int count = 0;
        if (folder.isFile()) {
            // check if is really a folder
            count = 1;
        } else {
            // recurest to count
            count = countResources(folder.listFiles());
        }
        return count;
    }

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

        paint(g);
    }

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

        // create the box
        m_offscreen = createImage(getSize().width, getSize().height);
        m_offgraphics = m_offscreen.getGraphics();
        m_offgraphics.setColor(getColor("bgColor"));
        m_offgraphics.fillRect(0, 0, getSize().width, getSize().height);
        m_offgraphics.setColor(getColor("outerBorderRightBottom"));
        m_offgraphics.drawLine(0, getSize().height - 1, getSize().width - 1, getSize().height - 1);
        m_offgraphics.drawLine(getSize().width - 1, 0, getSize().width - 1, getSize().height - 1);
        m_offgraphics.setColor(getColor("outerBorderLeftTop"));
        m_offgraphics.drawLine(0, 0, getSize().width - 1, 0);
        m_offgraphics.drawLine(0, 0, 0, getSize().height - 1);
        m_offgraphics.setColor(getColor("innerBorderRightBottom"));
        m_offgraphics.drawLine(1, getSize().height - 2, getSize().width - 2, getSize().height - 2);
        m_offgraphics.drawLine(getSize().width - 2, 1, getSize().width - 2, getSize().height - 2);
        m_offgraphics.setColor(getColor("innerBorderLeftTop"));
        m_offgraphics.drawLine(1, 1, getSize().width - 2, 1);
        m_offgraphics.drawLine(1, 1, 1, getSize().height - 2);
        m_offgraphics.setColor(getColor("bgHeadline"));
        m_offgraphics.fillRect(4, 4, getSize().width - 5, 18);

        m_offgraphics.setColor(getColor("innerBorderRightBottom"));
        m_offgraphics.drawLine(10, getSize().height - 11, getSize().width - 11, getSize().height - 11);
        m_offgraphics.drawLine(getSize().width - 11, 25, getSize().width - 11, getSize().height - 11);
        m_offgraphics.setColor(getColor("innerBorderLeftTop"));
        m_offgraphics.drawLine(10, 25, getSize().width - 11, 25);
        m_offgraphics.drawLine(10, 25, 10, getSize().height - 11);

        // draw title
        int cx = 10;
        int cy = 17;
        m_offgraphics.setFont(m_font);
        m_offgraphics.setColor(getColor("colorHeadline"));
        m_offgraphics.drawString(m_action, cx, cy);

        m_offgraphics.setColor(getColor("colorText"));
        //draw process message
        if (m_outputMode == 3 || m_outputMode == 4) {
            cx = Math.max((getSize().width - m_metrics.stringWidth(m_message)) / 2, 0);
        } else {
            cx = 25;
        }
        cy = 41;
        m_offgraphics.drawString(m_message, cx, cy);

        // draw process bar during zip creation
        if (m_outputMode == 2) {
            float bar = new Float(m_step).floatValue() / new Float(m_resources).floatValue();
            String barText = "(" + m_step + " / " + m_resources + ")";
            m_offgraphics.drawRect(25, 50, 450, 20);
            m_offgraphics.setColor(Color.white);
            m_offgraphics.fillRect(26, 51, 449, 19);
            m_offgraphics.setColor(getColor("progessBar"));
            m_offgraphics.fillRect(26, 51, new Float(bar * 449).intValue(), 19);
            int progressWith = m_metrics.stringWidth(barText);
            cx = Math.max((getSize().width - progressWith) / 2, 0);
            cy = 64;
            m_offgraphics.setColor(Color.black);
            m_offgraphics.drawString(barText, cx, cy);
        }

        // show nonsense during upload
        if (m_outputMode == 3) {
            m_offgraphics.drawImage(m_floater, m_floaterPos, 57, this);
            m_offgraphics.drawImage(m_source, 30, 47, this);
            m_offgraphics.drawImage(m_target, 440, 47, this);
            
        }

        // copy the offcreen graphics to the applet
        g.drawImage(m_offscreen, 0, 0, null);
    }

    /**
     * Move the floating upload image to right, wrap around on right side.<p>
     */
    public void moveFloater() {

        m_floaterPos += 10;
        if ((m_floaterPos) > 430) {
            m_floaterPos = 50;
        }
        repaint();
    }

    /**
     * Creates a ZipFile from all files to upload.<p>
     * 
     * @param files the files to be packed into the zipfile
     * @return reference to the zipfile
     */
    private File createZipFile(File[] files) {

        File targetFile = null;
        m_action = m_actionOutputCreate;
        try {
            // create a new zipStream
            String zipFileName = ".opencms_upload.zip";                
            String userHome = System.getProperty("user.home");
            // create file in user home directory where write permissions should exist
            if (userHome != null) {
                if (! userHome.endsWith(File.separator)) {
                    userHome = userHome + File.separator;
                }
                zipFileName = userHome + zipFileName;
            }
            ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            // loop through all files
            for (int i = 0; i < files.length; i++) {

                // if its a file, add it to the zipfile
                if (files[i].isFile()) {
                    addFileToZip(zipStream, files[i], files[i].getName());
                } else {
                    addFolderToZip(zipStream, files[i], "");
                }
                repaint();
                // free mem
                files[i] = null;
            }
            zipStream.close();
            // get the zipfile
            targetFile = new File(zipFileName);
        } catch (Exception e) {
            System.err.println("Error creating zipfile " + e);
        }
        return targetFile;

    }

    /**
     * Adds a single file to the zip output.<p>
     * 
     * @param zipStream the zip output stream
     * @param file the file to add to the stream
     * @param filename the name of the file to add
     * @throws Exception if something goes wrong
     */
    private void addFileToZip(ZipOutputStream zipStream, File file, String filename) throws Exception {

        // add to zipfile
        String name = filename;
        if (name.length() > 40) {
            name = "..." + name.substring(name.length() - 40, name.length());
        }
        m_message = m_messageOutputAdding + " " + name + "..";
        m_step++;
        repaint();
        ZipEntry entry = new ZipEntry(filename);
        zipStream.putNextEntry(entry);
        zipStream.write(getFileBytes(file));
        zipStream.closeEntry();
    }

    /**
     * Adds a folder and all subresources to the zip output.<p>
     * 
     * @param zipStream the zip output stream
     * @param file the file to add to the stream
     * @param prefix the foldername prefix
     * @throws Exception if something goes wrong
     */
    private void addFolderToZip(ZipOutputStream zipStream, File file, String prefix) throws Exception {

        String foldername = file.getName();

        prefix += "/" + foldername;
        // get all subresources
        File[] subresources = file.listFiles();
        // loop through the results                        
        for (int i = 0; i < subresources.length; i++) {
            // add it its a file
            if (subresources[i].isFile()) {

                // use the prefix for the filename, since it inside the folder     
                addFileToZip(zipStream, subresources[i], prefix + "/" + subresources[i].getName());
            } else {
                // recurse into the subfolder
                addFolderToZip(zipStream, subresources[i], prefix);
            }
        }
    }

    /** The JSESSIONID cookie header name. */
    public static final String C_JSESSIONID = "JSESSIONID";
    
    /**
     * Uploads the zipfile to the OpenCms.<p>
     * 
     * @param uploadFile the zipfile to upload
     */
    private void uploadZipFile(File uploadFile) {

        m_action = m_actionOutputUpload;
        repaint();
        
        MultipartPostMethod filePost = new MultipartPostMethod(m_targetUrl);

        try {
            filePost.addParameter(uploadFile.getName(), uploadFile);
            // add parameters for the upload zipfile JSP dialog
            filePost.addParameter("action", "submitform");
            filePost.addParameter("unzipfile", "true");
            filePost.addParameter("uploadfolder", m_uploadFolder);

            String sessionId = getParameter("sessionId");
            
            // add jsessionid query string
            String query = ";" + C_JSESSIONID.toLowerCase() + "=" + sessionId;                         
            filePost.setQueryString(query);
            filePost.addRequestHeader(C_JSESSIONID, sessionId);
            
            HttpClient client = new HttpClient();
            client.setConnectionTimeout(5000);
            
            // add the session cookie
            HttpState initialState = new HttpState();
            Cookie sessionCookie = new Cookie(filePost.getHostConfiguration().getHost(), C_JSESSIONID, sessionId, "/", null, false);
            initialState.addCookie(sessionCookie);
            initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
            client.setState(initialState);            
            
            // no execute the file upload
            int status = client.executeMethod(filePost);

            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" + filePost.getStatusLine();
                //JOptionPane.showMessageDialog(this, error, "Error!", JOptionPane.ERROR_MESSAGE);
                getAppletContext().showDocument(
                    new URL(m_errorUrl + "?action=showerror&uploaderror=" + error),
                    "explorer_files");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            filePost.releaseConnection();
            // finally delete the zipFile on the harddisc
            uploadFile.delete();
        }
    }

    /**
     * Returns a byte array containing the content of server FS file.<p>
     *
     * @param file the name of the file to read
     * @return bytes[] the content of the file
     * @throws Exception if something goes wrong
     */
    private byte[] getFileBytes(File file) throws Exception {

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

    /**
     * Extracts the colors from the parameter String.<p>
     * 
     * @param colors list of color names and values
     * @return HashMap with color names and values
     */
    private HashMap extractColors(String colors) {

        HashMap colorStorage = new HashMap();

        if (colors != null) {
            StringTokenizer tok = new StringTokenizer(colors, ",");
            // loop through the tokens
            // all tokens have the format "extension=type"    
            while (tok.hasMoreElements()) {
                String token = tok.nextToken();
                // now extract the file extension and the type
                String colorName = token.substring(0, token.indexOf("="));
                String colorValue = token.substring(token.indexOf("=") + 1);
                colorStorage.put(colorName, colorValue);
            }
        }
        return colorStorage;
    }

    /**
     * Gets a color for drawing the output.<p>
     * 
     * @param colorName the name of the color
     * @return color
     */
    private Color getColor(String colorName) {

        Color col = Color.black;
        try {
            col = Color.decode((String)m_colors.get(colorName));
        } catch (Exception e) {
            System.err.println("Error reading " + colorName + ":" + e);
        }
        return col;
    }
}

⌨️ 快捷键说明

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