我必须在java中使用RESTFUL Spring API下载一些文件。我已经编写了API,如果我直接在浏览器上使用它,它可以正常工作。
@RequestMapping(value = "/download/{tagCategory}/{query}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getResourceFile(@PathVariable String tagCategory, @PathVariable String query) throws IOException
{
File file = new File(FILE_PATH + tagCategory + "/" + query + ".txt");
if (file.exists())
System.out.println(file.toString() + " exists");
else
System.out.println(file.toString() + "not exists");
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName()).contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(file.length())
.body(resource);
}
现在,我想创建一个包装器(我将在我的不同项目中使用),它将调用此API并获取下载的文件。 包装器就像
@Override
public InputStreamResource getResourceFile(String tagCategory, String query) throws Exception
{
Map<String, Object> requestBody = new HashMap<String, Object>();
System.out.println("sdsdS12");
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<Map<String, Object>>(requestBody);
@SuppressWarnings("unchecked")
CustomResponse<InputStreamResource> response = (CustomResponse<InputStreamResource>) performRestCall(
"http://localhost:8080/SpringRestfulWebServicesExample/download/" + tagCategory + "/" + query, HttpMethod.GET, httpEntity,
new ParameterizedTypeReference<CustomResponse<InputStreamResource>>()
{});
InputStreamResource isr = response.getPayload();
File file = new File("C:/arpittandon.txt");
FileUtils.copyInputStreamToFile(isr.getInputStream(), file);
if (response.isSuccess())
return response.getPayload();
else
throw new Exception(response.getMessage());
}
其中CustomResponse是
public class CustomResponse<T>
{
private boolean success = true;
private String message = "";
private T payload;
public boolean isSuccess()
{
return success;
}
public void setSuccess(boolean success)
{
this.success = success;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public T getPayload()
{
return payload;
}
public void setPayload(T payload)
{
this.payload = payload;
}
}
并且performRESTCall是
@SuppressWarnings({ "rawtypes" })
protected Object performRestCall(String url, HttpMethod httpMethod, HttpEntity body, ParameterizedTypeReference ptr)
{
return performRestCall(url, httpMethod, body, ptr, HTTP_TIMEOUT);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object performRestCall(String url, HttpMethod httpMethod, HttpEntity body, ParameterizedTypeReference ptr, int timeout)
{
// System.out.println(url);
// System.out.println(body.toString());
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(HTTP_TIMEOUT);
factory.setConnectTimeout(timeout);
RestTemplate restTemplate = new RestTemplate(factory);
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
return restTemplate.exchange(url, httpMethod, body, ptr).getBody();
}
当我这样称呼时,我收到以下错误
org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法识别的令牌&#39; mesh_mh&#39;:期待(&#39; true&#39;,&#39; false&#39;或&#39;空&#39)
我在java中并不是很好,并且使用上面的代码作为现有代码中的模板。
我只需要在我的不同项目中使用api下载文件。我怎么能这样做