文章目录
SpringCloudFeign多文件上传Feign多文件上传POM依赖Fiegn接口端配置类Feign接口
接口提供端接口消费端在代码中调用Feign多文件上传,上面代码不需要改变消费方代码
SpringCloudFeign多文件上传
Feign多文件上传
POM依赖
<dependency>
<groupId>io.github.openfeign.form
</groupId>
<artifactId>feign-form
</artifactId>
<version>3.0.3
</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form
</groupId>
<artifactId>feign-form-spring
</artifactId>
<version>3.0.3
</version>
</dependency>
Fiegn接口端
配置类
public class FeignFormEncoder extends FormEncoder {
public FeignFormEncoder() {
this(new Default());
}
public FeignFormEncoder(Encoder delegate
) {
super(delegate
);
MultipartFormContentProcessor processor
= (MultipartFormContentProcessor
) getContentProcessor(MULTIPART
);
processor
.addWriter(new SpringSingleMultipartFileWriter());
processor
.addWriter(new SpringManyMultipartFilesWriter());
}
@Override
public void encode(Object object
, Type bodyType
, RequestTemplate template
) throws EncodeException
{
if (bodyType
.equals(MultipartFile
.class)) {
MultipartFile file
= (MultipartFile
) object
;
super.encode(singletonMap(file
.getName(), object
), MAP_STRING_WILDCARD
, template
);
return;
} else if (bodyType
.equals(MultipartFile
[].class)) {
MultipartFile
[] file
= (MultipartFile
[]) object
;
if (file
!= null
) {
super.encode(singletonMap(file
.length
== 0 ? "" : file
[0].getName(), object
), MAP_STRING_WILDCARD
, template
);
return;
}
}
super.encode(object
, bodyType
, template
);
}
}
Feign接口
@FeignClient(value
= "xxxxx-file", configuration
= FileFeignClient
.MultipartSupportConfig
.class)
public interface FileFeignClient {
@RequestMapping(value
= "/file/api/v1/files", method
= RequestMethod
.POST
, consumes
= MediaType
.MULTIPART_FORM_DATA_VALUE
)
List
<Map
<String, String>> handleFileUpload(@RequestPart(value
= "file", required
= false) MultipartFile
[] files
,);
@Configuration
class MultipartSupportConfig {
@Autowired
private ObjectFactory
<HttpMessageConverters> messageConverters
;
@Bean
@Primary
@Scope("prototype")
public Encoder
feignFormEncoder() {
return new FeignFormEncoder(new SpringEncoder(messageConverters
));
}
}
}
接口提供端
@PostMapping(value
= "/file/api/v1/files", consumes
= MediaType
.MULTIPART_FORM_DATA_VALUE
)
@ResponseBody
public List
<Map
<String,String>> handleFileUpload(@RequestPart(value
= "file",required
=false) MultipartFile
[] files
) throws Exception
{
}
接口消费端
@PostMapping(value
= "/aa",consumes
= MediaType
.MULTIPART_FORM_DATA_VALUE
)
public List
<Map
<String, String>> aaa(@RequestPart(value
= "file") MultipartFile
[] files
) {
List
<Map
<String, String>> maps
= fileFeignClient
.handleFileUpload(files
);
return maps
;
}
在代码中调用Feign多文件上传,上面代码不需要改变
如遇到在代码里面去调用文件上传的Feign接口怎么处理呢,不要慌,因为我之前在工作中也遇到了。
在消费方也就是调用方POM添加依赖
<dependency>
<groupId>commons-fileupload
</groupId>
<artifactId>commons-fileupload
</artifactId>
<version>1.3.1
</version>
</dependency>
消费方代码
import org
.apache
.commons
.fileupload
.FileItem
;
import org
.apache
.commons
.fileupload
.FileItemFactory
;
import org
.apache
.commons
.fileupload
.disk
.DiskFileItemFactory
;
@PostMapping(value
= "/aa",consumes
= MediaType
.MULTIPART_FORM_DATA_VALUE
)
public List
<Map
<String, String>> aaa(@RequestPart(value
= "file") MultipartFile
[] files
) {
MultipartFile
[] files
= new MultipartFile[1];
File file
= new File("D:\\壁纸\\timg.jpg");
FileItem fileItem
= createFileItem(file
, file
.getName());
MultipartFile mfile
= new CommonsMultipartFile(fileItem
);
files
[0] = mfile
;
List
<Map
<String, String>> maps
= fileFeignClient
.handleFileUpload(files
);
return maps
;
}
private FileItem
createFileItem(File file
, String fieldName
) {
FileItemFactory factory
= new DiskFileItemFactory(16, null
);
FileItem item
= factory
.createItem("file", ContentType
.MULTIPART
.getHeader(), true, file
.getName());
int bytesRead
= 0;
byte[] buffer
= new byte[8192];
try {
FileInputStream fis
= new FileInputStream(file
);
OutputStream os
= item
.getOutputStream();
while ((bytesRead
= fis
.read(buffer
, 0, 8192)) != -1) {
os
.write(buffer
, 0, bytesRead
);
}
os
.close();
fis
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
return item
;
}