Feign文件上传
导入jar
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>2.1.0</version>
</dependency>
client
@FeignClient(value = "HRM-COMMON",configuration=FeignMultipartSupportConfig.class,fallbackFactory = FastDfsClientFallbackFactory.class)
@RequestMapping("/fastDfs")
public interface FastDfsClient {
@PostMapping(produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
AjaxResult upload(@RequestPart(required =true,value = "file")MultipartFile file);
@GetMapping
Response download(@RequestParam(required = true,value = "path")String path);
}
fallbackFactory
@Component
public class FastDfsClientFallbackFactory implements FallbackFactory<FastDfsClient> {
@Override
public FastDfsClient create(Throwable throwable) {
return new FastDfsClient() {
@Override
public AjaxResult upload(MultipartFile file) {
return new AjaxResult().setSuccess(false).setMessage("上传失败");
}
@Override
public Response download(String path) {
return null;
}
};
}
}
config
@Configuration
public class FeignMultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder(){
return new SpringFormEncoder();
}
@Bean
public feign.Logger.Level multipartLoggerLevel(){
return Logger.Level.FULL;
}
}
service
@PostMapping(value = "/upload", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload(@RequestPart(value = "file") MultipartFile file) {
调用
!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
@Autowired
private FastDfsClient fastDfsClient;
@Test
public void test1() throws Exception{
FileItem fileItem = createFileItem(new File("C:\\Users\\Administrator\\Desktop\\问题.txt"));
MultipartFile mfile = new CommonsMultipartFile(fileItem);
fastDfsClient.upload(mfile);
}
private FileItem createFileItem(File file) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "textField";
FileItem item = factory.createItem("file", "text/plain", 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;
}
Feigin下载
Feign
@RequestMapping(value = "/download",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response download(@RequestParam("path")String path);
Service
@GetMapping(value = "/download",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void download(@RequestParam("path")String path, HttpServletResponse response) {
String pathTmp = path.substring(1);
String groupName = pathTmp.substring(0, pathTmp.indexOf("/"));
String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);
System.out.println(groupName);
System.out.println(remotePath);
OutputStream os = null;
InputStream is = null;
try {
byte[] datas = FastDfsApiOpr.download(groupName, remotePath);
os = response.getOutputStream();
is = new ByteInputStream(datas,datas.length);
IOUtils.copy(is,os);
调用
Response response =
fastDfsClient.download(templateUrl);
String tmpdir=System.getProperty("java.io.tmpdir");
System.out.println(tmpdir);
String zipName = tmpdir+"/temp.zip";
os = new FileOutputStream(zipName);
is = response.body().asInputStream();
IOUtils.copy(is , os);