2012-10-24 50 views
-1

如何从HTTP请求的浏览器 ,并从PC传送打开流文件,以Android设备如何将文件从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) { 

}

+1

请提供更多详情。你有Android应用程序吗? HTML从哪里来?你真的在使用Android设备运行HTTP服务器吗? –

+0

http://code.google.com/p/krvarma-android-samples/source/browse/trunk/aws/src/com/varma/android/aws/?r=80 我从上面获得帮助。 但我没有发现任何关于文件传输的内容。 –

+0

你能用3-4句话回答我的问题吗?我并不想完成整个项目,也没有人会这样做。 –

回答

1

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

首先,更改表格行:

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

所有导游在那里说,你需要一个多形式(而不是url编码),如果你要提交的文件;如果没有指定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

所有其他失败,你可以写你自己的。但在你做之前,尝试整合一个现成的。

相关问题