2014-03-26 62 views
0

我的問題是我不知道如何將用戶androidannotation REST API下載的文件,下面是我的示例代碼」如何使用AndroidAnnotation休息服務下載mp4文件

我創建了一個RESTful服務如下圖所示:

@Controller 
@RequestMapping("/") 
public class BaseController { 
    private String filePath = "web-inf/scheduler/downloadList.properties"; 
private static final int BUFFER_SIZE = 4096; 

@RequestMapping(value = "/files", method = RequestMethod.GET) 
public void getLogFile(HttpServletRequest request, HttpServletResponse response) throws Exception{ 
    // get absolute path of the application 

    ServletContext context = request.getSession().getServletContext(); 
    String appPath = context.getRealPath(""); 
    System.out.println("appPath = " + appPath); 

    // construct the complete absolute path of the file 
    String fullPath = appPath + filePath; 
    File downloadFile = new File(fullPath); 
    FileInputStream inputStream = new FileInputStream(downloadFile); 

    // get MIME type of the file 
    String mimeType = context.getMimeType(fullPath); 
    if (mimeType == null) { 
     // set to binary type if MIME mapping not found 
     mimeType = "application/octet-stream"; 
    } 
    System.out.println("MIME type: " + mimeType); 

    // set content attributes for the response 
    response.setContentType(mimeType); 
    response.setContentLength((int) downloadFile.length()); 

    // set headers for the response 
    String headerKey = "Content-Disposition"; 
    String headerValue = String.format("attachment; filename=\"%s\"", 
      downloadFile.getName()); 
    response.setHeader(headerKey, headerValue); 

    // get output stream of the response 
    OutputStream outStream = response.getOutputStream(); 

    byte[] buffer = new byte[BUFFER_SIZE]; 
    int bytesRead = -1; 

    // write bytes read from the input stream into the output stream 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     outStream.write(buffer, 0, bytesRead); 
    } 

    inputStream.close(); 
    outStream.close(); 

} 

} 

如果我使用谷歌瀏覽器到瀏覽器的URL的 「http:// [主機名]:8080/MNC-SMS-端點/文件」,那麼它能夠下載文件

。現在我想創建一個Android應用程序來從這個寧靜的服務獲取文件。

下面是我的android代碼:但它保持顯示我的錯誤,實際上我是新的androidannotation和spring-android。

@Rest(converters = { ByteArrayHttpMessageConverter.class }) 
public interface MainRestClient extends RestClientHeaders { 
    // url variables are mapped to method parameter names. 
    @Get("http://192.168.1.37:8080/mnc-sms-endpoint/files") 
    @Accept(MediaType.APPLICATION_OCTET_STREAM) 
    byte[] getEvents(); 
} 

以下是我的Android activiy:

@EActivity(R.layout.activity_main) 
public class MainActivity extends Activity { 
     @RestService 
     MainRestClient mainRestClient; 

@AfterViews 
    protected void init() { 
     mainRestClient.getEvents(); 

}

} 

回答

0

有什麼錯誤時,你的應用程序調用的WS?

你也應該刪除@Accept(...)註解,因爲你可能WS比application/octet-stream返回不同的內容類型

(因爲該行 String mimeType = context.getMimeType(fullPath);的)