通过feign上传文件[原]
通过feign上传文件且传token
FeignClientTestControllerImpl(客户端Controller) | ||||
| | ||||
IFileFeign (由客户端Controller使用以调用服务端Controller) | --> | FileUploadControllerImpl (服务方Controller) | ||
FileUploadControllerImpl
package com.bobo.a.b.controller.impl; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; //服务提供方 @RestController("demonController") @RequestMapping("controller") public class FileUploadControllerImpl { @RequestMapping( value = {"/upload/{name}"}, method = {RequestMethod.POST}, consumes = MULTIPART_FORM_DATA_VALUE) public void upload(HttpServletRequest request, HttpServletResponse response, @RequestPart(value = "file", required = true) MultipartFile file, @PathVariable(value = "name") String name, @RequestParam(value = "age", required = false) String age, @RequestHeader(value = "token") String token) { System.out.println(file.getName()); InputStream is = null; FileOutputStream fos = null; try { is = file.getInputStream(); File destFile = new File("d://bbb-target.png"); fos = new FileOutputStream(destFile); cn.hutool.core.io.FileUtil.writeFromStream(is, "d://bbb-target.png"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
FeignClientTestControllerImpl
package com.bobo.a.b.controller.impl; import com.rosellete.iescp.base.annotation.StdRequestMapping; import com.rosellete.iescp.cshop.feign.IFileFeign; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.io.FileUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.io.OutputStream; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; @RestController("demonClientController") @RequestMapping("clientController") public class FeignClientTestControllerImpl { @Resource IFileFeign fileFeign; @StdRequestMapping(value = {"/upload"}, method = {RequestMethod.GET, RequestMethod.POST}) public void upload() { File file = new File("D://bbb-src.png"); final MultipartFile multipartFile = getMultipartFile(file); final String result = fileFeign.upload(multipartFile, "bobo", "18","bobotoken"); System.out.println(result); } /** * File 转 Multipart * 封装MultipartFile参数(File转MultipartFile方法)==>https://www.jianshu.com/p/43705673b1aa * 本处采用第二种方式 * * @param file * @return */ public static MultipartFile getMultipartFile(File file) { DiskFileItem item = new DiskFileItem("file" , MULTIPART_FORM_DATA_VALUE , true , file.getName() , (int) file.length() , file.getParentFile()); try { OutputStream os = item.getOutputStream(); byte[] byteArray = FileUtils.readFileToByteArray(file); os.write(byteArray); } catch (IOException e) { e.printStackTrace(); } return new CommonsMultipartFile(item); } }
IFileFeign
package com.bobo.a.b.feign; import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.POST; /** * feignClient客户端 ,Feign实现微服务间文件上传(Finchley版本)==>https://www.jianshu.com/p/4f4d9d084b1d */ @FeignClient(name = "CSHOP-BIZ-LAND", path = "/cshop-biz-land", configuration = IFileFeign.ClientConfiguration.class) public interface IFileFeign { /** * * @param file 文件 * @param name 姓名 * @param age 年龄 * @param token 令牌 * @return */ @RequestMapping( value = "/controller/upload/{name}", method = POST, consumes = MULTIPART_FORM_DATA_VALUE //处理多个参数的时候,必须指定:consumes = MULTIPART_FORM_DATA_VALUE ) public String upload( @RequestPart(value = "file", required = true) MultipartFile file, @PathVariable(value = "name") String name, @RequestParam(value = "age", required = false) String age, @RequestHeader(value = "token") String token); /** * 配置类,经测试没有该配置也能正常调用 */ class ClientConfiguration { /** * 此处注入的是: ObjectFactory<HttpMessageConverters> */ @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } } }
感觉空虚寂寞,只是因为你无所关注,无处付出。