Search in sources :

Example 1 with UploadConfigBean

use of org.eweb4j.config.bean.UploadConfigBean in project eweb4j-framework by laiweiwei.

the class UploadUtil method handleUpload.

public static void handleUpload(Context context) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    UploadConfigBean ucb = cb.getMvc().getUpload();
    String tmpDir = ucb.getTmp();
    int memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(ucb.getMaxMemorySize()) + "");
    long sizeMax = CommonUtil.parseFileSize(ucb.getMaxRequestSize());
    if (tmpDir.trim().length() == 0)
        tmpDir = "${RootPath}" + File.separator + "WEB-INF" + File.separator + "tmp";
    tmpDir = tmpDir.replace("${RootPath}", ConfigConstant.ROOT_PATH);
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(memoryMax);
    factory.setRepository(new File(tmpDir));
    ServletFileUpload _upload = new ServletFileUpload(factory);
    if (!_upload.isMultipartContent(context.getRequest()))
        return;
    _upload.setSizeMax(sizeMax);
    try {
        List<FileItem> items = _upload.parseRequest(context.getRequest());
        Iterator<FileItem> it = items.iterator();
        while (it.hasNext()) {
            FileItem item = it.next();
            String fieldName = item.getFieldName();
            if (item.isFormField()) {
                String value = item.getString();
                context.getQueryParamMap().put(fieldName, new String[] { value });
            } else {
                String fileName = item.getName();
                if (fileName == null || fileName.trim().length() == 0)
                    continue;
                String stamp = CommonUtil.getNowTime("yyyyMMddHHmmss");
                File tmpFile = new File(tmpDir + File.separator + stamp + "_" + fileName);
                item.write(tmpFile);
                UploadFile uploadFile = new UploadFile(tmpFile, fileName, fieldName, item.getSize(), item.getContentType());
                if (context.getUploadMap().containsKey(fieldName)) {
                    context.getUploadMap().get(fieldName).add(uploadFile);
                } else {
                    List<UploadFile> uploads = new ArrayList<UploadFile>();
                    uploads.add(uploadFile);
                    context.getUploadMap().put(fieldName, uploads);
                }
            }
        }
    } catch (InvalidContentTypeException e) {
        throw new Exception("upload file error", e);
    }
}
Also used : InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) ArrayList(java.util.ArrayList) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) File(java.io.File) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean)

Example 2 with UploadConfigBean

use of org.eweb4j.config.bean.UploadConfigBean in project eweb4j-framework by laiweiwei.

the class ConfigBeanCreator method create.

public static ConfigBean create() {
    ConfigBean cb = new ConfigBean();
    I18N i18n = new I18N();
    Locale locale = new Locale();
    locale.setLanguage(java.util.Locale.CHINA.getLanguage());
    locale.setCountry(java.util.Locale.CHINA.getCountry());
    i18n.getLocale().add(locale);
    cb.setLocales(i18n);
    Properties props = new Properties();
    Prop file = new Prop();
    props.getFile().add(file);
    cb.setProperties(props);
    LogsConfigBean lcb = new LogsConfigBean();
    ConfigIOC ioc = new ConfigIOC();
    IOCXmlFiles iocXmlFiles = new IOCXmlFiles();
    iocXmlFiles.setPath(new ArrayList<String>());
    ioc.setIocXmlFiles(iocXmlFiles);
    ioc.setLogs(lcb);
    cb.setIoc(ioc);
    ConfigORM orm = new ConfigORM();
    orm.setLogs(lcb);
    Ddl ddl = new Ddl();
    orm.setDdl(ddl);
    ORMXmlFiles ormXmlFiles = new ORMXmlFiles();
    orm.setOrmXmlFiles(ormXmlFiles);
    ScanPojoPackage spp = new ScanPojoPackage();
    orm.setScanPojoPackage(spp);
    DBInfoXmlFiles dbInfoXmlFiles = new DBInfoXmlFiles();
    dbInfoXmlFiles.setPath(new ArrayList<String>());
    orm.setDbInfoXmlFiles(dbInfoXmlFiles);
    cb.setOrm(orm);
    ConfigMVC mvc = new ConfigMVC();
    mvc.setLogs(lcb);
    UploadConfigBean upload = new UploadConfigBean();
    mvc.setUpload(upload);
    ActionXmlFile actionFiles = new ActionXmlFile();
    actionFiles.setPath(new ArrayList<String>());
    mvc.setActionXmlFiles(actionFiles);
    InterXmlFile intFiles = new InterXmlFile();
    intFiles.setPath(new ArrayList<String>());
    mvc.setInterXmlFiles(intFiles);
    ScanActionPackage sap = new ScanActionPackage();
    mvc.setScanActionPackage(sap);
    ScanInterceptorPackage sip = new ScanInterceptorPackage();
    mvc.setScanInterceptorPackage(sip);
    cb.setMvc(mvc);
    return cb;
}
Also used : Locale(org.eweb4j.config.bean.Locale) ActionXmlFile(org.eweb4j.config.bean.ActionXmlFile) Prop(org.eweb4j.config.bean.Prop) ConfigIOC(org.eweb4j.config.bean.ConfigIOC) ConfigMVC(org.eweb4j.config.bean.ConfigMVC) InterXmlFile(org.eweb4j.config.bean.InterXmlFile) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) LogsConfigBean(org.eweb4j.config.bean.LogsConfigBean) ScanInterceptorPackage(org.eweb4j.config.bean.ScanInterceptorPackage) Properties(org.eweb4j.config.bean.Properties) ScanPojoPackage(org.eweb4j.config.bean.ScanPojoPackage) Ddl(org.eweb4j.config.bean.Ddl) IOCXmlFiles(org.eweb4j.config.bean.IOCXmlFiles) ConfigORM(org.eweb4j.config.bean.ConfigORM) LogsConfigBean(org.eweb4j.config.bean.LogsConfigBean) ScanActionPackage(org.eweb4j.config.bean.ScanActionPackage) ORMXmlFiles(org.eweb4j.config.bean.ORMXmlFiles) I18N(org.eweb4j.config.bean.I18N) DBInfoXmlFiles(org.eweb4j.config.bean.DBInfoXmlFiles) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean)

Aggregations

ConfigBean (org.eweb4j.config.bean.ConfigBean)2 UploadConfigBean (org.eweb4j.config.bean.UploadConfigBean)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 FileItem (org.apache.commons.fileupload.FileItem)1 InvalidContentTypeException (org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 ActionXmlFile (org.eweb4j.config.bean.ActionXmlFile)1 ConfigIOC (org.eweb4j.config.bean.ConfigIOC)1 ConfigMVC (org.eweb4j.config.bean.ConfigMVC)1 ConfigORM (org.eweb4j.config.bean.ConfigORM)1 DBInfoXmlFiles (org.eweb4j.config.bean.DBInfoXmlFiles)1 Ddl (org.eweb4j.config.bean.Ddl)1 I18N (org.eweb4j.config.bean.I18N)1 IOCXmlFiles (org.eweb4j.config.bean.IOCXmlFiles)1 InterXmlFile (org.eweb4j.config.bean.InterXmlFile)1 Locale (org.eweb4j.config.bean.Locale)1 LogsConfigBean (org.eweb4j.config.bean.LogsConfigBean)1 ORMXmlFiles (org.eweb4j.config.bean.ORMXmlFiles)1