重构 FastDFSFile。
parent
89584d2a48
commit
0f145e383e
|
@ -1,8 +1,17 @@
|
||||||
package xyz.zhouxy.plusone.oss;
|
package xyz.zhouxy.plusone.oss;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import org.csource.common.MyException;
|
import org.csource.common.MyException;
|
||||||
import org.csource.common.NameValuePair;
|
import org.csource.common.NameValuePair;
|
||||||
|
@ -14,7 +23,10 @@ import org.csource.fastdfs.TrackerServer;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import xyz.zhouxy.plusone.exception.SysException;
|
||||||
|
|
||||||
public class FastDFSUtil {
|
public class FastDFSUtil {
|
||||||
|
|
||||||
|
@ -44,7 +56,7 @@ public class FastDFSUtil {
|
||||||
* @throws FastDFSException
|
* @throws FastDFSException
|
||||||
*/
|
*/
|
||||||
public String[] upload(FastDFSFile file) throws FastDFSException {
|
public String[] upload(FastDFSFile file) throws FastDFSException {
|
||||||
logger.info("File Name: {}, File Length: {}", file.getName(), file.getContent().length);
|
logger.info("File Name: {}, File Length: {}", file.getFileName(), file.getContent().length);
|
||||||
|
|
||||||
NameValuePair[] metaList = new NameValuePair[1];
|
NameValuePair[] metaList = new NameValuePair[1];
|
||||||
metaList[0] = new NameValuePair("author", file.getAuthor());
|
metaList[0] = new NameValuePair("author", file.getAuthor());
|
||||||
|
@ -63,7 +75,7 @@ public class FastDFSUtil {
|
||||||
uploadResults[0], uploadResults[1], System.currentTimeMillis() - startTime);
|
uploadResults[0], uploadResults[1], System.currentTimeMillis() - startTime);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FastDFSException("IO Exception when uploadind the file:" + file.getName(), e);
|
throw new FastDFSException("IO Exception when uploadind the file:" + file.getFileName(), e);
|
||||||
} catch (MyException e) {
|
} catch (MyException e) {
|
||||||
throw new FastDFSException(e);
|
throw new FastDFSException(e);
|
||||||
}
|
}
|
||||||
|
@ -109,18 +121,68 @@ public class FastDFSUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
|
||||||
public static final class FastDFSFile {
|
public static final class FastDFSFile {
|
||||||
private String name;
|
@Getter
|
||||||
private byte[] content;
|
private final String fileName;
|
||||||
private String ext;
|
private final byte[] content;
|
||||||
private String md5;
|
@Getter
|
||||||
private String author;
|
private final String ext;
|
||||||
|
@Getter
|
||||||
|
private final String md5;
|
||||||
|
@Getter
|
||||||
|
private final String author;
|
||||||
|
|
||||||
public FastDFSFile(String name, byte[] content, String ext) {
|
private FastDFSFile(@Nonnull File file, @Nullable String author) throws IOException {
|
||||||
this.name = name;
|
this.fileName = file.getName();
|
||||||
|
this.content = Files.toByteArray(file);
|
||||||
|
this.ext = Files.getFileExtension(fileName);
|
||||||
|
this.md5 = md5Hex(content);
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FastDFSFile(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) {
|
||||||
|
this.fileName = fileName;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.ext = ext;
|
this.ext = Files.getFileExtension(fileName);
|
||||||
|
this.md5 = md5Hex(content);
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static FastDFSFile of(@Nonnull File file) throws IOException {
|
||||||
|
return new FastDFSFile(file, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static FastDFSFile of(@Nonnull File file, @Nullable String author) throws IOException {
|
||||||
|
return new FastDFSFile(file, author);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content) {
|
||||||
|
return new FastDFSFile(fileName, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) {
|
||||||
|
return new FastDFSFile(fileName, content, author);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getContent() {
|
||||||
|
return Arrays.copyOf(content, content.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private static String md5Hex(byte[] data) {
|
||||||
|
try {
|
||||||
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||||
|
messageDigest.update(data);
|
||||||
|
byte[] result = messageDigest.digest();
|
||||||
|
var sha512Hex = new BigInteger(1, result).toString(16);
|
||||||
|
return Objects.requireNonNull(sha512Hex);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new SysException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
package xyz.zhouxy.plusone;
|
package xyz.zhouxy.plusone;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@ -24,10 +23,7 @@ class FastDFSTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testOSS() throws FileNotFoundException, IOException, FastDFSException {
|
void testOSS() throws FileNotFoundException, IOException, FastDFSException {
|
||||||
try (FileInputStream in = new FileInputStream("D:\\ZhouXY\\Desktop\\666.png");) {
|
String[] upload = fastDFSUtil.upload(FastDFSFile.of(new File("D:\\ZhouXY\\Desktop\\666.png")));
|
||||||
byte[] content = IOUtils.toByteArray(in);
|
|
||||||
String[] upload = fastDFSUtil.upload(new FastDFSFile("666.png", content, "png"));
|
|
||||||
log.info(String.join("/", upload));
|
log.info(String.join("/", upload));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue