瀏覽代碼

新增文件服务,待完成

zhouhao 7 年之前
父節點
當前提交
c841dc398f

+ 15 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-api/pom.xml

@@ -0,0 +1,15 @@
+<?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">
+    <parent>
+        <artifactId>hsweb-system-file-service</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-system-file-service-api</artifactId>
+
+
+</project>

+ 27 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-api/src/main/java/org/hswebframework/web/service/file/FileInfo.java

@@ -0,0 +1,27 @@
+package org.hswebframework.web.service.file;
+
+import java.io.Serializable;
+
+/**
+ * Created by zhouhao on 2017/7/8.
+ */
+public interface FileInfo  extends Serializable{
+
+    String getId();
+
+    void setId(String id);
+
+    String getName();
+
+    long getLength();
+
+    long getCreateTime();
+
+    String getCreatorId();
+
+    String getMd5();
+
+    String getLocation();
+
+
+}

+ 19 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-api/src/main/java/org/hswebframework/web/service/file/FileService.java

@@ -0,0 +1,19 @@
+package org.hswebframework.web.service.file;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ */
+public interface FileService {
+
+    InputStream readFile(String fileId);
+
+    FileInfo saveFile(InputStream fileStream,String fileName,String creatorId) throws IOException;
+
+    void writeFile(String fileId, OutputStream out);
+
+    String md5(String fileId);
+
+}

+ 31 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-simple/pom.xml

@@ -0,0 +1,31 @@
+<?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">
+    <parent>
+        <artifactId>hsweb-system-file-service</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-system-file-service-simple</artifactId>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-system-file-service-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework</groupId>
+            <artifactId>hsweb-utils</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-codec</groupId>
+            <artifactId>commons-codec</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 78 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-simple/src/main/java/org/hswebframework/web/service/file/LocalFileService.java

@@ -0,0 +1,78 @@
+package org.hswebframework.web.service.file;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.hswebframework.utils.time.DateFormatter;
+
+import java.io.*;
+import java.util.Date;
+
+/**
+ * Created by zhouhao on 2017/7/8.
+ */
+public class LocalFileService implements FileService {
+
+    private String fileBasePath=".";
+
+
+    public String getFileBasePath() {
+        return fileBasePath;
+    }
+
+    @Override
+    public InputStream readFile(String fileId) {
+
+        return null;
+    }
+
+    @Override
+    public FileInfo saveFile(InputStream fileStream, String fileName,String creatorId) throws IOException {
+        //配置中的文件上传根路径
+        String fileBasePath = getFileBasePath();
+        //文件存储的相对路径,以日期分隔,每天创建一个新的目录
+        String filePath = "/file/".concat(DateFormatter.toString(new Date(),"yyyy-MM-dd"));
+        //文件存储绝对路径
+        String absPath = fileBasePath.concat(filePath);
+        File path = new File(absPath);
+        if (!path.exists()) path.mkdirs(); //创建目录
+        String newName = String.valueOf(System.nanoTime()); //临时文件名 ,纳秒的md5值
+        String fileAbsName = absPath.concat("/").concat(newName);
+        //try with resource
+        long fileLength = 0;
+        try (BufferedInputStream in = new BufferedInputStream(fileStream);
+             BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(fileAbsName))) {
+            byte[] buffer = new byte[2048 * 10];
+            int len;
+            while ((len = in.read(buffer)) != -1) {
+                fileLength+=len;
+                os.write(buffer, 0, len);
+            }
+            os.flush();
+        }
+        File newFile = new File(fileAbsName);
+        //获取文件的md5值
+        String md5;
+        try (FileInputStream inputStream = new FileInputStream(newFile)) {
+            md5 = DigestUtils.md5Hex(inputStream);
+        }
+        //判断文件是否已经存在
+      //  Resources resources = resourcesService.selectByMd5(md5);
+       // if (resources != null) {
+         //   newFile.delete();//文件已存在则删除临时文件不做处理
+           // return resources;
+        //} else {
+          //  newName = md5;
+            //newFile.renameTo(new File(absPath.concat("/").concat(newName)));
+        //}
+        return null;
+    }
+
+    @Override
+    public void writeFile(String fileId, OutputStream out) {
+
+    }
+
+    @Override
+    public String md5(String fileId) {
+        return null;
+    }
+}

+ 20 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/pom.xml

@@ -0,0 +1,20 @@
+<?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">
+    <parent>
+        <artifactId>hsweb-system-file</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-system-file-service</artifactId>
+    <packaging>pom</packaging>
+    <modules>
+        <module>hsweb-system-file-service-api</module>
+        <module>hsweb-system-file-service-simple</module>
+    </modules>
+
+
+</project>

+ 4 - 0
hsweb-system/hsweb-system-file/pom.xml

@@ -10,6 +10,10 @@
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>hsweb-system-file</artifactId>
+    <packaging>pom</packaging>
+    <modules>
+        <module>hsweb-system-file-service</module>
+    </modules>
 
 
 </project>