Browse Source

实现事件附件上传下载

林健华 2 years ago
parent
commit
e5759b4632

+ 28 - 0
bin/start.sh

@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+APP_JAR_NAME="${project.build.finalName}.jar"
+JVM_ARGS="-server -Xms1024m -Xmn512m"
+
+check(){
+    PID=`jps -l | grep ${APP_JAR_NAME} | awk '{print $1}'`
+    if [ -n "$PID" ];then
+        echo ${APP_JAR_NAME} has been started on pid:${PID}
+        return 1
+    else
+        return 0
+    fi
+}
+check
+IS_RUN=$?
+if [ ${IS_RUN} -eq 0 ];then
+    echo ------------------------------
+    echo ${APP_JAR_NAME} starting....
+    echo ------------------------------
+    cd ../
+    nohup java ${JVM_ARGS} -jar ${APP_JAR_NAME} > /dev/null 2>&1 &
+    check
+    IS_RUN=$?
+    if [ ${IS_RUN} -eq 0 ];then
+        echo ${APP_JAR_NAME} start failure...
+    fi
+fi
+

+ 12 - 0
bin/stop.sh

@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+APP_JAR_NAME="${project.build.finalName}.jar"
+PID=`jps -l | grep ${APP_JAR_NAME} | awk '{print $1}'`
+
+if [ -n "$PID" ];then
+    echo --------------------------
+    echo stop ${APP_JAR_NAME}
+    echo --------------------------
+    kill ${PID}
+    echo ${APP_JAR_NAME} has been stoped...
+fi
+

+ 0 - 8
pom.xml

@@ -61,14 +61,6 @@
             <artifactId>fastjson</artifactId>
             <version>1.2.28</version>
         </dependency>
-
-        <!-- Sa-Token 权限认证,在线文档:https://sa-token.cc -->
-        <dependency>
-            <groupId>cn.dev33</groupId>
-            <artifactId>sa-token-spring-boot-starter</artifactId>
-            <version>1.34.0</version>
-        </dependency>
-
     </dependencies>
 
     <build>

+ 26 - 31
src/main/java/com/benyun/config/CorsConfig.java

@@ -8,38 +8,33 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 import org.springframework.web.filter.CorsFilter;
 
 /**
-  * 跨域前后端配置类
-  * 赵毅梵
-  * 2021/9/8
-  **/
+ * 跨域前后端配置类
+ * 赵毅梵
+ * 2021/9/8
+ **/
 @Configuration
 @Slf4j
 public class CorsConfig {
-  @Bean
-  public CorsFilter corsFilter() {
-    // 1.添加CORS配置信息
-    CorsConfiguration config = new CorsConfiguration();
-    // 放行哪些原始域
-    config.addAllowedOrigin("*");
-    // 是否发送Cookie信息
-    // 若要传输Cookie,则要求固定原始域,不能使用*
-    config.setAllowCredentials(false);
-    // 放行哪些原始域(请求方式)
-    config.addAllowedMethod("*");
-    // 放行哪些原始域(头部信息)
-    config.addAllowedHeader("*");
-    // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
-    // config.addExposedHeader("*");
-    config.addExposedHeader("Content-Type");
-    config.addExposedHeader( "X-Requested-With");
-    config.addExposedHeader("accept");
-    config.addExposedHeader("Origin");
-    config.addExposedHeader( "Access-Control-Request-Method");
-    config.addExposedHeader("Access-Control-Request-Headers");
-    // 2.添加映射路径
-    UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
-    configSource.registerCorsConfiguration("/**", config);
-    // 3.返回新的CorsFilter.
-    return new CorsFilter(configSource);
-  }
+    @Bean
+    public CorsFilter corsFilter() {
+        // 1.添加CORS配置信息
+        CorsConfiguration config = new CorsConfiguration();
+        // 放行哪些原始域
+        config.addAllowedOrigin("*");
+        // 是否发送Cookie信息
+        // 若要传输Cookie,则要求固定原始域,不能使用*
+        config.setAllowCredentials(false);
+        // 放行哪些原始域(请求方式)
+        config.addAllowedMethod("*");
+        // 放行哪些原始域(头部信息)
+        config.addAllowedHeader("*");
+        // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
+        // config.addExposedHeader("*");
+        config.addExposedHeader("*");
+        // 2.添加映射路径
+        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
+        configSource.registerCorsConfiguration("/**", config);
+        // 3.返回新的CorsFilter.
+        return new CorsFilter(configSource);
+    }
 }

+ 140 - 0
src/main/java/com/benyun/controller/EventAppendixController.java

@@ -0,0 +1,140 @@
+package com.benyun.controller;
+
+
+import com.benyun.boot.core.vo.Result;
+import com.benyun.entity.EventAppendix;
+import com.benyun.service.EventAppendixService;
+import lombok.RequiredArgsConstructor;
+import org.beetl.sql.core.engine.PageQuery;
+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.IOException;
+import java.net.URLEncoder;
+import java.util.List;
+
+/**
+ * @author makejava
+ * create on 2023-03-27 08:55:05
+ */
+@RestController
+@RequestMapping("eventAppendix")
+@RequiredArgsConstructor(onConstructor_ = {@Autowired})
+public class EventAppendixController {
+    /**
+     * 服务对象
+     */
+    private final EventAppendixService eventAppendixService;
+
+    /**
+     * 查询所有数据
+     *
+     * @return 所有数据
+     */
+    @GetMapping("/queryAll")
+    public Result<List<EventAppendix>> queryAll() {
+        return Result.success(eventAppendixService.queryAll());
+    }
+
+    /**
+     * 分页查询数据
+     *
+     * @return 所有数据
+     */
+    @GetMapping("/queryPage")
+    public Result<List<EventAppendix>> queryPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, EventAppendix eventAppendix) {
+        PageQuery<EventAppendix> pageQuery = new PageQuery<EventAppendix>();
+        pageQuery.setPageSize(pageSize);
+        pageQuery.setPageNumber(pageNum);
+        pageQuery.setParas(eventAppendix);
+        return eventAppendixService.queryByPage(pageQuery);
+    }
+
+    /**
+     * 通过主键查询单条数据
+     *
+     * @param id 主键
+     * @return 单条数据
+     */
+    @GetMapping("/queryOne")
+    public Result<EventAppendix> queryOne(@RequestParam("id") Integer id) {
+        return Result.success(eventAppendixService.queryById(id));
+    }
+
+    /**
+     * 通过eventId查询数据
+     *
+     * @param eventId 事件ID
+     * @return 单条数据
+     */
+    @GetMapping("/queryListByEventId")
+    public Result<List<EventAppendix>> queryListByEventId(@RequestParam("eventId") String eventId) {
+        return Result.success(eventAppendixService.queryListByEventId(eventId));
+    }
+
+    /**
+     * 新增数据
+     *
+     * @param eventAppendix 实体对象
+     * @return 新增结果
+     */
+    @PostMapping("/create")
+    public Result<?> create(@RequestBody EventAppendix eventAppendix) {
+        eventAppendixService.insert(eventAppendix);
+        return Result.success(eventAppendix);
+    }
+
+    /**
+     * 上传附件
+     *
+     * @return 上传结果
+     */
+    @PostMapping
+    public Result<Boolean> upload(@RequestPart("file") MultipartFile file, @RequestParam("id") String id) {
+        return Result.success(eventAppendixService.saveAppendix(file, id));
+    }
+
+    /**
+     * 下载附件
+     *
+     */
+    @GetMapping("/download")
+    public void download(HttpServletResponse response, @RequestParam("id") Integer id) throws IOException {
+        EventAppendix eventAppendix = eventAppendixService.queryById(id);
+        response.setContentType("application/octet-stream");
+        response.setCharacterEncoding("utf-8");
+        response.addHeader("Content-disposition", "attachment;");
+        byte[] bytes = eventAppendixService.simpleAppendix(response, eventAppendix);
+        response.getOutputStream().write(bytes);
+        response.getOutputStream().close();
+    }
+
+    /**
+     * 修改数据
+     *
+     * @param eventAppendix 实体对象
+     * @return 修改结果
+     */
+    @PostMapping("/update")
+    public Result<?> update(@RequestBody EventAppendix eventAppendix) {
+        return eventAppendixService.updateById(eventAppendix) ? Result.success() : Result.failure();
+    }
+
+    /**
+     * 删除数据
+     *
+     * @param id 主键
+     * @return 删除结果
+     */
+    @PostMapping("/delete")
+    public Result<Boolean> delete(@RequestParam("id") Integer id) {
+        EventAppendix eventAppendix = eventAppendixService.queryById(id);
+        if (eventAppendixService.deleteFile(eventAppendix))
+            return Result.success(this.eventAppendixService.deleteById(id));
+        else return Result.success(false);
+    }
+
+
+}

+ 22 - 0
src/main/java/com/benyun/dao/EventAppendixDao.java

@@ -0,0 +1,22 @@
+package com.benyun.dao;
+
+import com.benyun.entity.EventAppendix;
+import org.beetl.sql.core.mapper.BaseMapper;
+import org.beetl.sql.core.engine.PageQuery;
+import org.beetl.sql.core.annotatoin.SqlResource;
+import org.springframework.stereotype.Repository;
+
+/**
+ * 事件附件表(EventAppendix)表数据库访问层
+ *
+ * @author makejava
+ * create on 2023-03-27 08:55:05
+ */
+@Repository
+@SqlResource("EventAppendix")
+public interface EventAppendixDao extends BaseMapper<EventAppendix> {
+    /**
+     * 分页查询
+     */
+    void queryByPage(PageQuery<EventAppendix> pageQuery);
+}

+ 39 - 0
src/main/java/com/benyun/entity/EventAppendix.java

@@ -0,0 +1,39 @@
+package com.benyun.entity;
+
+
+import lombok.*;
+import org.beetl.sql.core.TailBean;
+import org.beetl.sql.core.annotatoin.Table;
+import org.beetl.sql.core.annotatoin.AutoID;
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ * 事件附件表(EventAppendix)实体类
+ *
+ * @author makejava
+ * @since 2023-03-27 08:55:05
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@EqualsAndHashCode(callSuper = false)
+@Table(name = "event_appendix")
+public class EventAppendix extends TailBean {
+    private static final long serialVersionUID = -35753849133582681L;
+    /**
+     * 表ID
+     */
+    @AutoID
+    private Integer id;
+    /**
+     * 事件ID
+     */
+    private String eventId;
+    /**
+     * 附件名称
+     */
+    private String name;
+
+}

+ 58 - 0
src/main/java/com/benyun/service/EventAppendixService.java

@@ -0,0 +1,58 @@
+package com.benyun.service;
+
+
+import java.io.IOException;
+import java.util.List;
+
+import com.benyun.entity.EventAppendix;
+import com.benyun.boot.core.vo.Result;
+import org.beetl.sql.core.engine.PageQuery;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * 事件附件表(EventAppendix)表服务
+ *
+ * @author makejava
+ * create on 2023-03-27 08:55:05
+ */
+public interface EventAppendixService {
+
+    EventAppendix queryById(Integer id);
+
+    List<EventAppendix> queryAll();
+
+    void insert(EventAppendix eventAppendix);
+    
+    /**
+     * 根据主键更新记录
+     *
+     * @param eventAppendix 待更新的记录.包含主键在内,如果其他列的值为空则不更新
+     * @return 成功返回true,失败返回false
+     */
+    boolean updateById(EventAppendix eventAppendix);
+    
+    /**
+     * 根据主键删除记录
+     *
+     * @param id 主键
+     * @return 成功返回true,反之返回false
+     */
+    boolean deleteById(Integer id);
+    
+    /**
+     * 分页查询
+     * @param pageQuery 封装分页条件的分页对象
+     * @return pageQuery 作为一个引用,将查询结果封装进去
+     */
+    Result<List<EventAppendix>> queryByPage(PageQuery<EventAppendix> pageQuery);
+
+    boolean saveAppendix(MultipartFile file, String id);
+
+    List<EventAppendix> queryListByEventId(String eventId);
+
+    byte[] simpleAppendix(HttpServletResponse response, EventAppendix eventAppendix) throws IOException;
+
+    boolean deleteFile(EventAppendix eventAppendix);
+}

+ 137 - 0
src/main/java/com/benyun/service/impl/EventAppendixServiceImpl.java

@@ -0,0 +1,137 @@
+package com.benyun.service.impl;
+
+
+import cn.hutool.core.io.FileUtil;
+import com.benyun.boot.core.vo.Result;
+import com.benyun.dao.EventAppendixDao;
+import com.benyun.entity.EventAppendix;
+import com.benyun.service.EventAppendixService;
+import lombok.RequiredArgsConstructor;
+import org.beetl.sql.core.engine.PageQuery;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * 事件附件表(EventAppendix)表服务实现
+ *
+ * @author makejava
+ * create on 2023-03-27 08:55:05
+ */
+@Service
+@RequiredArgsConstructor(onConstructor_ = {@Autowired})
+public class EventAppendixServiceImpl implements EventAppendixService {
+
+    private final EventAppendixDao eventAppendixDao;
+    /**
+     * 附件目录前缀
+     */
+    @Value("${event.appendix.prefixPath}")
+    private String prefixPath;
+
+    @Override
+    public EventAppendix queryById(Integer id) {
+        return eventAppendixDao.single(id);
+    }
+
+    @Override
+    public List<EventAppendix> queryAll() {
+        return eventAppendixDao.all();
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void insert(EventAppendix eventAppendix) {
+        eventAppendixDao.insert(eventAppendix, true);
+    }
+
+    /**
+     * 根据主键更新记录
+     *
+     * @param eventAppendix 待更新的记录.包含主键在内,如果其他列的值为空则不更新
+     * @return 成功返回true,失败返回false
+     */
+    @Override
+    public boolean updateById(EventAppendix eventAppendix) {
+        return eventAppendixDao.updateTemplateById(eventAppendix) > 0;
+    }
+
+    /**
+     * 根据主键删除记录
+     *
+     * @param id 主键
+     * @return 成功返回true,反之返回false
+     */
+    @Override
+    public boolean deleteById(Integer id) {
+        return eventAppendixDao.deleteById(id) > 0;
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param pageQuery 封装分页条件的分页对象
+     * @return pageQuery 作为一个引用,将查询结果封装进去
+     */
+    @Override
+    public Result<List<EventAppendix>> queryByPage(PageQuery<EventAppendix> pageQuery) {
+        eventAppendixDao.queryByPage(pageQuery);
+        Result<List<EventAppendix>> result = Result.success();
+        result.setData(pageQuery.getList());
+        result.setCount((int) pageQuery.getTotalRow());
+        return result;
+    }
+
+    @Override
+    public boolean saveAppendix(MultipartFile file, String id) {
+        String filename = file.getOriginalFilename();
+        try {
+            // 创建File对象,自动识别相对或绝对路径,相对路径将自动从ClassPath下寻找
+            File fileNew = FileUtil.file(prefixPath + "/" + id + "/" + filename);
+            if (!FileUtil.exist(fileNew)) {
+                // 创建文件
+                FileUtil.touch(fileNew);
+
+                EventAppendix eventAppendix = new EventAppendix();
+                eventAppendix.setEventId(id);
+                eventAppendix.setName(filename);
+                eventAppendixDao.insert(eventAppendix, true);
+            }
+            // 写入数据
+            FileUtil.writeBytes(file.getBytes(), fileNew);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+    @Override
+    public byte[] simpleAppendix(HttpServletResponse response, EventAppendix eventAppendix) {
+        File fileNew = FileUtil.file(prefixPath + "/" + eventAppendix.getEventId() + "/" + eventAppendix.getName());
+        if (FileUtil.exist(fileNew)) {
+            return FileUtil.readBytes(fileNew);
+        }
+        return null;
+    }
+
+    @Override
+    public List<EventAppendix> queryListByEventId(String eventId) {
+        return eventAppendixDao.createLambdaQuery().andEq(EventAppendix::getEventId, eventId).select();
+    }
+
+    @Override
+    public boolean deleteFile(EventAppendix eventAppendix) {
+        File fileNew = FileUtil.file(prefixPath + "/" + eventAppendix.getEventId() + "/" + eventAppendix.getName());
+        if (FileUtil.exist(fileNew)) {
+            return FileUtil.del(fileNew);
+        }
+        return false;
+    }
+}

+ 5 - 0
src/main/resources/application-dev.yml

@@ -31,6 +31,7 @@ server:
     mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
 
 spring:
+  # 上传文件大小限制 默认1m
   servlet:
     multipart:
       max-file-size: 10MB
@@ -67,3 +68,7 @@ knife4j:
     username: admin
     password: benyun
 
+event:
+  appendix:
+    # prefixPath: ../appendix/
+    prefixPath: E:\Git47\appendix

+ 8 - 0
src/main/resources/sql/EventAppendix.md

@@ -0,0 +1,8 @@
+queryByPage
+===
+SELECT 
+@pageTag(){
+ * 
+@}
+FROM event_appendix 
+WHERE 1=1