upload image to 后端

裴令秋
2023-12-01

postman上传MultipartFile到后端(阿里云)

Spring-mvc中配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"></property>
    <property name="maxUploadSize" value="10485760000"></property>
    <property name="maxInMemorySize" value="40960"></property>
</bean>

Spirng-properties中配置阿里云配置

<bean id="aliyunOssPropertiesLoader" class="com.sitech.ecommerce.product.common.utils.AliyunOssPropertiesLoader">
    <property name="aliyunOssEndpoint" value="${aliyun_oss_endpoint}" />
    <property name="aliyunOssAccessId" value="${aliyun_oss_access_id}" />
    <property name="aliyunOssAccessKey" value="${aliyun_oss_access_key}" />
    <property name="aliyunOssPublicBucketName" value="${aliyun_oss_public_bucket_name}" />
    <property name="aliyunOssUrlPrefix" value="${aliyun_oss_url_prefix}" />
</bean>

添加配置文件aliyun.config.properties配置上面参数

后台controller:

public String uploads(HttpServletRequest request){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//设置编码
commonsMultipartResolver.setDefaultEncoding("utf-8");

//判断 request 是否有文件上传
if (!commonsMultipartResolver.isMultipart(request)){
    throw new ServiceException(StatusCode.STATUSCODE_UPLOAD_FAIL, ExceptionMessageUtil.getMessage(StatusCode.STATUSCODE_UPLOAD_FAIL));
}
MultipartHttpServletRequest mulReq = (MultipartHttpServletRequest) request;
List<MultipartFile> files = mulReq.getFiles("files");

下面调用阿里云上传接口

 

 

 类似资料: