SpringMVC上传文件自动获取请求name并上传

配置解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8">property>
        
        <property name="maxUploadSize" value="52428800">property>
        
        <property name="maxInMemorySize" value="5242880">property>
    bean>

Controller

		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());

        if (multipartResolver.isMultipart(request)) {
            Iterator<String> iterator = multiRequest.getFileNames();
            try {
                MultipartFile updateFile = multiRequest.getFile(iterator.next().toString());
                String base64 = new String(Base64.encodeBase64(updateFile.getBytes()));
                FileCloudUploadResultModel fileCloudUploadResultModel = fileCloudService.uploadFiles(base64);
                if (fileCloudUploadResultModel == null) {
                    return Result.fail("上传失败");
                }
                return Result.success(fileCloudUploadResultModel);
            } catch (Exception e) {
                log.error("uploadFile exception:", e);
                return Result.fail("上传失败");
            }
        }
        return Result.fail("上传失败");

你可能感兴趣的:(Java)