是否可以在桌面上使用 RestTemplate 而不是 Android?
Is it possible to use RestTemplate on desktop instead of Android?
我在尝试将文件从笔记本电脑上传到服务器时遇到错误。
Exception in thread "main" java.lang.NoClassDefFoundError: android/os/Build$VERSION
我搜索了错误代码并找到了一个答案,提示我应该 运行 我的代码在 Android 设备上,但是如果我 运行 我的代码在我的笔记本电脑上呢?是否可以在桌面上创建一个 RestTemplate 独立程序 运行ning?
服务器端代码:
@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);
}
要在桌面环境中使用 RestTemplate,您需要 spring-web 库:
http://mvnrepository.com/artifact/org.springframework/spring-web/
包的名称和 类 相同,因此您可以重复使用您的代码。
我在尝试将文件从笔记本电脑上传到服务器时遇到错误。
Exception in thread "main" java.lang.NoClassDefFoundError: android/os/Build$VERSION
我搜索了错误代码并找到了一个答案,提示我应该 运行 我的代码在 Android 设备上,但是如果我 运行 我的代码在我的笔记本电脑上呢?是否可以在桌面上创建一个 RestTemplate 独立程序 运行ning?
服务器端代码:
@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);
}
要在桌面环境中使用 RestTemplate,您需要 spring-web 库: http://mvnrepository.com/artifact/org.springframework/spring-web/
包的名称和 类 相同,因此您可以重复使用您的代码。