Search in sources :

Example 16 with CommonsMultipartFile

use of org.springframework.web.multipart.commons.CommonsMultipartFile in project Gemma by PavlidisLab.

the class CommonsMultipartMonitoredResolver method cleanupMultipart.

@Override
public void cleanupMultipart(MultipartHttpServletRequest request) {
    if (request instanceof FailedMultipartHttpServletRequest)
        return;
    Map<String, MultipartFile> multipartFiles = request.getFileMap();
    for (MultipartFile multipartFile : multipartFiles.values()) {
        CommonsMultipartFile file = (CommonsMultipartFile) multipartFile;
        if (logger.isDebugEnabled()) {
            logger.debug("Cleaning up multipart file [" + file.getName() + "] with original filename [" + file.getOriginalFilename() + "], stored " + file.getStorageDescription());
        }
        file.getFileItem().delete();
    }
}
Also used : CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile)

Example 17 with CommonsMultipartFile

use of org.springframework.web.multipart.commons.CommonsMultipartFile in project DCGyunpan by JKchenmin.

the class FileUtil method fileUpLoad.

public List<Map<String, Object>> fileUpLoad(CommonsMultipartFile[] file) {
    PropertiesUtil pUtil = new PropertiesUtil();
    List<Map<String, Object>> fList = new ArrayList<>();
    try {
        // 获取系统时间作为文件上传的时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(day));
        String fileTime = df.format(day).toString();
        Properties properties = pUtil.readProperties("jdbc.properties");
        String filePath = properties.getProperty("filepath");
        for (int i = 0; i < file.length; i++) {
            String fileName = file[i].getOriginalFilename();
            String newFileName = getNewName(fileName);
            File dir = new File(filePath, newFileName);
            if (!dir.exists()) {
                System.out.println("文件不存在,准备创建!");
                dir.mkdir();
            }
            try {
                file[i].transferTo(dir);
                Map<String, Object> map = new HashMap<>();
                // 文件的原来名称
                map.put("fileOriginalName", fileName);
                // 文件上传到服务器新的名字
                map.put("fileNewName", newFileName);
                // 文件上传的时间
                map.put("fileTime", fileTime);
                // 文件上传到服务器的地址
                map.put("fileUrl", filePath);
                fList.add(map);
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Map<String, Object> upFileMap = new HashMap();
        upFileMap.put("upFileFlag", true);
        fList.add(upFileMap);
    } catch (Exception e) {
        e.printStackTrace();
        Map<String, Object> upFileMap = new HashMap();
        upFileMap.put("upFileFlag", false);
        fList.add(upFileMap);
    }
    return fList;
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) SimpleDateFormat(java.text.SimpleDateFormat) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) File(java.io.File)

Example 18 with CommonsMultipartFile

use of org.springframework.web.multipart.commons.CommonsMultipartFile in project collect by openforis.

the class FileUploadController method uploadFile.

@RequestMapping(value = "/uploadFile.htm", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(UploadItem uploadItem) throws IOException, SurveyImportException {
    CommonsMultipartFile fileData = uploadItem.getFileData();
    File file = File.createTempFile("collect_", fileData.getOriginalFilename());
    InputStream is = fileData.getInputStream();
    FileUtils.copyInputStreamToFile(is, file);
    return file.getAbsolutePath();
}
Also used : InputStream(java.io.InputStream) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 19 with CommonsMultipartFile

use of org.springframework.web.multipart.commons.CommonsMultipartFile in project collect by openforis.

the class LogoController method uploadLogo.

@RequestMapping(value = "/uploadLogo.htm", method = RequestMethod.POST)
@ResponseBody
public String uploadLogo(UploadItem uploadItem, BindingResult result, @RequestParam String position) throws IOException, SurveyImportException, IdmlParseException {
    CommonsMultipartFile fileData = uploadItem.getFileData();
    InputStream is = fileData.getInputStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    IOUtils.copy(is, output);
    byte[] byteArray = output.toByteArray();
    String contentType = fileData.getContentType();
    LogoPosition p = LogoPosition.valueOf(position.toUpperCase());
    Logo logo = new Logo(p, byteArray, contentType);
    logoManager.save(logo);
    return "ok";
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) LogoPosition(org.openforis.collect.model.LogoPosition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) Logo(org.openforis.collect.model.Logo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 20 with CommonsMultipartFile

use of org.springframework.web.multipart.commons.CommonsMultipartFile in project Asqatasun by Asqatasun.

the class AddScenarioFormValidator method checkScenarioFileTypeAndSize.

/**
 * @param addScenarioCommand
 * @param errors
 * @return  whether the scenario handled by the current AddScenarioCommand
 * has a correct type and size
 */
public boolean checkScenarioFileTypeAndSize(AddScenarioCommand addScenarioCommand, Errors errors) {
    if (addScenarioCommand.getScenarioFile() == null) {
        // if no file uploaded
        LOGGER.debug("empty Scenario File");
        errors.rejectValue(GENERAL_ERROR_MSG_KEY, MANDATORY_FIELD_MSG_BUNDLE_KEY);
        errors.rejectValue(SCENARIO_FILE_KEY, NO_SCENARIO_UPLOADED_MSG_BUNDLE_KEY);
        return false;
    }
    Metadata metadata = new Metadata();
    MimeTypes mimeTypes = TikaConfig.getDefaultConfig().getMimeRepository();
    String mime;
    try {
        CommonsMultipartFile cmf = addScenarioCommand.getScenarioFile();
        if (cmf.getSize() > maxFileSize) {
            Long maxFileSizeInMega = maxFileSize / 1000000;
            String[] arg = { maxFileSizeInMega.toString() };
            errors.rejectValue(GENERAL_ERROR_MSG_KEY, MANDATORY_FIELD_MSG_BUNDLE_KEY);
            errors.rejectValue(SCENARIO_FILE_KEY, FILE_SIZE_EXCEEDED_MSG_BUNDLE_KEY, arg, "{0}");
            return false;
        } else if (cmf.getSize() > 0) {
            mime = mimeTypes.detect(new BufferedInputStream(cmf.getInputStream()), metadata).toString();
            LOGGER.debug("mime  " + mime + "  " + cmf.getOriginalFilename());
            if (!authorizedMimeType.contains(mime)) {
                errors.rejectValue(GENERAL_ERROR_MSG_KEY, MANDATORY_FIELD_MSG_BUNDLE_KEY);
                errors.rejectValue(SCENARIO_FILE_KEY, NOT_SCENARIO_MSG_BUNDLE_KEY);
                return false;
            }
        } else {
            LOGGER.debug("File with size null");
            errors.rejectValue(GENERAL_ERROR_MSG_KEY, MANDATORY_FIELD_MSG_BUNDLE_KEY);
            errors.rejectValue(SCENARIO_FILE_KEY, NO_SCENARIO_UPLOADED_MSG_BUNDLE_KEY);
            return false;
        }
    } catch (IOException ex) {
        LOGGER.warn(ex.getMessage());
        errors.rejectValue(SCENARIO_FILE_KEY, NOT_SCENARIO_MSG_BUNDLE_KEY);
        errors.rejectValue(GENERAL_ERROR_MSG_KEY, MANDATORY_FIELD_MSG_BUNDLE_KEY);
        return false;
    }
    return true;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Metadata(org.apache.tika.metadata.Metadata) IOException(java.io.IOException) MimeTypes(org.apache.tika.mime.MimeTypes) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile)

Aggregations

CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)23 InputStream (java.io.InputStream)9 IOException (java.io.IOException)8 File (java.io.File)7 BaseTest (com.epam.ta.reportportal.BaseTest)5 Test (org.junit.jupiter.api.Test)5 MifosRuntimeException (org.mifos.core.MifosRuntimeException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 MultipartFile (org.springframework.web.multipart.MultipartFile)4 FileItem (org.apache.commons.fileupload.FileItem)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)2 Metadata (org.apache.tika.metadata.Metadata)2 MimeTypes (org.apache.tika.mime.MimeTypes)2 LocalDate (org.joda.time.LocalDate)2 CreateAccountFeeDto (org.mifos.dto.domain.CreateAccountFeeDto)2 CreateAccountPenaltyDto (org.mifos.dto.domain.CreateAccountPenaltyDto)2 UploadedFileDto (org.mifos.dto.screen.UploadedFileDto)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2