0

如何在浏览器中从 Http 请求打开流文件并从 pc 传输到 android 设备

public class WebServer extends Thread {
        private static final String SERVER_NAME = "AndWebServer";

        private static final String MESSAGE_PATTERN = "/message*";
       public WebServer(Context context, NotificationManager notifyManager){
                super(SERVER_NAME);

                this.setContext(context);
                this.setNotifyManager(notifyManager);

                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

                serverPort = Integer.parseInt(pref.getString(Constants.PREF_SERVER_PORT, "" + Constants.DEFAULT_SERVER_PORT));
                httpproc = new BasicHttpProcessor();
                httpContext = new BasicHttpContext();

        httpproc.addInterceptor(new ResponseDate());
        httpproc.addInterceptor(new ResponseServer());
        httpproc.addInterceptor(new ResponseContent());
        httpproc.addInterceptor(new ResponseConnControl());

        httpService = new HttpService(httpproc, 
                                                                                new DefaultConnectionReuseStrategy(),
                                                                                new DefaultHttpResponseFactory());


        registry = new HttpRequestHandlerRegistry();

        registry.register(MESSAGE_PATTERN, new MessageCommandHandler(context,  
        httpService.setHandlerResolver(registry);
        }

        @Override
        public void run() { ...   }

}


public MessageCommandHandler(Context context, NotificationManager notifyManager){
            this.context = context;
            this.notifyManager = notifyManager;


    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
            String uriString = request.getRequestLine().getUri();
            Uri uri = Uri.parse(uriString);
            String message = URLDecoder.decode(uri.getQueryParameter("msg"));
            // get open stearm file and save 
            AppLog.logString("Message URI: " + uriString);

            displayMessage(message);

            HttpEntity entity = new EntityTemplate(new ContentProducer() {....
            }
    });

            response.setHeader("Content-Type", "text/html");
            response.setEntity(entity);
    }

    protected void displayMessage(final String message) {

}

4

1 回答 1

1

看起来您有一个基于 Android 的 Web 服务器,它接受从桌面 Web 客户端发布的带有文件字段的表单。

首先,将表格行更改为:

<form action="insert" enctype="multipart/form-data" method="post">

那里的所有指南都说如果您想提交文件,您需要一个多部分表单(而不是 urlencoded);如果不指定会发生什么enctype,我不确定;浏览器可能只是悄悄地将上传的内容类型更改为多部分。

要从HttpRequest对象表单中检索实体数据,请执行以下操作:

Entity en = ((BasicHttpEntityEnclosingRequest)request).getEntity();
InputStream ins = en.getContent(); //Lets you read from the entity

该流不适用于上传的文件 - 它适用于整个实体。它包括所有表单域,文件就是其中之一。

现在棘手的部分开始了。Android 没有内置的多部分表单解析器。那里有一些免费的开放解析器,请查看

http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html

还有http://blog.kieranties.com/2012/03/multipart-form-posting-in-android.html

其他都失败了,你可以自己写。但在此之前,请尝试集成一个现成的。

于 2012-10-24T17:01:53.900 回答