如何在浏览器中从Http Request打开流文件 并从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) {
}
答案 0 :(得分:1)
看起来你有一个基于Android的Web服务器,它接受一个带有从桌面Web客户端发布的文件字段的表单。
首先,将表单行更改为:
<form action="insert" enctype="multipart/form-data" method="post">
如果你想提交文件,那里的所有指南都说你需要一个多部分表格(而不是urlencoded);如果不指定enctype
会发生什么,我不确定;浏览器可能只是静静地将上传的内容类型更改为multipart。
要从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
所有其他方法都失败了,你可以写自己的。但在此之前,请尝试整合现成的。