【问题标题】:how to upload file using http put in android?如何在android中使用http上传文件?
【发布时间】:2011-05-20 02:44:40
【问题描述】:

我有一个 REST Web 服务和 android。现在我想使用 android 请求 Http Put 来调用 Web 服务。在我的 REST Web 服务中,如果用户想要执行 Http Put,他可以像这样在终端中请求:

curl -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" -v -T /home/student1/a.jpg http://localhost:8080/user1/folder/a.jpg

我的问题是如何使用 HttpPut 在 android 中设置 -T /home/student1/a.jpg?

【问题讨论】:

    标签: android web-services rest httprequest http-put


    【解决方案1】:

    这里有一些你可以使用的 sn-p:

    File f = new File(...);
    ...
    ...
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPut httpPut = new HttpPut("http://mydomain.com/some/action");
    
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("myFile", new FileBody(f));
    
    httpPut.setEntity(entity);
    HttpResponse response  = httpclient.execute(httpPut);
    

    【讨论】:

    • 顺便说一句,如何从 Android UI 获取 File f=new file(file...) 中的文件路径?
    • Android UI 是什么意思?你的文件在哪里?
    • 文件从本地驱动器附加,然后使用网络服务上传到服务器系统
    • 如果有人可以提供帮助,我正在解决同样的问题并且一切都做得很好,但是当发出请求时,它会抛出异常说“sendto failed: ECONNRESET (Connection reset by peer)”,请救命!!
    • 需要外部 apache jar 文件。没有的解决方案会很好。
    猜你喜欢
    • 2011-07-22
    • 2011-11-02
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2023-03-24
    • 2015-11-22
    • 1970-01-01
    相关资源
    最近更新 更多