package hongbao.web.controller;
import hongbao.autoconfig.F4jProperties;
import hongbao.autoconfig.FileProperties;
import hongbao.common.FileTypeEnum;
import hongbao.controller.BaseController;
import hongbao.entity.HongBaoFile;
import hongbao.file.FileStore;
import hongbao.model.JsonResult;
import hongbao.model.ResultUtil;
import hongbao.model.VoiceStoreDto;
import hongbao.service.FileServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {
@Autowired
private FileServiceI fileService;
@RequestMapping(value = "/uploadPicFile", method = {RequestMethod.POST})
public JsonResult filePicUpload(@RequestParam(value = "file") MultipartFile file) throws IOException {
String path= "文件存储路径";
String fileName=file.getOriginalFilename();
if (!file.isEmpty()) {
try {
File fileDir = new File(path);
if (!fileDir.exists()) {// 判断目录是否存在
fileDir.mkdirs();
//dir.mkdirs(); //多层目录需要调用mkdirs
}
byte[] fdate = file.getBytes();
FileOutputStream out = new FileOutputStream(path+fileName);
out.write(fdate);
out.close();
//保存文件
/*
这里写文件业务逻辑
*/
return ResultUtil.success(bFile);
} catch (IOException e) {
e.printStackTrace();
return ResultUtil.error(410, "操作失败");
}
}else {
return ResultUtil.error(410, "文件不能为空");
}
}
@RequestMapping(value = "/fileDownload", method = RequestMethod.GET)
public void fileDownload(@RequestParam(value = "fileId") String fileId,HttpServletResponse res) {
//获取文件
HongBaoFile bFile = fileService.selectById(fileId);
res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename=" + bFile.getFileName());
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(bFile.getFilePath())));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
sihuitiankong
- 粉丝: 1
- 资源: 2
最新资源
- 使用特定版本的 Python 设置 GitHub Actions 工作流程.zip
- 汽车电子中MICROSAR OBD协议栈解决方案及其应用
- 基于Kotlin,AndroidX的仿微信图片选择器
- 平台重构与技术进化:汽车生态系统的未来发展
- HarmonyOs DevEco Studio小技巧34-鸿蒙应用架构设计 MVVM模式与三层架构
- pycharm为基础的人工智能课程设计
- 使用 Python 进行基础操作和高级操作 .zip
- 使用 Python 编写的 livereload 服务器.zip
- 终端(自己造的终端,亲测,和原版终端一模一样,连python命令也能使用)
- 使用 Python 类型提示进行数据验证.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈