Springboot文件上传并在线预览

news/2024/7/10 0:17:41 标签: vue, 文件上传

服务端接口

在这里插入图片描述

@RestController
public class UploadController {
    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
    @PostMapping("/upload")
    //多文件上传 @RequestPart("files")MultipartFile[] files
    public Map<String,Object> fileupload(MultipartFile file, HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();
        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/") + format;
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        try {
       	 	//保存到文件服务器,oSS服务器
            file.transferTo(new File(folder, newName));//file.transferTo(new File("D:\\"+oldName));
            String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
            result.put("status", "OK");
            result.put("name", oldName);
            result.put("url", url);
        } catch (IOException e) {
            result.put("status", "ERROR");
            result.put("msg", e.getMessage());
        }
        return result;
    }
}

##接口测试
在这里插入图片描述

Vue前端

在这里插入图片描述

  • Vue模板
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>

<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>

</script>
</body>
</html>

参考链接
文件上传实现详细描述


Java 将 Word 文档转换为 PDF 的完美工具

使用工具:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html(免费版)
参考文章


http://www.niftyadmin.cn/n/798428.html

相关文章

vue打包后上线启动个node服务

文件prod.server.js // 依赖express ,config var express require(express); var config require(./config/index); // 当前环境变量下的port 如果没有就去config.build下找port var port process.env.PORT || config.build.port; // 启动express var app express(); // 定…

Java实现单链表的增删改查

/*** Description : 链表测试* Author : ch* Date: 2020-11-20*/ public class LinkNode {public static void main(String[] args) {LinkedList L new LinkedList();L.init();System.out.println("输入链表节点&#xff1a;");Scanner scanner new Scanner(System…

面试笔试整理3:深度学习机器学习面试问题准备(必会)

第一部分&#xff1a;深度学习 原文&#xff1a;https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum9&fps1 1、神经网络基础问题 &#xff08;1&#xff09;Backpropagation&#xff08;要能推倒&#xff09;   后向传播是在求解损失函数L对参数…

Java第三方登录-QQ登录

Java第三方登录-QQ登录

Jackson序列化LocalDate与Springboot集成

前言 Java8的date API一经推出便广受好评&#xff0c;今日也准备用一用&#xff0c;然后就用出问题了。基本用法见https://www.cnblogs.com/woshimrf/p/java8-date-api.html 问题 LocalDate可以很友好的toString为YYYY-MM-dd的格式&#xff0c;很适合我当前的业务&#xff0c;但…

SpringBoot整合二维码生成

spring-boot-qrcode 介绍 SpringBoot整合二维码,使用Google提供的二维码依赖架包.实现二维码文件的生成和前端的显示 安装教程 导入依赖 <!--导入二维码依赖--><!-- https://mvnrepository.com/artifact/com.google.zxing/core --><dependency><group…

游戏场景下的DDoS风险分析及防护

欢迎大家前往腾讯云社区&#xff0c;获取更多腾讯海量技术实践干货哦~ 作者&#xff1a;腾讯游戏云资深架构师 vince 本篇文章主要是分享游戏业务面临的安全风险场景&#xff0c;以及基于这些场景的特点&#xff0c;我们应该如何做好对应的防护。 背景&#xff1a;游戏行业DDoS…

数据库表结构文档生成工具screw

数据库设计文档生成工具https://gitee.com/leshalv/screw