add ota file upload and download
This commit is contained in:
parent
c64ccd4c5d
commit
35ebcc1bd3
|
@ -0,0 +1,79 @@
|
|||
package com.aiit.xiuos.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.aiit.xiuos.Utils.Constant;
|
||||
import com.aiit.xiuos.Utils.ResultRespons;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/ota")
|
||||
@Slf4j
|
||||
public class OTAController {
|
||||
|
||||
/*
|
||||
上传接口
|
||||
*/
|
||||
//MultipartFile用于接受前端传过来的file对象
|
||||
@PostMapping("/upload")
|
||||
public ResultRespons upload(MultipartFile file) throws IOException {
|
||||
String md5 = SecureUtil.md5(file.getInputStream());
|
||||
System.out.println("文件的md5是:"+md5);
|
||||
String filename = file.getOriginalFilename();//获取文件的名称
|
||||
// String flag = IdUtil.fastSimpleUUID();//通过Hutool工具包的IdUtil类获取uuid作为前缀
|
||||
String rootFilePath = System.getProperty("user.dir") + "/otafiles/" + filename;
|
||||
FileUtil.writeBytes(file.getBytes(), rootFilePath);//使用Hutool工具包将我们接收到文件保存到rootFilePath中
|
||||
return new ResultRespons(Constant.SUCCESS_CODE, "上传成功", filename);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
下载接口
|
||||
*/
|
||||
@GetMapping("/download/{name}")
|
||||
public ResultRespons getFiles(@PathVariable String name, HttpServletResponse response) {
|
||||
OutputStream os;//新建一个输出流对象
|
||||
String basePath = System.getProperty("user.dir") + "/otafiles/"; //定义文件上传的根路径
|
||||
List<String> fileNames = FileUtil.listFileNames(basePath);//获取所有的文件名称
|
||||
String fileName = fileNames.stream().filter(filename -> filename.contains(name)).findAny().orElse("");//找到跟参数一致的文件
|
||||
try {
|
||||
if (StrUtil.isNotEmpty(fileName)) {
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||
response.setContentType("application/octet-stream");
|
||||
byte[] bytes = FileUtil.readBytes(basePath + fileName);//通过文件的路径读取文件字节流
|
||||
os = response.getOutputStream();//通过response的输出流返回文件
|
||||
os.write(bytes);
|
||||
os.flush();
|
||||
os.close();
|
||||
return new ResultRespons(Constant.SUCCESS_CODE, "下载成功");
|
||||
}else{
|
||||
return new ResultRespons(Constant.ERROR_CODE, "文件名不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return new ResultRespons(Constant.ERROR_CODE, "下载失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/delete/{name}")
|
||||
public ResultRespons deleteFile(@PathVariable String name) throws IOException {
|
||||
String filePath =System.getProperty("user.dir") + "/otafiles/"+name;
|
||||
Boolean flag =FileUtil.isFile(filePath);
|
||||
if(flag){
|
||||
FileUtil.del(filePath);
|
||||
return new ResultRespons(Constant.SUCCESS_CODE, "删除成功");
|
||||
}else{
|
||||
return new ResultRespons(Constant.ERROR_CODE, "文件不存在");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue