A项目接口
多参数接收对象
import lombok.Data;
@Data
public class FileDataVO {
private String field1;
private String field2;
...
}
保存接口
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.xxx.common.utils.R;
import com.xxx.common.utils.TokenUtils;
import com.xxx.vo.FileDataVO
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/file")
public class FileController {
@Value("${remote.app-server.url}")
private String url;
@PostMapping("/saveImgAndData")
public R saveImgAndData(MultipartHttpServletRequest request, @RequestParam String params) throws IOException {
FileDataVO vo = JSON.parseObject(params, FileDataVO.class);
String token = TokenUtils.getByUsername(url + "/remote/login", vo.getUsername());
MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
List<MultipartFile> files = multiFileMap.get("files");
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
if (CollUtil.isNotEmpty(files)) {
List<Object> fileList = new ArrayList<>();
for (MultipartFile file : files) {
ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() throws IllegalStateException {
return file.getOriginalFilename();
}
};
fileList.add(byteArrayResource);
}
map.put("files", fileList);
}
map.add("data", vo);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("token", token);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, headers);
Long result = restTemplate.postForObject(url + "/file/saveImgAndData", httpEntity, Long.class);
return R.ok().put("id", result);
}
}
B项目接口
多参数接收对象
import lombok.Data;
@Data
public class FileDataVO {
private String field1;
private String field2;
...
}
上传图片到本地工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
public class FileUploadUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUploadUtils.class);
public static String uploadFileToLocal(MultipartFile file, Long id, String filePath, String uploadName) {
logger.info("====>Enter the B method uploadFileToLocal");
String relative = null;
try {
String folderPath = filePath + File.separator + id + File.separator;
logger.info("====>folderPath:" + folderPath);
File folder = new File(folderPath);
if (!folder.exists()) {
logger.info("====>folder.mkdirs()");
folder.mkdirs();
}
relative = File.separator + id + File.separator + uploadName;
String path = filePath + relative;
File targetFile = new File(path);
file.transferTo(targetFile);
logger.info("====>Exit the B method uploadFileToLocal");
} catch (IOException e) {
e.printStackTrace();
}
return relative;
}
}
A项目接口调用B项目接口
import com.gexin.fastjson.JSON;
import com.xxx.vo.FileDataVO
import com.xxx.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileController {
@Value("${file.savePath}")
private String savePath;
@PostMapping("/saveImgAndData")
public Long saveImgAndData(@RequestParam(required = false) MultipartFile[] files, @RequestParam("data") String data) {
FileDataVO vo = JSON.parseObject(data, FileDataVO.class);
Long id = saveData.getId();
if (files != null && files.length >= 1) {
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
String filePath = FileUploadUtils.uploadFileToLocal(file, id, savePath, fileName);
}
}
return id;
}
}