|
|
@@ -0,0 +1,54 @@
|
|
|
+package com.uas.platform.b2c.common.base;
|
|
|
+
|
|
|
+import com.uas.platform.b2c.BaseJunitTest;
|
|
|
+import com.uas.platform.b2c.common.base.service.FileService;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+public class FileServiceTest extends BaseJunitTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpload() {
|
|
|
+ String filePath = "C:\\Users\\Administrator\\Desktop\\kaifa.jpg";
|
|
|
+ File file = new File(filePath);
|
|
|
+ String fileName = file.getName();
|
|
|
+ byte[] bytes = file2byte(file);
|
|
|
+ String returnPath = fileService.save(fileName, bytes);
|
|
|
+ System.out.println(returnPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] file2byte(String filePath) {
|
|
|
+ byte[] buffer = null;
|
|
|
+ File file = new File(filePath);
|
|
|
+ return file2byte(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] file2byte(File file) {
|
|
|
+ byte[] buffer = null;
|
|
|
+ try {
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int n;
|
|
|
+ while ((n = fis.read(b)) != -1) {
|
|
|
+ bos.write(b, 0, n);
|
|
|
+ }
|
|
|
+ fis.close();
|
|
|
+ bos.close();
|
|
|
+ buffer = bos.toByteArray();
|
|
|
+ }
|
|
|
+ catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|