Spring MVC RestTemplate文件上传报401错误求助及代码排查
问题描述
我需要通过Spring MVC RestTemplate按照下图所示的参数上传文件。
我尝试了多种实现方式,但均报错401 Unauthorized。我需要一份可通过Spring MVC RestTemplate实现文件上传的代码,现将自己无法正常运行的代码附上,请帮忙排查问题。
我的代码示例
@RequestMapping(value = "/uploadPEFile", method = { RequestMethod.POST}, produces = "application/json") @ResponseBody public String uploadPEFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse httpResponse) { JsonResponse objResponse = null; String enterpriseId = ""; String enterpriseName = ""; String type = ""; String productId = ""; String userIds = ""; String title = ""; String description = ""; String date = ""; String externalUrl = ""; String presenterName = ""; try { enterpriseId = request.getParameter("enterpriseId"); enterpriseName = request.getParameter("enterpriseName"); type = request.getParameter("type"); productId = request.getParameter("productId"); userIds = request.getParameter("userIds"); title = request.getParameter("title"); description = request.getParameter("description"); date = request.getParameter("date"); if(date != null && !date.isEmpty()) { String [] dateArray = date.split(" "); date = dateArray[2]+" "+getMonth(dateArray[1])+", "+dateArray[3]; } externalUrl = request.getParameter("externalURL"); presenterName = request.getParameter("presenterName"); JsonObject jsonBody = new JsonObject(); jsonBody.addProperty("title", title); jsonBody.addProperty("description", description); jsonBody.addProperty("date", date); jsonBody.addProperty("external_url", externalUrl); jsonBody.addProperty("presenter_name", presenterName); String cmsDeviceId = PortalUtil.getPropertyValue("cms.device.id"); if (file != null) { final String filename = file.getOriginalFilename(); ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) { @Override public String getFilename() { return filename; } }; String filePath = "/persivia edge/"+type+"/"+filename; File userFile = new File(System.getProperty(PortalConstants.cdnPath) + filePath); if(!userFile.exists()) { userFile.mkdirs(); } file.transferTo(userFile); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); body.add("device_id",cmsDeviceId); // body.add("attachment",fileAsResource); // body.add("attachment",new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename())); body.add("attachment",new FileSystemResource(filePath)); body.add("source", "web"); body.add("filename", filename); body.add("enterprise_id", enterpriseId); body.add("enterpriseName", enterpriseName); body.add("type", type); body.add("product_id",productId); body.add("userIds",userIds); body.add("body", jsonBody.toString()); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); RestTemplate restTemplate = new RestTemplate(); String requestUrl = PortalUtil.getPropertyValue("cms.api.url")+"wp-json/wp/v2/upload_content"; String response = restTemplate.postForObject(requestUrl, requestEntity, String.class); httpResponse.setStatus(HttpServletResponse.SC_OK); JsonObject data = new JsonObject(); objResponse = JsonResponse.build(ResponseStatus.status200, ResponseStatus.message200, "Logo info updated successfully", data); } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e.getCause()); objResponse = JsonResponse.build(ResponseStatus.status500, ResponseStatus.message500, e.getMessage(), null); httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return new Gson().toJson(objResponse); }
内容的提问来源于stack exchange,提问作者Majid Khakwani
相关产品推荐
相关产品推荐

