首页 > 解决方案 > 将图像发送到端点时出现问题,错误“状态”:500,“错误”:“内部服务器错误”

问题描述

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

如果我直接联系服务(邮递员 http://exampleResourese/sendimg),那么我会正常得到答案,那么问题出在我的代码中(在端点 http://localhost:8091/api/send 上)。

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

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

控制器:

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

服务 :

@Service 公共类 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"}"

请告诉我如何正确传输图片?这张图片将从前面加载,然后到我的服务器,之后我将它进一步传输到端点

标签: javaspring-bootresthttp-post

解决方案


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

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

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

但是,如果您想使用多部分,请将其保存在目录中的某个位置,然后从那里将文件上传到另一个服务(因为您说如果您使用邮递员,它会正常发送,然后从您的 PC 发送目录),在这种情况下,File 类会有所帮助。测试,如果有问题,则显示执行结果。


推荐阅读