2015年12月27日 Java Coding 浏览(170)

支持Struts、Spring通用文件上传代码

本站很多功能模块涉及上传操作,这里分享下本站上传的java源码,支持struts、spring、servlet等,即是通用的上传功能。可将文件上传至指定目录。

上传代码如下:

package com.everycoding.upload;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.everycoding.commons.util.ImageUtil;
import com.everycoding.commons.util.StorePathUtil;
import com.everycoding.upload.define.AppInfo;
import com.everycoding.upload.define.BaseState;
import com.everycoding.upload.define.FileType;
import com.everycoding.upload.define.State;

public class BinaryUploader {
    private static final MultipartFile getMultipartFile(DefaultMultipartHttpServletRequest request, Map<String, Object> conf) throws IOException {
        List<MultipartFile> mFiles = request.getFiles((String) conf.get("fieldName"));
        if (mFiles != null) {
            for (MultipartFile multipartFile : mFiles) {
                if (multipartFile != null) {
                    return multipartFile;
                }
            }
        }
        return null;
    }

    private static final FileItemStream getFileItemStream(HttpServletRequest request) throws FileUploadException, IOException {
        FileItemStream fileStream = null;
        boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
        if (!ServletFileUpload.isMultipartContent(request)) {
            return null;
        }
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        if (isAjaxUpload) {
            upload.setHeaderEncoding("utf-8");
        }
        FileItemIterator iterator = upload.getItemIterator(request);

        while (iterator.hasNext()) {
            fileStream = iterator.next();

            if (!fileStream.isFormField())
                break;
            fileStream = null;
        }

        return fileStream;

    }

    public static final State save(HttpServletRequest request, Map<String, Object> conf) {
        try {
            InputStream is;
            String originFileName;
            if (request instanceof DefaultMultipartHttpServletRequest) {
                MultipartFile mFile = BinaryUploader.getMultipartFile((DefaultMultipartHttpServletRequest) request, conf);
                is = mFile.getInputStream();
                originFileName = mFile.getOriginalFilename();
            } else {
                FileItemStream fileStream = BinaryUploader.getFileItemStream(request);
                is = fileStream.openStream();
                originFileName = fileStream.getName();
            }
            if (is == null) {
                return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
            }
            String savePath = (String) conf.get("savePath");
            String suffix = FileType.getSuffixByFilename(originFileName);
            originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
            savePath = savePath + suffix;

            long maxSize = ((Long) conf.get("maxSize")).longValue();

            if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
                return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
            }

            savePath = PathFormat.parse(savePath, originFileName);

            String physicalPath = (String) conf.get("rootPath") + savePath;

            State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
            is.close();
            ImageUtil.compress(StorePathUtil.genSmallImagePath(physicalPath), physicalPath, 160, 130, 0.7f);
            if (storageState.isSuccess()) {
                storageState.putInfo("url", "http://images.everycoding.com/image"+PathFormat.format(savePath));
                storageState.putInfo("type", suffix);
                storageState.putInfo("original", originFileName + suffix);
            }

            return storageState;
        } catch (FileUploadException e) {
            return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
        } catch (IOException localIOException) {
        }
        return new BaseState(false, AppInfo.IO_ERROR);
    }

    private static boolean validType(String type, String[] allowTypes) {
        List<String> list = Arrays.asList(allowTypes);
        return list.contains(type);
    }
}


用户头像