From 276fd516da5b62799b0301b2ad5a9d1cfb1bbeff Mon Sep 17 00:00:00 2001 From: changyou Date: Wed, 22 Jul 2020 16:27:12 +0800 Subject: [PATCH] upload file to gitlab --- pom.xml | 23 ++++ .../filesdemo/controller/FileController.java | 107 +++++++++++++++++- .../com/ch/filesdemo/service/FileService.java | 16 +++ src/main/resources/templates/index.html | 6 +- 4 files changed, 144 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 51fe65d..ef27ef3 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,29 @@ 2.3.0.RELEASE + + + org.gitlab + java-gitlab-api + 4.1.1 + + + + + commons-io + commons-io + 2.6 + + + + org.apache.commons + commons-compress + + + org.apache.commons + commons-text + + diff --git a/src/main/java/com/ch/filesdemo/controller/FileController.java b/src/main/java/com/ch/filesdemo/controller/FileController.java index dfd2aa0..edc1138 100644 --- a/src/main/java/com/ch/filesdemo/controller/FileController.java +++ b/src/main/java/com/ch/filesdemo/controller/FileController.java @@ -2,6 +2,10 @@ import com.ch.filesdemo.dto.UploadFileResponse; import com.ch.filesdemo.service.FileService; +import org.gitlab.api.GitlabAPI; +import org.gitlab.api.models.GitlabGroup; +import org.gitlab.api.models.GitlabProject; +import org.gitlab.api.models.GitlabSimpleRepositoryFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -10,12 +14,16 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.core.io.Resource; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; + import java.util.Arrays; +import java.util.Base64; import java.util.List; import java.util.stream.Collectors; @@ -27,6 +35,10 @@ @RestController public class FileController { + + private final String GitLab_URL = "http://192.168.110.89/"; + private final String api_Token = "eNjSzLSh3B_RBhG7uNgs"; + private static final Logger logger = LoggerFactory.getLogger(FileController.class); @Autowired @@ -34,13 +46,21 @@ public class FileController { @PostMapping("/uploadFile") public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file){ + String fileName = fileService.storeFile(file); + String filePath = fileService.getFilePath(); - String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() - .path("/downloadFile/") - .path(fileName) - .toUriString(); + String group = "chtest"; + String projectName = "demo"; + String folderName = "documents"; + + String fileDownloadUri = "上传文件失败!"; + try { + fileDownloadUri = uploadToGitlab(group,projectName,folderName,fileName,filePath); + }catch (Exception e){ + System.out.println("上传文件失败: " + e.getMessage()); + } return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize()); } @@ -77,4 +97,81 @@ public ResponseEntity downloadFile(@PathVariable String fileName, Http .body(resource); } + + /** + * 上传文件到 gitlab + * @param group 组织名称 如 chtest + * @param projectName 工程名称,如 demo + * @param folderName 文件夹,可多层,如 ch/test + * @param fileName 文件名,如 readme.txt + * @param filePath 文件所在电脑路径 + * @throws IOException + */ + private String uploadToGitlab(String group, String projectName, String folderName, String fileName, String filePath) throws IOException { + + + String gitlabUrl = GitLab_URL; + String apiToken = api_Token; + + String branchName = "master"; + + GitlabAPI api = GitlabAPI.connect(gitlabUrl, apiToken); + + GitlabProject project = null; + try { + + GitlabGroup gitlabGroup = api.getGroup(group); + + try { + project = api.getProject(group,projectName); + if (project == null) { + project = api.createProjectForGroup(projectName, gitlabGroup); + } + }catch (Exception e){ + System.out.println("不存在项目,新建工程 " + e.getMessage()); + project = api.createProjectForGroup(projectName, gitlabGroup); + } + + String base64file = fileToBase64(filePath); + String gitlabPath = folderName+"/"+fileName; + + System.out.println("开始上传:" + gitlabPath); + GitlabSimpleRepositoryFile repositoryFile = api.createRepositoryFile(project, gitlabPath, branchName, + "上传文件", base64file); + + System.out.println("结束上传:" + repositoryFile.getFilePath()); + + return project.getWebUrl()+"/blob/master/"+gitlabPath; + }catch (Exception e) { + System.out.println(e.getMessage()); + } + return ""; + } + + private String fileToBase64(String path) { + String base64 = null; + InputStream in = null; + try { + File file = new File(path); + in = new FileInputStream(file); + byte[] bytes = new byte[(int) file.length()]; + in.read(bytes); + base64 = Base64.getEncoder().encodeToString(bytes); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + return base64; + } + } diff --git a/src/main/java/com/ch/filesdemo/service/FileService.java b/src/main/java/com/ch/filesdemo/service/FileService.java index bde9303..cc62f4c 100644 --- a/src/main/java/com/ch/filesdemo/service/FileService.java +++ b/src/main/java/com/ch/filesdemo/service/FileService.java @@ -28,6 +28,19 @@ public class FileService { // 文件在本地存储的地址 private final Path fileStorageLocation; + /** + * 文件本地路径 + */ + private String filePath; + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + @Autowired public FileService(FileProperties fileProperties) { this.fileStorageLocation = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize(); @@ -58,6 +71,9 @@ public String storeFile(MultipartFile file) { Path targetLocation = this.fileStorageLocation.resolve(fileName); Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING); + String path = targetLocation.toString(); + this.setFilePath(path); + return fileName; } catch (IOException ex) { throw new FileException("Could not store file " + fileName + ". Please try again!", ex); diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index c9990c6..4642f84 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -17,11 +17,11 @@

Sorry! Your browser doesn't support Javascript

-

Spring Boot File Upload / Download Rest API Example

+

上传 / 下载文件示例 demo

-

Upload Single File

+

上传单文件

@@ -32,7 +32,7 @@

Upload Single File

-

Upload Multiple Files

+

上传多文件