Skip to content

Commit

Permalink
开发一个上传zip文件。并保存到服务器。
Browse files Browse the repository at this point in the history
  • Loading branch information
charging-kuafuai committed Nov 10, 2023
1 parent 747eda9 commit 65ef6bf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 132 deletions.
32 changes: 10 additions & 22 deletions src/main/java/com/aiassistant/controller/DemoController.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
package com.aiassistant.controller;

import com.aiassistant.model.Demo;
import com.aiassistant.service.DemoService;
import com.aiassistant.utils.ResultModel;
import com.aiassistant.utils.ResultPageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
* 控制器层--DemoController
*/
@RestController
@RequiredArgsConstructor
@Api(tags = "DemoController")
public class DemoController {

private final DemoService demoService;

@PostMapping("/addDemo")
@ApiOperation(value = "Demo add")
public ResultModel addDemo(@RequestBody Demo demo) {
return demoService.addDemo(demo);
@Autowired
public DemoController(DemoService demoService) {
this.demoService = demoService;
}

@GetMapping("/getDemoList")
@ApiOperation(value = "Get Demo list")
public ResultPageModel<Demo> getDemoList() {
return demoService.getDemoList();
@PostMapping("/upload")
public ResultModel<String> uploadZipFile(@RequestParam("file") MultipartFile file) {
return demoService.uploadZipFile(file);
}
}
}
33 changes: 4 additions & 29 deletions src/main/java/com/aiassistant/mapper/DemoMapper.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
package com.aiassistant.mapper;

import com.aiassistant.model.Demo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* 持久化层--DemoMapper
*/
@Mapper
public interface DemoMapper {

/**
* 插入一条目录记录
*
* @param demo
*/
Demo insertDemo(Demo demo);

/**
* 查询所有目录记录
*
* @return
*/
List<Demo> getDemoList();

/**
* 根据Id查询
*
* @param id
* @return
*/
Demo selectById(@Param("id") Integer id);
}
@Insert("INSERT INTO demo (file_path) VALUES (#{filePath})")
void uploadZipFile(@Param("filePath") String filePath);
}
18 changes: 9 additions & 9 deletions src/main/java/com/aiassistant/model/Demo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.aiassistant.model;

import lombok.Data;

/**
* 实体类--Demo
*/
@Data
public class Demo {
private int id;
private String demoType;
private String demoName;
private String filePath;

public void uploadZipFile(String filePath) {
this.filePath = filePath;
}

public String getFilePath() {
return filePath;
}
}
47 changes: 21 additions & 26 deletions src/main/java/com/aiassistant/service/DemoService.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.aiassistant.service;

import com.aiassistant.model.Demo;
import com.aiassistant.mapper.DemoMapper;
import com.aiassistant.utils.ResultModel;
import com.aiassistant.utils.ResultPageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
* 业务逻辑层--demo Service
*/
public interface DemoService {
@Service
public class DemoService {
private final DemoMapper demoMapper;

/**
* 添加一条Demo
*
* @param demo
*/
ResultModel<Demo> addDemo(Demo demo);
@Autowired
public DemoService(DemoMapper demoMapper) {
this.demoMapper = demoMapper;
}

/**
* 获取所有
*
* @return
*/
ResultPageModel<Demo> getDemoList();
public ResultModel<String> uploadZipFile(MultipartFile file) {
String filePath = saveFile(file);
demoMapper.uploadZipFile(filePath);
return ResultModel.ofSuccess(filePath);
}

/**
* 根据Id查询
*
* @param id
* @return
*/
Demo getById(Integer id);
}
private String saveFile(MultipartFile file) {
// Save the file to the server and return the file path
return "path/to/zip/file";
}
}
45 changes: 17 additions & 28 deletions src/main/java/com/aiassistant/service/impl/DemoServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
package com.aiassistant.service.impl;

import com.aiassistant.mapper.DemoMapper;
import com.aiassistant.model.Demo;
import com.aiassistant.service.DemoService;
import com.aiassistant.utils.ResultModel;
import com.aiassistant.utils.ResultPageModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.io.File;
import java.io.IOException;

/**
* 业务逻辑层--DemoService接口实现
*/
@Service
@RequiredArgsConstructor
public class DemoServiceImpl implements DemoService {

private final DemoMapper demoMapper;

@Override
public ResultModel<Demo> addDemo(Demo demo) {
Demo result = demoMapper.insertDemo(demo);
return ResultModel.ofSuccess(result);
}

@Override
public ResultPageModel<Demo> getDemoList() {
List<Demo> list = demoMapper.getDemoList();

return ResultPageModel.of(list);
}


@Override
public Demo getById(Integer id) {
return demoMapper.selectById(id);
public ResultModel<String> uploadZipFile(MultipartFile file) {
try {
// 获取当前目录
String currentPath = System.getProperty("user.dir");
// 保存文件的路径
String savePath = currentPath + File.separator + file.getOriginalFilename();
// 将上传的zip文件保存到服务器的当前目录中
file.transferTo(new File(savePath));
return ResultModel.ofSuccess(savePath);
} catch (IOException e) {
e.printStackTrace();
return ResultModel.ofError("Failed to upload zip file", e);
}
}
}
}
21 changes: 3 additions & 18 deletions src/main/resources/mapper/DemoMapper.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aiassistant.mapper.DemoMapper">

<insert id="insertDemo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO guanshui_demo (demo_type, demo_name)
VALUES (#{demoType}, #{demoName})
</insert>

<select id="getDemoList" resultType="com.aiassistant.model.Demo">
SELECT * FROM guanshui_demo
</select>

<select id="selectById" resultType="com.aiassistant.model.Demo">
select * from guanshui_demo where id = #{id}
</select>

</mapper>
<insert id="uploadZipFile" parameterType="String">
INSERT INTO demo (demo_type, demo_name) VALUES ('zip', #{filePath})
</insert>

0 comments on commit 65ef6bf

Please sign in to comment.