android:AndroidAnnotations上傳檔案,網路介面如此簡潔
網路介面如此簡潔
使用HttpClient進行檔案的上傳,可以參考部落格:使用HttpClient進行檔案上傳
如果項目使用AndroidAnnotation,寫上傳介面就會非常方便,比如之前寫POST介面
首先參考之前的部落格,使用AndroidAnnotations進行POST請求。
如下是使用AndroidAnnotations進行檔案上傳的網路介面
@Rest(rootUrl = "http://192.168.31.183:8080/SSHMySql/", converters = {GsonHttpMessageConverter.class})public interface FileService extends RestClientErrorHandling{ @Post("/upload.action") @Accept(MediaType.APPLICATION_JSON) ReqResult uploadFile(MultiValueMap params);}
調用時傳遞參數,其中”userImage”欄位為伺服器端接收檔案的欄位,desc為另外一個參數,當然,你也可以添加額外的參數。
如下為調用網路介面時的代碼
public ReqResult postUploadFile(String localFilePath,String desc) { try { if (!new File(localFilePath).exists()) { return null; } MultiValueMap params = new LinkedMultiValueMap(); params.add("userImage", new FileSystemResource(localFilePath)); params.add("desc",desc); return fileService.uploadFile(params); } catch (Exception e) { e.printStackTrace(); } return null; }
這裡最需要說明的是“userImage”參數的值為:new FileSystemResource(localFilePath)
Android開發聯盟QQ群:272209595
未經許可,不得用於商業目的