2016-06-01 150 views
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); 
} 

回答

相關問題