|
@@ -0,0 +1,94 @@
|
|
|
+package com.macro.mall.tiny.modules.business.controller;
|
|
|
+
|
|
|
+import com.macro.mall.tiny.common.api.CommonResult;
|
|
|
+import com.macro.mall.tiny.config.UploadConfig;
|
|
|
+import com.macro.mall.tiny.modules.business.service.FileService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 大文件上传
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/file")
|
|
|
+@CrossOrigin
|
|
|
+@Api(tags = "FileController", description = "提供文件上传相关API")
|
|
|
+public class FileController {
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param fileUploadParam
|
|
|
+ * @return void
|
|
|
+ * @describe 文件上传
|
|
|
+ * @author gjs
|
|
|
+ * @date 2021/3/25
|
|
|
+ * @since J7.23.0
|
|
|
+ */
|
|
|
+ @ApiOperation("文件上传接口")
|
|
|
+ @PostMapping("/upload")
|
|
|
+ public void upload(String name,
|
|
|
+ Long size,
|
|
|
+ Integer chunks,
|
|
|
+ Integer chunk,
|
|
|
+ MultipartFile file) throws IOException {
|
|
|
+ if (chunks != null && chunks != 0) {
|
|
|
+ fileService.uploadWithBlock(name, size, chunks, chunk, file);
|
|
|
+ } else {
|
|
|
+ fileService.upload(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("文件下载接口")
|
|
|
+ @PostMapping("/download")
|
|
|
+ public Object downloadFile(@RequestParam String fileName, final HttpServletResponse response) {
|
|
|
+ OutputStream os = null;
|
|
|
+ InputStream is = null;
|
|
|
+ try {
|
|
|
+ // 取得输出流
|
|
|
+ os = response.getOutputStream();
|
|
|
+ // 清空输出流
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/x-download;charset=UTF-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
|
|
+// response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
|
|
+ //读取流
|
|
|
+ File f = new File(UploadConfig.path + fileName);
|
|
|
+ is = new FileInputStream(f);
|
|
|
+ //复制
|
|
|
+ IOUtils.copy(is, response.getOutputStream());
|
|
|
+ response.getOutputStream().flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ return CommonResult.failed("下载附件失败,error:" + e.getMessage());
|
|
|
+ }
|
|
|
+ //文件的关闭放在finally中
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("关闭文件输入流失败:", e);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (os != null) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("关闭文件输出流失败:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|