如何将文件从PC传输到Android设备



如何从浏览器中的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) {

}

看起来您有一个基于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/oryilly/servlet/multipartrequest.html

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

所有其他失败,您都可以写自己的。但是在您这样做之前,请尝试整合一个已准备就绪的。

最新更新