wuheng 2 anos atrás
commit
597d219161

+ 49 - 0
.gitignore

@@ -0,0 +1,49 @@
+######################################################################
+# Build Tools
+
+.gradle
+/build/
+!gradle/wrapper/gradle-wrapper.jar
+
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+######################################################################
+# IDE
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### JRebel ###
+rebel.xml
+
+### NetBeans ###
+nbproject/private/
+build/*
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
+*DS_Store
+######################################################################
+# Others
+*.log
+*.xml.versionsBackup
+*.swp
+
+!*/build/*.java
+!*/build/*.html
+!*/build/*.xml
+.vscode/
+git

+ 4 - 0
Dockerfile

@@ -0,0 +1,4 @@
+FROM openjdk:8
+COPY ./target/uploadfiles-1.0-SNAPSHOT.jar /tmp/app.jar
+EXPOSE 8080
+ENTRYPOINT java -Xms256m -Xmx512m -jar /tmp/app.jar

+ 57 - 0
pom.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.lovecoding.uploadfiles</groupId>
+    <artifactId>uploadfiles</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <parent>
+        <artifactId>spring-boot-dependencies</artifactId>
+        <groupId>org.springframework.boot</groupId>
+        <version>2.3.3.RELEASE</version>
+    </parent>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.github.tobato</groupId>
+            <artifactId>fastdfs-client</artifactId>
+            <version>1.27.2</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <mainClass>com.lovecoding.upload.Application</mainClass>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                            <goal>build-info</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 14 - 0
src/main/java/com/lovecoding/upload/Application.java

@@ -0,0 +1,14 @@
+package com.lovecoding.upload;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class,args);
+    }
+
+}
+

+ 28 - 0
src/main/java/com/lovecoding/upload/config/CorsConfig.java

@@ -0,0 +1,28 @@
+package com.lovecoding.upload.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.filter.FormContentFilter;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class CorsConfig implements WebMvcConfigurer {
+
+    public void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+                .allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token")
+                .allowedMethods("*")
+                .allowedOrigins("*")
+                .allowCredentials(true);
+    }
+
+    /**
+     * 支持PUT、DELETE请求
+     */
+    @Bean
+    public FormContentFilter httpPutFormContentFilter() {
+        return new FormContentFilter();
+    }
+
+}

+ 62 - 0
src/main/java/com/lovecoding/upload/config/FastDFSClientUtil.java

@@ -0,0 +1,62 @@
+package com.lovecoding.upload.config;
+
+import com.github.tobato.fastdfs.domain.fdfs.StorePath;
+import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
+import com.github.tobato.fastdfs.service.FastFileStorageClient;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+@Component
+public class FastDFSClientUtil {
+
+    @Autowired
+    private FastFileStorageClient storageClient;
+
+    /**
+     * 上传
+     * @param file
+     * @return
+     * @throws IOException
+     */
+    public String uploadFile(MultipartFile file) throws IOException {
+        StorePath storePath = storageClient.uploadFile((InputStream) file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
+        return storePath.getFullPath();
+    }
+
+    /**
+     * 删除
+     * @param filePath
+     */
+    public void delFile(String filePath) {
+        storageClient.deleteFile(filePath);
+
+    }
+
+    /**
+     * 下载
+     * @param groupName
+     * @param path
+     * @return
+     */
+    public byte[] download(String groupName, String path) throws IOException {
+        InputStream ins = storageClient.downloadFile(groupName, path, new DownloadCallback<InputStream>() {
+            public InputStream recv(InputStream ins) throws IOException {
+                return ins;
+            }
+        });
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        byte[] buff = new byte[100];
+        int rc = 0;
+        while ((rc = ins.read(buff, 0, 100)) > 0) {
+            byteArrayOutputStream.write(buff, 0, rc);
+        }
+        return byteArrayOutputStream.toByteArray();
+    }
+}

+ 12 - 0
src/main/java/com/lovecoding/upload/config/FdfsConfiguration.java

@@ -0,0 +1,12 @@
+package com.lovecoding.upload.config;
+
+import com.github.tobato.fastdfs.FdfsClientConfig;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.PropertySource;
+
+@Configuration
+@PropertySource("fast_dfs.properties")
+@Import(FdfsClientConfig.class)
+public class FdfsConfiguration {
+}

+ 42 - 0
src/main/java/com/lovecoding/upload/controller/FileController.java

@@ -0,0 +1,42 @@
+package com.lovecoding.upload.controller;
+
+import com.lovecoding.upload.config.FastDFSClientUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+@RestController
+@RequestMapping("/file")
+public class FileController {
+
+    @Autowired
+    private FastDFSClientUtil fastDFSClientUtil;
+
+    @Value("${fileServerUrl}")
+    private String fileServerUrl ;
+
+    @PostMapping("/upload")
+    public String uploadFile(MultipartFile file) {
+        try {
+            //判断文件是否存在
+            if (file == null) {
+                throw new RuntimeException("文件不存在");
+            }
+            //获取文件的完整名称
+            String originalFilename = file.getOriginalFilename();
+            if (StringUtils.isEmpty(originalFilename)) {
+                throw new RuntimeException("文件不存在");
+            }
+            String url = fastDFSClientUtil.uploadFile(file);
+            return fileServerUrl+url;
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "文件上传失败";
+    }
+}

+ 4 - 0
src/main/resources/application.properties

@@ -0,0 +1,4 @@
+server.port=8080
+
+spring.servlet.multipart.max-file-size=50MB
+spring.servlet.multipart.max-request-size=50MB

+ 8 - 0
src/main/resources/fast_dfs.properties

@@ -0,0 +1,8 @@
+#socket连接超时时长
+fdfs.soTimeout=3000
+#连接tracker服务器超时时长sfupload-files:0.4
+fdfs.connectTimeout=600
+
+fdfs.trackerList=fastdfs-tracker:22122
+#服务器地址
+fileServerUrl=http://39.105.160.25:11003/