文件上传,富文本编辑器的后台操作

这里是使用的是wangeditor编辑器 编辑器地址

package com.chen.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.chen.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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("/files")
@Api(value = "FileController", tags = "文件上传")
@SuppressWarnings("all")
public class FileController {

    @Value("8081")
    private String port;

    private static final String ip="http://localhost";


    @PostMapping("/upload")
    @ApiOperation(("文件上传"))
    public Result<?> upload(MultipartFile file) throws IOException {   //接受前台传过来的对象
        String flag = IdUtil.fastSimpleUUID();//定义文件唯一标识
        String originalFilename = file.getOriginalFilename();  //获取文件名称
        String rootFilePath = System.getProperty("user.dir") + "/src/main/resources/files/" + flag + originalFilename; //获取building-service文件夹路径+文件路径
        //将前台传过来的file对象写入到(磁盘)文件夹中
        FileUtil.writeBytes(file.getBytes(), rootFilePath);
        return Result.success(ip+":"+port+ "/files/" + flag);
    }


    //富文本
    @PostMapping("/editor/load")
    public JSON editorUpload(MultipartFile file) throws IOException {  //接受前台的对象
        //定义文件唯一标识
        String flag = IdUtil.fastSimpleUUID();

        String originalFilename = file.getOriginalFilename();//获取文件名称
        String rootFilePath = System.getProperty("user.dir") + "/src/main/resources/files/" + flag + "_" + originalFilename;//获取文件路径
        FileUtil.writeBytes(file.getBytes(), rootFilePath); //使用hutool工具实现文件的写入
        String url = ip + ":" + port + "/files/" + flag;
        JSONObject json = new JSONObject();
        json.set("errno", 0);
        JSONArray arr = new JSONArray();
        JSONObject data = new JSONObject();
        arr.add(data);
        data.set("url", url);
        json.set("data", arr);
        return json;
    }


    @GetMapping("/{flag}")
    @ApiOperation("文件下载")
    public void getFiles(@PathVariable String flag, HttpServletResponse response) {
        OutputStream os;  // 新建一个输出流对象
        String basePath = System.getProperty("user.dir") + "/src/main/resources/files/";  // 定于文件上传的根路径
        List<String> fileNames = FileUtil.listFileNames(basePath);  // 获取所有的文件名称
        String fileName = fileNames.stream().filter(name -> name.contains(flag)).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();   // 通过输出流返回文件
                os.write(bytes);
                os.flush();
                os.close();
            }
        } catch (Exception e) {
            System.out.println("文件下载失败");
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值