All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unique.support.upload.BinaryUploader Maven / Gradle / Ivy

The newest version!
package org.unique.support.upload;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.unique.commons.utils.FileUtil;

/**
 * commons-fileupload上传二进制文件
 * @author:biezhi
 * @version:1.0
 */
public class BinaryUploader {

	public static final State save(HttpServletRequest request, Map conf) {

		FileItem fileStream = null;
		
		boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
		
		if (!ServletFileUpload.isMultipartContent(request)) {
			return new BaseState(false, StateInfo.NOT_MULTIPART_CONTENT);
		}

		ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
		
		if (isAjaxUpload) {
			upload.setHeaderEncoding("utf-8");
		}

		try {
			Map formParams = new HashMap();
			
			List itemList = upload.parseRequest(request);
			
			if(null != itemList && itemList.size() > 0){
				
				for(FileItem item : itemList){
					if(item.isFormField()){
						formParams.put(item.getFieldName(), item.getString("UTF-8"));
					} else {
						fileStream = item;
					}
				}
			}
			
			if (fileStream == null) {
				return new BaseState(false, StateInfo.NOTFOUND_UPLOAD_DATA);
			}

			// 文件保存根
			String rootDir = request.getServletContext().getRealPath("/");
			
			String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
			
			// 域名
			String domain = (null == conf.get("domain")) ? basepath : conf.get("domain");
			
			String saveDir = (null == conf.get("savedir")) ? "" : conf.get("savedir");
			
			// 原始文件名
			String originFileName = fileStream.getName();
			
			// 文件后缀
			String suffix = FileUtil.getSuffix(originFileName);
			
			String savename = originFileName.substring(0, originFileName.length() - suffix.length());
			
			// 自定义文件名称
			if(null != conf.get("filename")){
				savename = conf.get("filename");
			}
			
			// 文件保存路径
			String savePath = rootDir + saveDir + "/" + savename + suffix;
			
			// 文件保存路径
			String relativePath = saveDir + "/" + savename + suffix;
			
			long maxSize = Long.valueOf((null==conf.get("maxsize")? "1048576" :conf.get("maxsize")));

			// allowfiles
			if (null != conf.get("allowfiles") && !validType(suffix, conf.get("allowfiles"))) {
				return new BaseState(false, StateInfo.NOT_ALLOW_FILE_TYPE);
			}
			
			String physicalPath = savePath;

			InputStream is = fileStream.getInputStream();
			State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
			is.close();
			
			if (storageState.isSuccess()) {
				storageState.putFormParams(formParams);
				storageState.putInfo("url", domain + "/" + relativePath);
				storageState.putInfo("suffix", suffix);
				storageState.putInfo("savepath", savePath);
				storageState.putInfo("relative", relativePath);
				storageState.putInfo("original", originFileName);
			}

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

	private static boolean validType(String type, String allowTypes) {
		List list = Arrays.asList(allowTypes.split("||"));

		return list.contains(type);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy