服务器配置(转载):https://www.cnblogs.com/pfnie/articles/6759105.html
代码配置:(https://download.csdn.net/download/qq_31293575/11223575)
配置文件配置:
spring:
data:
mongodb:
password: bgy123456
port: 27017
host: 47.92.193.157
name: bgy
mongodb:
database: attachmentsDB
单元测试:
package com.asdf.funny.images;
import com.asdf.funny.images.service.AttachmentsManager;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImagesApplicationTests {
@Autowired
private AttachmentsManager attachmentsManager;
public static String baseImgUrl = "/admin/accessorybag/attachments/images?mongoId=";
@Test
public void contextLoads()throws Exception {
//所有图片
File file = new File("G:\\手机视频\\手机图片\\普通");
File [] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
File file1 = files[i];
FileInputStream input = new FileInputStream(file1);
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
Map<String, Object> map = attachmentsManager.fileUploadTMongoDBAndReids(multipartFile);
Map<String, Object> newMap = new HashMap<>();
if ("true".equals(map.get("success"))) {
//添加成功
String img = baseImgUrl + map.get("msg");
newMap.put("img", img);
newMap.put("mongoId", map.get("msg"));
newMap.put("fileName", map.get("fileName"));
}
}
}
}
成功截图:(具体参数请参照代码配置)