0
当我试图从笔记本电脑上传文件到我的服务器时遇到了一个错误。是否可以在桌面上使用RestTemplate而不是Android?
Exception in thread "main" java.lang.NoClassDefFoundError: android/os/Build$VERSION
我搜索了错误代码,发现答案是暗示我应该在Android设备上运行我的代码,但如果我在我的笔记本上运行我的代码。是否可以创建在桌面上运行的RestTemplate独立程序?
服务器端代码:
@RequestMapping(value="/upload", method=RequestMethod.POST)
public String handleUpload(String filename, MultipartFile file) throws IOException {
if(!file.isEmpty()) {
File saveFile = new File(rootPath + "\\" + filename);
saveFile.createNewFile();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(saveFile));
FileCopyUtils.copy(file.getInputStream(), bufferedOutputStream);
bufferedOutputStream.close();
return "uploaded successfully";
} else {
return "failed";
}
}
客户端代码:
public static void main(String[] args)
{
String url = UPLOAD_URL;
String filePath = PATH + "\\204375-106.jpg";
SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
RestTemplate rest = new RestTemplate(httpRequestFactory);
FileSystemResource resource = new FileSystemResource(new File(filePath));
MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();
param.add("file", resource);
param.add("filename", "204375-106.jpg");
String string = rest.postForObject(url, param, String.class);
System.out.println(string);
}