|
@@ -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());
|
|
|
+ String fileAbsName = absPath.concat("/").concat(newName);
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ String md5;
|
|
|
+ try (FileInputStream inputStream = new FileInputStream(newFile)) {
|
|
|
+ md5 = DigestUtils.md5Hex(inputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeFile(String fileId, OutputStream out) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String md5(String fileId) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|