package market.Qms.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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;

public class ExcelHelper {

	/** 获取FileItem */
	public static List<FileItem> getUpload(Object className, File file, HttpServletRequest request)
			throws FileUploadException {
		// 获取文件流
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 1024 * 5);// 5M 单个文件的最大大小
		factory.setRepository(file);
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(1024 * 1024 * 5); // 5M 总的大小
		return upload.parseRequest(request);
	}

	public static InputStream inputStream(FileItem item, long length) throws IOException {
		
		// 获取文件名
		String fileName = item.getName();
		// 解决fileName兼容性问题
		int lastindex = fileName.lastIndexOf("\\");
		fileName = fileName.substring(lastindex + 1);
		// 获取文件流
		InputStream in = item.getInputStream();
		return in;
	}

}
