uploader.java

来自「web版的SVN客户端」· Java 代码 · 共 101 行

JAVA
101
字号
/*
 * Copyright (c) 2004, 2005 Polarion Software, All rights reserved.
 * Email: community@polarion.org
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License, Version 2.0 (the "License"). You may not use
 * this file except in compliance with the License. Copy of the License is
 * located in the file LICENSE.txt in the project distribution. You may also
 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * POLARION SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package org.polarion.svnwebclient.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;
import org.polarion.svnwebclient.web.support.FormParameters;

/**
 * 
 * @author <A HREF="mailto:svnbrowser@polarion.org">Polarion Software </A>
 */
public class Uploader {

    public static Map doPost(HttpServletRequest request, HttpServletResponse responce, String destinationDirectory, String tempDirectory) {
        Map parameters = new HashMap();

        File tempDir = new File(tempDirectory);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }

        File destDir = new File(destinationDirectory);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        DiskFileUpload fu = new DiskFileUpload();
        // maximum size before a FileUploadException will be thrown
        fu.setSizeMax(-1);
        // maximum size that will be stored in memory
        fu.setSizeThreshold(4096);
        // the location for saving data that is larger than getSizeThreshold()
        fu.setRepositoryPath(tempDirectory);
        List fileItems = null;
        try {
            fileItems = fu.parseRequest(request);
        } catch (FileUploadException e) {
            Logger.getInstance(Uploader.class).error(e, e);
        }

        for (Iterator i = fileItems.iterator(); i.hasNext();) {
            FileItem fi = (FileItem) i.next();
            if (fi.isFormField()) {
                try {
                    parameters.put(fi.getFieldName(), new String(fi.getString().getBytes(), "UTF8"));
                } catch (UnsupportedEncodingException e) {
                    Logger.getInstance(Uploader.class).error(e, e);
                }
            } else {
                // filename on the client
                String fileName = null;
                try {
                    fileName = new String(fi.getName().getBytes(), "UTF8");
                } catch (UnsupportedEncodingException e) {
                    Logger.getInstance(Uploader.class).error(e, e);
                }
                // write the file
                try {
                    File uploadFile = new File(destinationDirectory + "/" + FileUtil.getLastPathElement(fileName));
                    fi.write(uploadFile);
                    // fi.delete();
                } catch (Exception e) {
                    Logger.getInstance(Uploader.class).error(e, e);
                }
                parameters.put(FormParameters.FILE_NAME, FileUtil.getLastPathElement(fileName));
            }
        }

        return parameters;
    }
}

⌨️ 快捷键说明

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