瑞吉外卖 文件下载上传
# 瑞吉外卖 文件上传和下载
```java
@Slf4j
@RestController
@RequestMapping("/common")
public class FileController {
@Value("${reggie.path}")
String path;
@PostMapping("/upload")
public R<String> upload(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + suffix;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
file.transferTo(new File(path + fileName));
return R.success(fileName);
}
@PostMapping("/download")
public void download(String name, HttpServletResponse response) throws IOException {
FileInputStream fileInputStream = new FileInputStream(new File(path + name));
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
byte[] bytes = new byte[1024];
int length = 0;
while ((length = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
outputStream.flush();
}
outputStream.close();
fileInputStream.close();
}
}
```