FeignClient跨服务上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
FeignClient导出 需要返回ResponseEntity<byte[]>
ByteArrayOutputStream out = new ByteArrayOutputStream();
XSSFWorkbook workbook = exportCommonExcel(datas,simpleExportBean,null,null,0);
workbook.write(out);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode(simpleExportBean.getExportFileName(), "UTF-8")+".xlsx");
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(out.toByteArray(), httpHeaders, HttpStatus.OK);
return filebyte;

跨服务上传
https://blog.csdn.net/u014534643/article/details/82495648
1.
<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>
2.
configuration = UpDownFtpFacade.MultipartSupportConfig.class)
@RequestPart(value = "file") MultipartFile file,
class MultipartSupportConfig {


@FeignClient(value = "**", configuration = UpDownFtpFacade.MultipartSupportConfig.class)
public interface UpDownFtpFacade {

/**
* FTP上传文件
*
* @param file 文件
* @param logId 日志id
* @return
*/
@PostMapping(value = "/ftp/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
FtpApiResponse<FtpUploadResDTO> uploadFileFTP(@RequestPart(value = "file") MultipartFile file,
@RequestParam("logId") String logId);

/**
* FTP下载文件
*
* @param fileName 文件名
* @param logId 日志id
* @return
*/
@PostMapping(value = "/ftp/downloadFile")
FtpApiResponse<String> downloadFileFTP(@RequestParam("fileName") String fileName,
@RequestParam("logId") String logId);

/**
* 引用配置类MultipartSupportConfig.并且实例化
*/
@Configuration
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.xxxxxx.xxxxxx.config;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentType;
import feign.form.FormEncoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;

/**
* Created by xxxxxx on 2020/3/25.
*/
public class FeignSpringFormEncoder extends FormEncoder {
public FeignSpringFormEncoder() {
this(new Default());
}
public FeignSpringFormEncoder(Encoder delegate) {
super(delegate);

MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.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;
Map data = Collections.singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
} else if (bodyType.equals(MultipartFile[].class)) {
MultipartFile[] file = (MultipartFile[]) object;
if(file != null) {
Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
}
}
super.encode(object, bodyType, template);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.xxxxxx.xxxxxx.config;

import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;


@Configuration
public class FeignConfiguration {
@Bean
public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));
}
@Value("${security.user.name}")
private String userName;

@Value("${security.user.password}")
private String passWord;

@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){
return new BasicAuthRequestInterceptor(userName, passWord, Charset.forName("UTF-8"));
}
}