提交 fc1405db 作者: 方治民

feat: 新增文件下载示例、关键字查询 Param 类、端口变更

上级 0c712c1e
......@@ -7,14 +7,19 @@ import com.yiring.app.exception.CodeException;
import com.yiring.app.mapper.TestTableMapper;
import com.yiring.common.core.Result;
import com.yiring.common.param.PageParam;
import com.yiring.common.util.FileUtils;
import com.yiring.common.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -51,6 +56,13 @@ public class HelloController {
return Result.ok(vo);
}
@ApiOperation(value = "download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
ClassPathResource resource = new ClassPathResource("static/cat.jpg");
FileUtils.download(response, resource.getFile());
}
@Resource
TestTableMapper testTableMapper;
......
server:
port: 8181
port: 8081
servlet:
context-path: /api
......
/* (C) 2021 YiRing, Inc. */
package com.yiring.common.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serial;
import java.io.Serializable;
import javax.validation.Valid;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* 公共的关键字查询参数类
*
* @author ifzm
* @version 0.1
* 2022/4/27 08:53
*/
@ApiModel("KeywordParam")
@Valid
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class KeywordParam implements Serializable {
@Serial
private static final long serialVersionUID = -8690942241103456894L;
@ApiModelProperty(value = "关键字", example = "hi")
String keyword;
}
......@@ -14,6 +14,8 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -38,8 +40,8 @@ public class MinioController {
/**
* minio 上传文件,成功返回文件 url
*/
@ApiOperation(value = "文件上传")
@PostMapping(value = "upload", headers = "Content-Type=Multipart/Form-Data")
@ApiOperation(value = "文件上传", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping(value = "upload", headers = HttpHeaders.CONTENT_TYPE + "=" + MediaType.MULTIPART_FORM_DATA_VALUE)
public Result<String> upload(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file) {
try {
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
......
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// hutool
implementation "cn.hutool:hutool-core:${hutoolVersion}"
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.common.util;
import cn.hutool.core.io.file.FileReader;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletResponse;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
/**
* 文件工具类
*
* @author Jim
* @version 0.1
* 2022/4/22 13:54
*/
@UtilityClass
public class FileUtils {
/**
* 文件下载
* @param response HttpServletResponse
* @param file File
* @throws IOException IOException
*/
public void download(HttpServletResponse response, File file) throws IOException {
String filename = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length()));
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + filename);
FileReader.create(file).writeToStream(response.getOutputStream(), true);
}
}
......@@ -4,7 +4,7 @@ pluginManagement {
gradlePluginPortal()
}
}
rootProject.name = 'basic'
rootProject.name = 'basic-api'
include 'app'
include 'basic-auth'
include 'basic-common:core'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论