引言
本文将要介绍 flea-jersey 提供的文件上传功能。
文件上传功能,需要引入 Flea RESTful 接口服务端和客户端依赖,详细如下所示:
1. 客户端依赖
1 2 3 4 5 6
| <dependency> <groupId>com.huazie.fleaframework</groupId> <artifactId>flea-jersey-client</artifactId> <version>2.0.0</version> </dependency>
|
2. 服务端依赖
1 2 3 4 5 6
| <dependency> <groupId>com.huazie.fleaframework</groupId> <artifactId>flea-jersey-server</artifactId> <version>2.0.0</version> </dependency>
|
3. 文件上传接入讲解
Flea RESTful接口服务端和客户端接入,本篇博文不再赘述;可见笔者 flea-jersey 下的文章。
3.1 服务端上传资源定义
上传资源继承 FleaJerseyPostResource,该类定义可至GitHub查看 flea-jersey-server,具体如下所示:
1 2 3 4
| @Path("upload") public class UploadResource extends FleaJerseyPostResource {
}
|
1 2 3 4 5 6 7 8 9 10 11
| public abstract class FleaJerseyPostResource extends FleaJerseyFPostResource implements JerseyPostResource {
@Override public FleaJerseyResponse doPostResource(FleaJerseyRequest request) { return doResource(request); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| public abstract class FleaJerseyFPostResource extends Resource implements JerseyFileUploadResource {
@POST @Path("/fileUpload") @Override public FleaJerseyResponse doFileUploadResource(FormDataMultiPart formDataMultiPart) { return doCommonFileUploadResource(formDataMultiPart); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public interface JerseyFileUploadResource {
@Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) FleaJerseyResponse doFileUploadResource(FormDataMultiPart formDataMultiPart);
}
|
上传资源,配置参考如下:
3.2 服务端文件上传服务定义
文件上传服务接口类,参考实现如下:
1 2 3 4 5 6 7 8 9 10 11 12
| public interface IFleaUploadSV {
OutputFileUploadInfo fileUpload(InputFileUploadInfo input) throws Exception; }
|
文件上传服务实现类,参考实现如下:
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
| @Service public class FleaUploadSVImpl implements IFleaUploadSV {
private static final Logger LOGGER = LoggerFactory.getLogger(FleaUploadSVImpl.class); @Override public OutputFileUploadInfo fileUpload(InputFileUploadInfo input) throws Exception {
if (LOGGER.isDebugEnabled()) { LOGGER.debug("FleaUploadSVImpl#fileUpload(InputFileUploadInfo) Start"); }
String token = input.getToken(); if (StringUtils.isBlank(token)) { throw new ServiceException("ERROR-SERVICE0000000001", "上传鉴权令牌【token】"); }
FleaFileObject fileObject = FleaJerseyManager.getManager().getFileObject(); String fileName = fileObject.getFileName(); File uploadFile = fileObject.getFile();
String fileId = DateUtils.date2String(null, DateFormatEnum.YYYYMMDD) + RandomCode.toUUID(); IOUtils.toFile(new FileInputStream(uploadFile), "E:\\" + fileId + "_" +fileName); OutputFileUploadInfo outputFileUploadInfo = new OutputFileUploadInfo(); outputFileUploadInfo.setFileId(fileId);
if (LOGGER.isDebugEnabled()) { LOGGER.debug("FleaUploadSVImpl#fileUpload(InputFileUploadInfo) Start"); } return outputFileUploadInfo; } }
|
文件上传服务,配置参考如下:
3.3 客户端文件上传配置
文件上传客户端,配置参考如下:
字段名 |
中文描述 |
client_code |
客户端编码 |
resource_url |
资源地址 |
resource_code |
资源编码 |
service_code |
服务编码 |
request_mode |
请求方式 |
media_type |
媒体类型 |
client_input |
客户端业务入参 |
client_output |
客户端业务出参 |
上述配置中 请求方式 为 fpost,这里定义为文件POST请求,可参考枚举类 RequestModeEnum
1
| FPOST("FPOST", "com.huazie.fleaframework.jersey.client.request.impl.FPostFleaRequest", "文件POST请求")
|
文件POST请求具体实现,可至 GitHub查看 FPostFleaRequest
3.4 客户端文件上传调用
文件上传自测类,可至GitHub查看 JerseyTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Test public void testUploadFile() { try { String clientCode = "FLEA_CLIENT_FILE_UPLOAD";
InputFileUploadInfo input = new InputFileUploadInfo(); input.setToken(RandomCode.toUUID()); File file = new File("E:\\IMG.jpg"); FleaJerseyManager.getManager().addFileDataBodyPart(file);
FleaJerseyClient client = applicationContext.getBean(FleaJerseyClient.class); Response<OutputFileUploadInfo> response = client.invoke(clientCode, input, OutputFileUploadInfo.class);
LOGGER.debug("result = {}", response); } catch (Exception e) { LOGGER.debug("Exception = ", e); } }
|
总结
至此,文件上传已接入完毕,下篇博文将会讲解文件下载接入。