姜钧瀚第3次提交 文件上传下载

master
小萌新 5 months ago
parent d341d6f2fe
commit 5e37be8891
  1. 33
      pom.xml
  2. 56
      src/main/java/com/teaching/backend/controller/CommonController.java
  3. 53
      src/main/java/com/teaching/backend/controller/FavourInformation/FavourInformationController.java
  4. 132
      src/main/java/com/teaching/backend/controller/WordController2.java
  5. 4
      src/main/java/com/teaching/backend/mapper/CourseResourcesMapper.java
  6. 19
      src/main/java/com/teaching/backend/mapper/ResourcesRelationshipMapper.java
  7. 18
      src/main/java/com/teaching/backend/mapper/UserMapper.java
  8. 16
      src/main/java/com/teaching/backend/model/entity/CourseResources.java
  9. 17
      src/main/java/com/teaching/backend/model/entity/ResourceRelationship.java
  10. 27
      src/main/java/com/teaching/backend/service/CourseResourcesService.java
  11. 23
      src/main/java/com/teaching/backend/service/FileTableService.java
  12. 17
      src/main/java/com/teaching/backend/service/ResourcesRelationshipService.java
  13. 16
      src/main/java/com/teaching/backend/service/UserService.java
  14. 5
      src/main/java/com/teaching/backend/service/favour/SeResourceFavourService.java
  15. 121
      src/main/java/com/teaching/backend/service/impl/CourseResourcesServiceImpl.java
  16. 97
      src/main/java/com/teaching/backend/service/impl/FileTableServiceImpl.java
  17. 21
      src/main/java/com/teaching/backend/service/impl/ResourcesRelationshipServiceImpl.java
  18. 24
      src/main/java/com/teaching/backend/service/impl/UserServiceImpl.java
  19. 6
      src/main/java/com/teaching/backend/service/impl/favour/SeResourceFavourServiceImpl.java
  20. 63
      src/main/java/com/teaching/backend/utils/WordUtil.java
  21. 0
      src/main/java/com/teaching/backend/utils/textceshi.java
  22. 2
      src/main/resources/application.properties
  23. 23
      src/main/resources/application.yml
  24. 34
      src/main/resources/static/hello2.html
  25. BIN
      src/main/resources/word_template/ceshi.docx
  26. BIN
      src/main/resources/word_template/ceshi2.docx
  27. BIN
      src/main/resources/word_template/ceshi3.docx
  28. 5
      src/test/java/com/teaching/TeachingBackendApplicationTests.java
  29. 3
      template.ftl

@ -57,6 +57,28 @@
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
@ -177,6 +199,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>

@ -2,19 +2,19 @@ package com.teaching.backend.controller;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.model.entity.FileTable;
import com.teaching.backend.service.FileTableService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import com.teaching.backend.model.entity.CourseResources;
import com.teaching.backend.service.CourseResourcesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -22,9 +22,8 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.nio.file.Paths;
import java.util.Date;
@ -44,7 +43,7 @@ public class CommonController {
@Resource
private FileTableService fileTableService;
private CourseResourcesService courseResourcesService;
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@ -60,21 +59,30 @@ public class CommonController {
@PostMapping("/upload")
public BaseResponse<String> upload(@RequestParam("courseId") String courseId, MultipartFile file) throws IOException {
public BaseResponse<String> upload(MultipartFile file) throws IOException {
// if (file.getSize() > (10)) {
// return ResultUtils.error(690,"内容太大啦!");
// }
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
Integer Filetype;
String subDir;
if (suffix.equalsIgnoreCase(".jpg") || suffix.equalsIgnoreCase(".jpeg") || suffix.equalsIgnoreCase(".png")) {
subDir = "img/";
Filetype=1 ;
} else if (suffix.equalsIgnoreCase(".mp4") || suffix.equalsIgnoreCase(".avi") || suffix.equalsIgnoreCase(".mov")) {
subDir = "video/";
} else {
Filetype=2 ;
} else if (suffix.equalsIgnoreCase(".mp3") || suffix.equalsIgnoreCase(".wav") || suffix.equalsIgnoreCase(".flac")) {
subDir = "video/";
Filetype = 3;
}else {
subDir = "others/";
Filetype=4;
}
@ -84,9 +92,11 @@ public class CommonController {
ossClient.putObject(bucketName, subDir + fileName, file.getInputStream());
String objectUrl = ossClient.generatePresignedUrl(bucketName, subDir + fileName, new Date(System.currentTimeMillis() + 3600 * 1000)).toString();
// String objectUrl = ossClient.generatePresignedUrl(bucketName, subDir + fileName, new Date(System.currentTimeMillis() + 3600 * 1000)).toString();
fileTableService.updateFile(courseId, fileName, objectUrl); // 更新文件路径为OSS生成的URL
String objectUrl = "https://" + bucketName + "." + endpoint + "/" + subDir + fileName;
Integer type=Filetype;
courseResourcesService.updateFile(type, fileName, objectUrl); // 更新文件路径为OSS生成的URL
ossClient.shutdown();
@ -97,23 +107,25 @@ public class CommonController {
//文件下载
@GetMapping("/download")
public BaseResponse<String> download(@RequestParam String courseId, @RequestParam String path, HttpServletResponse response) throws IOException {
public BaseResponse<String> download(@RequestParam String Id, @RequestParam String path, HttpServletResponse response) throws IOException {
System.out.println("执行了下载的方法");
fileTableService.download(courseId,path,response,endpoint,accessKeyId,accessKeySecret,bucketName);
courseResourcesService.download(Id,path,response,endpoint,accessKeyId,accessKeySecret,bucketName);
return ResultUtils.success("成功");
}
@GetMapping("/get/record")
public BaseResponse<Page<FileTable>> pageBaseResponse(int page,int pageSize){
public BaseResponse<Page<CourseResources>> pageBaseResponse(@RequestParam Integer type, Integer page,Integer pageSize){
Page<FileTable> pageInfo=new Page<>(page,pageSize);
Page<CourseResources> pageInfo=new Page<>(page,pageSize);
LambdaQueryWrapper<FileTable> lambdaQueryWrapper=new LambdaQueryWrapper<>();
LambdaQueryWrapper<CourseResources> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(type!=null,CourseResources::getType,type);
fileTableService.page(pageInfo, lambdaQueryWrapper);
courseResourcesService.page(pageInfo, lambdaQueryWrapper);
return ResultUtils.success(pageInfo);

@ -0,0 +1,53 @@
package com.teaching.backend.controller.FavourInformation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.PageRequest;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.model.dto.favour.resourceFavour.ResourceFavourAddRequest;
import com.teaching.backend.model.entity.CourseResources;
import com.teaching.backend.model.entity.favour.SeCourseFavour;
import com.teaching.backend.model.entity.favour.SeResourceFavour;
import com.teaching.backend.service.CourseResourcesService;
import com.teaching.backend.service.favour.SeResourceFavourService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* ClassName: Get
* Package: com.teaching.backend.controller.FavourInformation
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/13 18:15
* @Version 1.0
*/
@RestController
public class FavourInformationController{
@Resource
CourseResourcesService courseResourcesService;
@GetMapping("/Favour/get")
public BaseResponse<Page<CourseResources>> getInformation2(@RequestParam List<Integer> resourceIdList , @RequestParam Integer type, PageRequest pageRequest) {
int page=pageRequest.getCurrent();
int pageSize=pageRequest.getPageSize();
Page<CourseResources> resourcesPage=courseResourcesService.getresourceRecords(type,resourceIdList,page,pageSize);
return ResultUtils.success(resourcesPage);
}
}

@ -0,0 +1,132 @@
package com.teaching.backend.controller;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.teaching.backend.utils.WordUtil;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
@RestController
public class WordController2 {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.bucketName}")
private String bucketName;
@Value("${filename}")
private String templateFileName; // 模板文件名
@PostMapping("/Word/get")
public ResponseEntity<byte[]> createWord(@RequestParam("outputPath") String outputPath) {
String filename = "generatedWord.docx";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
InputStream templateStream = null;
//获取模版
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, templateFileName));
templateStream = ossObject.getObjectContent();
WordUtil wordUtil = new WordUtil();
wordUtil.setTemplateInputStream(templateStream); // 设置模板文件输入流
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("username1", "姜钧瀚");
dataMap.put("img", "src/main/resources/static/3823.png");
// 设置 Word 生成的输出目录
wordUtil.setOutputDir(outputPath);
String generatedFilePath = wordUtil.createWord(dataMap);
if (generatedFilePath.equals("操作失败")) {
System.out.println("操作失败");
return ResponseEntity.badRequest().body("操作失败".getBytes());
}
File file = new File(generatedFilePath);
byte[] fileContent;
try {
fileContent = Files.readAllBytes(file.toPath());
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.badRequest().body("文件读取失败".getBytes());
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", filename);
//返回文件内容
return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
}
@PostMapping("/Word/get2")
private void downWord(HttpServletResponse response) throws IOException {
// 读取模板文件并创建XWPFDocument对象
File rootFile = new File(ResourceUtils.getURL("classpath:").getPath());
File templateFile = new File(rootFile, "/word_template/ceshi2.docx");
try (FileInputStream fileInputStream = new FileInputStream(templateFile)) {
XWPFDocument word = new XWPFDocument(fileInputStream);
// 填充数据等后续处理代码
Map<String, String> params = new HashMap<>();
params.put("username", "姜钧瀚");
//处理正文
List<XWPFParagraph> paragraphs = word.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
for (String key : params.keySet()) {
if (text.contains(key)) {
run.setText(text.replaceAll(key, params.get(key)), 0);
}
}
}
}
// 导出Word
String fileName = "123123123.docx";
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
word.write(response.getOutputStream());
}
}
}

@ -1,7 +1,7 @@
package com.teaching.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.FileTable;
import com.teaching.backend.model.entity.CourseResources;
import org.apache.ibatis.annotations.Mapper;
/**
@ -14,5 +14,5 @@ import org.apache.ibatis.annotations.Mapper;
* @Version 1.0
*/
@Mapper
public interface FileTableMapper extends BaseMapper<FileTable> {
public interface CourseResourcesMapper extends BaseMapper<CourseResources> {
}

@ -0,0 +1,19 @@
package com.teaching.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.ResourceRelationship;
import org.apache.ibatis.annotations.Mapper;
import org.apache.tomcat.websocket.BackgroundProcess;
/**
* ClassName: ResourcesRelationshipMapper
* Package: com.teaching.backend.mapper
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/12 21:07
* @Version 1.0
*/
@Mapper
public interface ResourcesRelationshipMapper extends BaseMapper<ResourceRelationship> {
}

@ -1,18 +0,0 @@
package com.teaching.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* ClassName: UserMapper
* Package: com.teaching.backend.mapper
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/3 17:47
* @Version 1.0
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

@ -1,5 +1,7 @@
package com.teaching.backend.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -21,12 +23,16 @@ import java.util.stream.Stream;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("filetable")
public class FileTable {
@TableName("Resource")
public class CourseResources {
@TableId(value = "id", type = IdType.AUTO)
private int id;
private String courseId;
private String fileName;
private String filePath;
private String name;
private int type;
private int status;
private String tags;
private String path;
}

@ -6,21 +6,24 @@ import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* ClassName: User
* ClassName: ResourcesRelationship
* Package: com.teaching.backend.model.entity
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/3 17:42
* @Create 2024/6/12 21:03
* @Version 1.0
*/
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private int userid;
private int bokeid;
private int type;
public class ResourceRelationship {
private String Id;
private String courseid;
private String chapterid;
private String knowid;
private String resourceid;
}

@ -0,0 +1,27 @@
package com.teaching.backend.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.entity.CourseResources;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* ClassName: FileTableService
* Package: com.teaching.backend.service
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/4 11:49
* @Version 1.0
*/
public interface CourseResourcesService extends IService<CourseResources> {
void updateFile(Integer type, String fileName, String filePath);
void download(String id, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException;
Page<CourseResources> getresourceRecords(Integer type, List<Integer> resourceIdList, int page, int pageSize);
}

@ -1,23 +0,0 @@
package com.teaching.backend.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.entity.FileTable;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ClassName: FileTableService
* Package: com.teaching.backend.service
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/4 11:49
* @Version 1.0
*/
public interface FileTableService extends IService<FileTable> {
void updateFile(String courseId, String fileName, String filePath);
void download(String courseId, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException;
}

@ -0,0 +1,17 @@
package com.teaching.backend.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.entity.ResourceRelationship;
/**
* ClassName: ResourcesRelationshipService
* Package: com.teaching.backend.service
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/12 21:04
* @Version 1.0
*/
public interface ResourcesRelationshipService extends IService<ResourceRelationship> {
}

@ -1,16 +0,0 @@
package com.teaching.backend.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.entity.User;
/**
* ClassName: UserService
* Package: com.teaching.backend.service
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/3 17:48
* @Version 1.0
*/
public interface UserService extends IService<User> {
}

@ -1,8 +1,11 @@
package com.teaching.backend.service.favour;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.dto.favour.resourceFavour.ResourceFavourAddRequest;
import com.teaching.backend.model.entity.favour.SeResourceFavour;
import java.util.List;
/**
* @Author:youhang
* @Date:2024-05-30-20:22
@ -19,4 +22,6 @@ public interface SeResourceFavourService extends IService<SeResourceFavour> {
*/
boolean doResourceFavour(String courseId, String resourceId, String userId);
}

@ -0,0 +1,121 @@
package com.teaching.backend.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.mapper.CourseResourcesMapper;
import com.teaching.backend.model.entity.CourseResources;
import com.teaching.backend.model.entity.ResourceRelationship;
import com.teaching.backend.service.CourseResourcesService;
import com.teaching.backend.service.ResourcesRelationshipService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* ClassName: FileTableImpl
* Package: com.teaching.backend.service.impl
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/4 11:50
* @Version 1.0
*/
@Service
public class CourseResourcesServiceImpl extends ServiceImpl<CourseResourcesMapper, CourseResources> implements CourseResourcesService {
@Resource
ResourcesRelationshipService resourcesRelationshipService;
@Override
public void updateFile(Integer type, String fileName, String filePath) {
CourseResources courseResources = new CourseResources();
courseResources.setName(fileName);
courseResources.setPath(filePath);
courseResources.setType(type);
this.save(courseResources);
int resourceId=courseResources.getId();
System.out.println(resourceId);
ResourceRelationship resourcesRelationship=new ResourceRelationship();
resourcesRelationship.setResourceid(String.valueOf(resourceId));
resourcesRelationshipService.save(resourcesRelationship);
}
@Override
public void download(String Id, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException {
CourseResources courseResources=getById(Id);
System.out.println(courseResources);
// 创建OSS客户端
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String key = extractKeyFromFullPath(courseResources.getPath());
OSSObject object = ossClient.getObject(bucketName, key);
String fileName = courseResources.getName();
// 设置保存文件的路径
String savePath = path + "/" + fileName;
// 获取文件的输入流
InputStream inputStream = object.getObjectContent();
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 创建保存文件
File saveFile = new File(savePath);
// 将文件内容复制到保存文件中
FileUtils.copyInputStreamToFile(inputStream, saveFile);
// 关闭输入流和OSS客户端
inputStream.close();
ossClient.shutdown();
}
@Override
public Page<CourseResources> getresourceRecords(Integer type, List<Integer> resourceIdList, int page, int pageSize) {
Page <CourseResources> pageInfo=new Page<>(page,pageSize);
LambdaQueryWrapper<CourseResources>lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(CourseResources::getId,resourceIdList);
lambdaQueryWrapper.eq(CourseResources::getType,type);
return this.page(pageInfo,lambdaQueryWrapper);
}
private String extractKeyFromFullPath(String filePath) {
// 提取存储桶名称
String key = filePath.substring(filePath.indexOf("com") + 4, filePath.indexOf("?"));
return key;
}
}

@ -1,97 +0,0 @@
package com.teaching.backend.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.mapper.FileTableMapper;
import com.teaching.backend.model.entity.FileTable;
import com.teaching.backend.service.FileTableService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* ClassName: FileTableImpl
* Package: com.teaching.backend.service.impl
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/4 11:50
* @Version 1.0
*/
@Service
public class FileTableServiceImpl extends ServiceImpl<FileTableMapper,FileTable> implements FileTableService {
@Override
public void updateFile(String courseId, String fileName, String filePath) {
System.out.println("执行了service中的方法");
System.out.println(courseId);
System.out.println(fileName);
System.out.println(filePath);
FileTable fileTable = new FileTable();
fileTable.setFileName(fileName);
fileTable.setFilePath(filePath);
// fileTable.setCourseId(courseId); // 设置主键值
//
// this.save(fileTable); // 使用save方法来添加新的记录
UpdateWrapper<FileTable> fileTableUpdateWrapper = new UpdateWrapper<>();
fileTableUpdateWrapper.eq("course_id", courseId);
this.update(fileTable, fileTableUpdateWrapper);
}
@Override
public void download(String courseId, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException {
FileTable fileTable = this.getById(courseId);
System.out.println(fileTable);
// 创建OSS客户端
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String key = extractKeyFromFullPath(fileTable.getFilePath());
OSSObject object = ossClient.getObject(bucketName, key);
String fileName = fileTable.getFileName();
// 设置保存文件的路径
String savePath = path + "/" + fileName;
// 获取文件的输入流
InputStream inputStream = object.getObjectContent();
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 创建保存文件
File saveFile = new File(savePath);
// 将文件内容复制到保存文件中
FileUtils.copyInputStreamToFile(inputStream, saveFile);
// 关闭输入流和OSS客户端
inputStream.close();
ossClient.shutdown();
}
private String extractKeyFromFullPath(String filePath) {
// 提取存储桶名称
String key = filePath.substring(filePath.indexOf("com") + 4, filePath.indexOf("?"));
return key;
}
}

@ -0,0 +1,21 @@
package com.teaching.backend.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.mapper.ResourcesRelationshipMapper;
import com.teaching.backend.model.entity.ResourceRelationship;
import com.teaching.backend.service.ResourcesRelationshipService;
import org.springframework.stereotype.Service;
/**
* ClassName: ResourcesRelationshipServiceImpl
* Package: com.teaching.backend.service.impl
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/12 21:05
* @Version 1.0
*/
@Service
public class ResourcesRelationshipServiceImpl extends ServiceImpl<ResourcesRelationshipMapper, ResourceRelationship> implements ResourcesRelationshipService {
}

@ -1,24 +0,0 @@
package com.teaching.backend.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.mapper.UserMapper;
import com.teaching.backend.model.entity.User;
import com.teaching.backend.service.UserService;
import org.springframework.stereotype.Service;
/**
* ClassName: UserServiceImpl
* Package: com.teaching.backend.service.impl
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/3 17:48
* @Version 1.0
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

@ -1,5 +1,6 @@
package com.teaching.backend.service.impl.favour;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.ErrorCode;
@ -7,10 +8,13 @@ import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.favour.SeResourceFavourMapper;
import com.teaching.backend.model.dto.favour.resourceFavour.ResourceFavourAddRequest;
import com.teaching.backend.model.entity.favour.SeResourceFavour;
import com.teaching.backend.service.favour.SeResourceFavourService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -62,4 +66,6 @@ public class SeResourceFavourServiceImpl extends ServiceImpl<SeResourceFavourMap
}
}

@ -7,7 +7,6 @@ import lombok.Data;
import java.io.*;
import java.net.URLDecoder;
import java.util.Map;
import java.util.Random;
@ -20,65 +19,53 @@ import java.util.Random;
public class WordUtil {
private Configuration configuration = null;
/*
* 模板文件存放的目录
*/
private String baseDir;
/*
* 模板文件名称
*/
private String templateFile;
/*
* word生成的输出目录
*/
private String outputDir;
private InputStream templateInputStream; // 模板文件输入流
public WordUtil(){
public WordUtil() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
/*
* <p>转换成word<br>
*/
public String createWord(Map<String,Object> dataMap){
public void setTemplateInputStream(InputStream templateStream) {
this.templateInputStream = templateStream;
}
configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径
public String createWord(Map<String,Object> dataMap) {
configuration.setClassForTemplateLoading(this.getClass(), "");
Template t = null;
try {
//得到模板文件
configuration.setDirectoryForTemplateLoading(new File(baseDir));
t = configuration.getTemplate(templateFile);
t = new Template(templateFile, new InputStreamReader(templateInputStream, "UTF-8"), configuration);
} catch (IOException e) {
e.printStackTrace();
}
//随机生成
Random random=new Random();
File outFile = new File(outputDir+ random.nextInt(200) + ".docx"); //导出文件
Random random = new Random();
File outFile = new File(outputDir + random.nextInt(200) + ".docx");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
t.process(dataMap, out);
return outFile.getPath();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (TemplateException | IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "文档生成失败";
}
}

@ -2,4 +2,4 @@ aliyun.oss.endpoint=oss-cn-wuhan-lr.aliyuncs.com
aliyun.oss.accessKeyId=LTAI5tFkdu3y5WddxbjgaG2F
aliyun.oss.accessKeySecret=1xUchxUTlmUBoTV5JQIrKsVjSkmsLF
aliyun.oss.bucketName=ceshi132132
filename=123.ftl

@ -3,13 +3,16 @@ server:
spring:
application:
name: teaching-backend
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
mvc:
pathmatch:
matching-strategy: ant_path_matcher
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/teaching_db
# url: jdbc:mysql://39.106.16.162:3306/teaching_db
url: jdbc:mysql://localhost:3306/teaching_db
username: root
password: root
mybatis:
@ -18,18 +21,18 @@ mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/**/mapper/*.xml
# 自定义jwt key
jwt:
tokenHeader: Authorization #JWT存储的请求头
secret: mySecret #JWT加解密使用的密钥
expiration: 604800 #JWT的超期限时间(60*60*24)一天
tokenHead: Bearer #JWT负载中拿到开头
tokenHeader: Authorization
secret: mySecret
expiration: 604800
tokenHead: Bearer
knife4j:
enable: true
openapi:
title: 用户管理接口文档
description: "用户管理接口文档"
title: 111
description: "123"
concat: zjh
version: v1.0.0
group:
@ -40,7 +43,7 @@ knife4j:
- com.teaching.backend.controller
secure:
ignored:
urls: #安全路径白名单
urls:
#- /swagger-ui/
- /**
# - /swagger-resources/**

@ -19,30 +19,32 @@
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script>
new Vue({
el: '#app2',
el: '#app',
data: {
selectedFile:''
asd: []
},
methods: {
handleFileChange(event) {
this.selectedFile = event.target.files[0];
},
uploadFile() {
const formData = new FormData();
formData.append('file', this.selectedFile);
// 发送文件上传请求
axios.post('/video/upload', formData)
fetchList() {
const params = {
resourceIdList: [4], // 传递资源ID列表
type: 1 // 资源类型
};
axios.get('/Favour/get', {
params: params
})
.then(response => {
// 处理上传成功的响应
console.log(response.data);
this.asd = response.data.records; // 直接赋值给 asd
console.log( this.asd )
})
.catch(error => {
// 处理上传失败的错误
console.error(error);
console.error("出错了", error);
});
}
},
},
mounted() {
this.fetchList(); // 在组件挂载后立即调用fetchList方法获取资源记录
}
});

@ -1,9 +1,14 @@
package com.teaching;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
@SpringBootTest
class TeachingBackendApplicationTests {
Map<String, List<String>> userLearningRecords = new HashMap<>();
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save