如何使用AndroidAnnotation进行休息服务下载mp4文件

时间:2014-03-26 04:47:37

标签: android spring rest android-annotations

我的问题是我不确定如何使用androidannotation rest api下载文件,下面是我的示例代码"

我已经创建了一个安静的服务,如下所示:

@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();

}

}

如果我使用Google Chrome来浏览网址" http:// [hostname]:8080 / mnc-sms-endpoint / files"然后它就可以下载文件了。

现在我想创建一个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();
}

以下是我的机器人活动:

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

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

}

}

1 个答案:

答案 0 :(得分:0)

当您的应用调用WS时错误是什么?

此外,您应该删除@Accept(...)注释,因为您的WS可能会返回与application/octet-stream不同的内容类型(因为此行String mimeType = context.getMimeType(fullPath);