服务端接口:
@ApiOperation(value = "上传文件或者文件夹"
+ "如果已经存在,则增加新的版本。", notes = "通过 multipart/form-data 的方式上传文件和设置必要的参数(如:user data),如果上传文件夹,则path参数需要填写对应的文件路径")
@ApiResponses(value = {
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = ResponseMessages.OK, response = FileModelDataWrapper.class),
@ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = ResponseMessages.UNAUTHORIZED, response = ExceptionModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = ResponseMessages.FORBIDDEN, response = ExceptionModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_CONFLICT, message = ResponseMessages.NOT_FOUND, response = ExceptionModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = ResponseMessages.INTERNAL_ERROR, response = ExceptionModel.class)})
@PutMapping(value = "/projects/{projectId}/parent/{parentId}/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity createFileOrNewVersion(HttpServletRequest request,
@ApiParam(value = "Project ID", required = true) @PathVariable("projectId") String projectId,
@ApiParam(value = "父级 Folder 的 ID", required = true) @PathVariable("parentId") String folderId,
@RequestParam(value = "file", required = false) MultipartFile file,
@ApiParam(value = "上传文件夹时文件的路径") @RequestParam(value = "path", required = false) String path) {
try {
checkUploadSignature(request);
......
} catch(Exception e) {
return ServiceExceptionHandler.newHandle(e);
}
}
封装服务端接口的SDK:
1、引入依赖:
<!-- feign-form -->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
</dependency>
2、注册SpringFormEncoder:
@Configuration
class MultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() throws BeansException {
return new HttpMessageConverters(new RestTemplate().getMessageConverters());
}
}));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
3、配置FeignClient及定义接口(注意使用@RequestPart注解MultipartFile参数):
@FeignClient(value = "server-sfs-service", url = "${server.sfs.url}", path = "${server.sfs.versionPath}",
configuration = SfsClient.MultipartSupportConfig.class)
public interface SfsClient {
@PutMapping(value = "/projects/{projectId}/parent/{parentId}/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
FileModelDataWrapper createFileOrNewVersion(@PathVariable("projectId") String projectId,
@PathVariable("parentId") String folderId,
@RequestPart("file") MultipartFile file);
@Configuration
class MultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() throws BeansException {
return new HttpMessageConverters(new RestTemplate().getMessageConverters());
}
}));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
}
调用方:
import org.springframework.mock.web.MockMultipartFile;
File file = new File("C:\\Users\\A\\Desktop\\test.txt");
MultipartFile multipartFile = new MockMultipartFile("file",
file.getName(), null, new FileInputStream(file));
sfsClient.createFileOrNewVersion("Ka", "root", multipartFile);
网友评论