添加文件实现

This commit is contained in:
yizhaorong
2020-05-28 08:00:49 +08:00
parent 164c852811
commit cc78d6b1f2
3 changed files with 82 additions and 6 deletions
+22
View File
@@ -34,7 +34,13 @@ public class Package {
private String extra;
// 文件名
private String fileName;
// 源文件
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="source_file_id",referencedColumnName="id")
private Storage sourceFile;
// 图标
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="icon_file_id",referencedColumnName="id")
private Storage iconFile;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name = "appId")
@@ -132,6 +138,22 @@ public class Package {
this.fileName = fileName;
}
public Storage getSourceFile() {
return sourceFile;
}
public void setSourceFile(Storage sourceFile) {
this.sourceFile = sourceFile;
}
public Storage getIconFile() {
return iconFile;
}
public void setIconFile(Storage iconFile) {
this.iconFile = iconFile;
}
public App getApp() {
return app;
}
@@ -4,8 +4,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.yzr.model.Storage;
import org.yzr.utils.CharUtil;
import org.yzr.utils.file.FileType;
import org.yzr.utils.file.FileUtil;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.nio.file.Path;
import java.util.stream.Stream;
@@ -44,6 +47,16 @@ public class StorageUtil {
*/
public Storage store(InputStream inputStream, long contentLength, String contentType, String fileName) {
String key = generateKey(fileName);
try {
int len = 28;
byte[] b = new byte[len];
FileType type = FileUtil.getType(input);
input.unread(b);
inputStream = input;
} catch (Exception e) {
e.printStackTrace();
}
storage.store(inputStream, contentLength, contentType, key);
String url = generateUrl(key);
+47 -6
View File
@@ -1,17 +1,24 @@
package org.yzr.utils.file;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
public class FileUtil {
/**
* 判断文件类型
*/
public static FileType getType(String filePath) throws IOException {
public static FileType getType(String filePath) {
// 获取文件头
String fileHead = getFileHeader(filePath);
return getFileType(fileHead);
}
public static FileType getType(InputStream inputStream) {
return getFileType(getFileHeader(inputStream));
}
private static FileType getFileType(String fileHead) {
if (fileHead != null && fileHead.length() > 0) {
fileHead = fileHead.toUpperCase();
FileType[] fileTypes = FileType.values();
@@ -24,19 +31,53 @@ public class FileUtil {
return null;
}
private static String getFileHeader(InputStream inputStream) {
InputStream stream = copy(inputStream);
byte[] b = new byte[28];
try {
stream.read(b, 0, 28);
} catch (Exception e) {
e.printStackTrace();
}
return bytesToHex(b);
}
private static InputStream copy(InputStream inputStream) {
InputStream cloneInputStream = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > -1 ) {
baos.write(buffer, 0, len);
}
baos.flush();
cloneInputStream = new ByteArrayInputStream(baos.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return cloneInputStream;
}
/**
* 读取文件头
*/
private static String getFileHeader(String filePath) throws IOException {
private static String getFileHeader(String filePath) {
byte[] b = new byte[28];
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
inputStream.read(b, 0, 28);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}