com.finedo.base.utils.upload.FileUploadUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.finedo.base.utils.upload.FileUploadUtils.java

Source

package com.finedo.base.utils.upload;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.finedo.base.domain.FileInfo;

//?
public class FileUploadUtils {
    private static final Logger logger = Logger.getLogger(FileUploadUtils.class);

    private static String trimExtension(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int i = filename.lastIndexOf('.');
            if ((i > -1) && (i < (filename.length()))) {
                return filename.substring(0, i);
            }
        }
        return filename;
    }

    public static final List<FileInfo> saveFiles(String uploadDir, List<FileItem> list) {

        List filelist = new ArrayList();

        if (list == null) {
            return filelist;
        }

        File dir = new File(uploadDir);
        if (!(dir.isDirectory())) {
            dir.mkdirs();
        }

        String uploadPath = uploadDir;

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        Iterator it = list.iterator();
        String name = "";
        String extName = "";

        while (it.hasNext()) {
            FileItem item = (FileItem) it.next();
            if (!(item.isFormField())) {
                name = item.getName();
                logger.info("saveFiles name=============" + name);

                if (name == null)
                    continue;
                if (name.trim().equals("")) {
                    continue;
                }

                if (name.lastIndexOf(".") >= 0) {
                    extName = name.substring(name.lastIndexOf("."));
                }

                File file = null;
                do {
                    name = UUID.randomUUID().toString();
                    file = new File(uploadPath + name + extName);
                } while (file.exists());

                File saveFile = new File(uploadPath + name + extName);
                try {
                    item.write(saveFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String fileName = item.getName().replace("\\", "/");

                String[] ss = fileName.split("/");
                fileName = trimExtension(ss[(ss.length - 1)]);

                FileInfo fileinfo = new FileInfo();
                fileinfo.setName(fileName);
                fileinfo.setUname(name);
                fileinfo.setFilePath(uploadDir);
                fileinfo.setFileExt(extName);
                fileinfo.setSize(String.valueOf(item.getSize()));
                fileinfo.setContentType(item.getContentType());
                fileinfo.setFieldname(item.getFieldName());
                filelist.add(fileinfo);
            }
        }
        return filelist;
    }

    public static final List<FileInfo> saveLicense(String uploadDir, List<FileItem> list) {

        List<FileInfo> filelist = new ArrayList<FileInfo>();

        if (list == null) {
            return filelist;
        }

        //?
        //??
        logger.info("saveFiles uploadDir=============" + uploadDir);
        File dir = new File(uploadDir);
        if (!dir.isDirectory()) {
            dir.mkdirs();
        } else {

        }

        String uploadPath = uploadDir;

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        Iterator<FileItem> it = list.iterator();
        String name = "";
        String extName = "";

        while (it.hasNext()) {
            FileItem item = it.next();
            if (!item.isFormField()) {
                name = item.getName();
                if (name == null || name.trim().equals("")) {
                    continue;
                }
                long l = item.getSize();
                logger.info("file size=" + l);
                if (10 * 1024 * 1024 < l) {
                    logger.info("File size is greater than 10M!");
                    continue;
                }
                name = name.replace("\\", "/");
                if (name.lastIndexOf("/") >= 0) {
                    name = name.substring(name.lastIndexOf("/"));
                }
                logger.info("saveFiles name=============" + name);
                File saveFile = new File(uploadPath + name);
                try {
                    item.write(saveFile);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

                String fileName = item.getName().replace("\\", "/");

                String[] ss = fileName.split("/");
                fileName = trimExtension(ss[ss.length - 1]);
                FileInfo fileinfo = new FileInfo();
                fileinfo.setName(fileName);
                fileinfo.setUname(name);
                fileinfo.setFilePath(uploadDir);
                fileinfo.setFileExt(extName);
                fileinfo.setSize(String.valueOf(item.getSize()));
                fileinfo.setContentType(item.getContentType());
                fileinfo.setFieldname(item.getFieldName());
                filelist.add(fileinfo);
            }
        }
        return filelist;
    }
}