【问题标题】:how to use AndroidAnnotation for rest service to download a mp4 file如何使用 AndroidAnnotation for rest service 下载 mp4 文件
【发布时间】:2014-03-26 04:47:36
【问题描述】:

我的问题是我不确定如何使用 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();

}

}

如果我使用谷歌浏览器浏览 URL“http://[hostname]:8080/mnc-sms-endpoint/files”然后它就可以下载文件了。

现在我想创建一个 android 应用来从这个 restful 服务中获取文件。

以下是我的 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();

}

}

【问题讨论】:

    标签: android spring rest android-annotations


    【解决方案1】:

    你的应用调用WS时出现什么错误?

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

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 2020-04-24
      • 2014-02-18
      • 1970-01-01
      • 2012-12-31
      • 2020-02-05
      • 1970-01-01
      • 2019-01-08
      相关资源
      最近更新 更多