sharp-fileupload客户端Restful(四)

客户端api可以用来上传、下载、浏览文件等操作,由 DocumentService 完成服务操作。DocumentService 主要工作:

  • 管理数据库维护元数据
  • FileStore 来完成存储
  • 依赖 ImageService 完成图片类型的浏览

准备工作

创建表

CREATE TABLE `sys_document` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL,
  `extension` varchar(16) DEFAULT NULL,
  `content_type` varchar(16) DEFAULT NULL,
  `size` int(11) DEFAULT NULL,
  `group_name` varchar(255) NOT NULL,
  `path` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

添加依赖

<dependency>
    <groupId>com.rick.db</groupId>
    <artifactId>sharp-database</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

配置数据库

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/fastdfs?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2b8
    username: root
    password: jkxyx205

接口测试

批量上传

  • 请求
POST /documents/upload HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="file"; filename="/Users/rick/Desktop/StockSnap_STDZONQNPW.jpg
Content-Disposition: form-data; name="file"; filename="/Users/rick/Desktop/java技能树.jpg


------WebKitFormBoundary7MA4YWxkTrZu0gW--
  • 后端处理
@PostMapping("/upload")
public Result<List<?>> fileUpload(MultipartHttpServletRequest multipartRequest) throws IOException {
    return ResultUtils.success(documentService.store(FileMetaUtils.parse(multipartRequest, UPLOAD_NAME), "upload"));
}

@PostMapping("/upload2")
public Result<List<?>> fileUpload2(@RequestParam(UPLOAD_NAME) List<MultipartFile> fileList) throws IOException {
    return ResultUtils.success(documentService.store(FileMetaUtils.parse(fileList), "upload"));
}

@PostMapping("/upload3")
public Result<List<?>> fileUpload3(@RequestBody List<FileMeta> fileMetaList) throws IOException {
    return ResultUtils.success(documentService.store(fileMetaList, "upload"));
}
  • 响应
{
    "success": true,
    "code": 0,
    "msg": "OK",
    "data": [
        {
            "name": "StockSnap_STDZONQNPW",
            "extension": "jpg",
            "contentType": "image/jpeg",
            "size": 30079171,
            "groupName": "upload",
            "path": "475304840076365824.jpg",
            "url": "http://localhost:7892/upload/475304840076365824.jpg",
            "id": 475304840302858240,
            "createdAt": "2021-10-01T04:24:42.474Z",
            "fullName": "StockSnap_STDZONQNPW.jpg",
            "fullPath": "upload/475304840076365824.jpg"
        },
        {
            "name": "java技能树",
            "extension": "jpg",
            "contentType": "image/jpeg",
            "size": 337707,
            "groupName": "upload",
            "path": "475304840239943680.jpg",
            "url": "http://localhost:7892/upload/475304840239943680.jpg",
            "id": 475304840332218368,
            "createdAt": "2021-10-01T04:24:42.481Z",
            "fullName": "java技能树.jpg",
            "fullPath": "upload/475304840239943680.jpg"
        }
    ]
}

查看信息

  • 请求
GET /documents/475304840302858240 HTTP/1.1
  • 后端处理
/**
 * 文件详情
 * @return
 */
@GetMapping(value = "/{id}")
public Result<Document> documentInfo(@PathVariable Long id) {
    return ResultUtils.success(documentService.findById(id));
}
  • 响应
{
    "success": true,
    "code": 0,
    "msg": "OK",
    "data": {
        "name": "StockSnap_STDZONQNPW",
        "extension": "jpg",
        "contentType": "image/jpeg",
        "size": 30079171,
        "groupName": "upload",
        "path": "475304840076365824.jpg",
        "url": "http://localhost:7892/upload/475304840076365824.jpg",
        "id": 475304840302858240,
        "createdAt": "2021-10-01T04:24:42Z",
        "fullName": "StockSnap_STDZONQNPW.jpg",
        "fullPath": "upload/475304840076365824.jpg"
    }
}

下载

  • 请求
# 单文件下载

http://localhost:8080/documents/download?id=475308098194935808

# 批量下载

http://localhost:8080/documents/download?id=475308098194935808&id=475308098169769984

  • 后端处理
@GetMapping(value = "/download/{id}")
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable long id) throws IOException {
    documentService.download(request, response , id);
}

@GetMapping(value = "/download")
public void download(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = "id") long[] ids) throws IOException {
    documentService.download(request, response, ids);
}

浏览

  • 请求
# 客户端跳转

http://localhost:8080/documents/preview/475308098194935808

# 图片进行处理

http://localhost:8080/documents/preview2/475308098194935808?rw=9&rh=5

# office预览必须带上文件扩展名

https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fa923-112-87-216-2.ngrok.io%2Fdocuments%2Fpreview2%2F477893013692383232/小程序部署准备工作.docx


https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fa923-112-87-216-2.ngrok.io%2Fdocuments%2Fpreview2%2F477897073204035588/演示.pptx


https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fa923-112-87-216-2.ngrok.io%2Fdocuments%2Fpreview2%2F477897073204035587/需求.xlsx

Note: the needs to be URL encoded, and the document must be publicly accessible on the internet.

微软接口文档

  • 后端处理
 /**
 * 预览文件: 通过nginx,这种访问速度会更快些
 * http://localhost:8080/documents/preview/475029213054144512
 * @return
 * @throws IOException
 */
@GetMapping(value = "/preview/{id:\\d+}")
public void view(HttpServletResponse response, @PathVariable Long id) throws IOException {
    response.sendRedirect(documentService.getURL(id));
}

/**
 * 预览普通文件,将文件写到流中
 * http://localhost:8080/documents/preview2/475029213054144512
 * 预览office,必须有后缀docx/xlsx/pptx,否则会File not found
 * https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fa923-112-87-216-2.ngrok.io%2Fdocuments%2Fpreview2%2F477896371325009920/hello.docx
 * @return
 * @throws IOException
 */
@GetMapping(value = {"/preview2/{id:\\d+}", "/preview2/{id:\\d+}/{fileName}.{extension:(?i)docx|xlsx|pptx}"})
public void view2(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id, ImageParam imageParam) throws IOException {
    Document document = documentService.findById(id);
    documentService.preview(id, imageParam, HttpServletResponseUtils.getOutputStreamAsView(request, response, document.getFullName()));
    }

重命名

  • 请求
PUT /documents/475029213070921728/rename?name=hello world HTTP/1.1```
  • 后端处理
@PutMapping("/{id}/rename")
public Result rename(@PathVariable Long id,  String name) {
    documentService.rename(id, name);
    return ResultUtils.success();
}

删除

  • 请求
DELETE /documents/475010776193994752 HTTP/1.1
  • 响应
{
    "success": true,
    "code": 0,
    "msg": "OK"
}
  • 后端处理
    @DeleteMapping(value = "/{id}")
    public Result deleteDocument(@PathVariable Long id) {
        documentService.delete(id);
        return ResultUtils.success();
    }
  • 响应
{
    "success": true,
    "code": 0,
    "msg": "OK"
}