1、分页原理
2、删除
@GetMapping()
@Operation(summary = "查询指定id的记录" )
public Result<Category> getById(Long id) {
log.info("id = {}",id);
Category category= categoryService.getById(id);
return Result.success(category);
}
3、文件读写流 stream
@Operation(summary = "从配置项指定的文件夹中,下载指定的文件")
@GetMapping("/download")
public void download(String name, HttpServletResponse response) throws IOException {
FileInputStream fis =null;
ServletOutputStream os=null;
try {
fis= new FileInputStream(basePath + name);
os = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if(fis != null) {
fis.close();
}
if(os != null){
os.close();
}
}
}
4、文件上传和下载
package cn.zz.yunhe.study.shequgouv6.controller.admin;
import cn.zz.yunhe.study.shequgouv6.common.Result;
import cn.zz.yunhe.study.shequgouv6.properties.FileUpDownPath;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@Tag(name = "文件上传、下载的公共模块")
@RestController
@RequestMapping("/api/v6/admin/common")
public class CommonController {
@Autowired
private FileUpDownPath fileUpDownPath;
@Value("${shequgou.img.path}")
private String basePath;
@Operation(summary = "文件上传到配置项所指定的文件夹中,并文件名UUID格式化")
@PostMapping("/upload")
public Result<String> upload(MultipartFile file) {
log.info("上传文件文件:{},", file.toString());
log.info("从自定义配置属性中毒配置路径:{}",fileUpDownPath.getPath());
File dir = new File(basePath);
if (!dir.exists()) {
dir.mkdirs();
}
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID() + suffix;
try {
file.transferTo(new File(basePath + fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
return Result.success(fileName);
}
@Operation(summary = "从配置项指定的文件夹中,下载指定的文件")
@GetMapping("/download")
public void download(String name, HttpServletResponse response) throws IOException {
FileInputStream fis =null;
ServletOutputStream os=null;
try {
fis= new FileInputStream(basePath + name);
os = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if(fis != null) {
fis.close();
}
if(os != null){
os.close();
}
}
}
}