0
ラップトップからサーバーにファイルをアップロードしようとしたときにエラーが発生しました。Androidの代わりにデスクトップでRestTemplateを使用することはできますか?
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";
}
}
クライアント側のコード: http://mvnrepository.com/artifact/org.springframework/spring-web/
名前:あなたは、ばねウェブライブラリが必要になりますデスクトップ環境にRestTemplateを使用するための
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);
}