将图像发送到端点时出现问题,错误 "status":500,"error":"Internal Server Error"

Problem sending image to endpoint, error "status":500,"error":"Internal Server Error"

我需要发图片给第三方api,但是出现错误{"timestamp": "2021-11-15T15:25:06.532 + 00:00", "status" : 500, "error": "内部服务器错误", "路径":"/api/send/"}

如果我直接联系服务(postman http:///exampleResourese/sendimg),然后我正常得到答案,那么问题出在我的代码中(在端点 http://localhost:8091/api/send).

告诉我如何通过 RestTemlate 或其他方法发送图片。

第三方服务接受图片字符串($binary)

控制器:

   @RequestMapping(value = "/send",method = RequestMethod.POST)
     public ResponseEntity<?> sendCopyPassport(@RequestParam("images") MultipartFile files) throws MalformedURLException, IOException{
    
        return documentsReceivingService.uploadAndGetListDocuments(files);
    
}

服务:

@服务 public class DocumentsReceivingService {

   @Autowired
    RestTemplate restTemplate;

   private final String UPLOADFILE = "http://exampleResourese/sendimg";


 Logger logger = LoggerFactory.getLogger(DocumentsReceivingService.class);

    public ResponseEntity<?> uploadAndGetListDocuments(MultipartFile files) throws MalformedURLException, IOException{


LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
     Charset charset = Charset.forName("UTF-8");
        String str =null;
       
       
       byte[] bytes =null;
try{

bytes = files.getBytes();
 str = new String(bytes,charset);
} catch (IOException e) {
            
            ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body( new MessageResponse("err"));
        }

   map.add("images",str);
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.MULTIPART_FORM_DATA);

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
String response = restTemplate.postForEntity(UPLOADFILE, requestEntity, String.class);

return  ResponseEntity.ok(response);

}

错误:

Response 500 INTERNAL_SERVER_ERROR
2021-11-15 20:43:00.938 DEBUG 22308 --- [nio-8091-exec-2] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"
2021-11-15 20:43:00.938 ERROR 22308 --- [nio-8091-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"] with root cause

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"

请问如何正确转图?这个图片会从前端加载,然后传到我的服务器,之后我再传到端点

如果您将该服务用作代理(并且您不打算将文件下载到您的主管),请尝试通过请求 json 将文件传输到 base64(将字节编码为 base64)。 例如:

{ 
  "file": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA .. "
}

查看第三方服务接受何种形式。

不过,如果你想使用 multipart,那么将它保存到你自己的目录中的某个地方,然后从那里将文件上传到另一个服务(因为你说如果你使用邮递员,它会正常发送,并且它从您的 PC 目录发送它),在这种情况下,文件 class 会有所帮助。 测试,如果有问题,再展示执行结果。