0
我已經在android上創建了http服務。現在我想從瀏覽器上傳文件到服務器(android)。讓我們來看看我做了什麼:android httpservice從瀏覽器上傳文件
private static final String ALL_PATTERN = "*";
private static final String UPLOADFILE_PATTERN = "/UploadFile/*";
/* Some variables */
public WebServer(Context context) {
this.setContext(context);
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(ALL_PATTERN, new HomeCommandHandler(context));
registry.register(UPLOADFILE_PATTERN, new UploadCommandHandler(context));
httpService.setHandlerResolver(registry);
}
然後我寫在瀏覽器的URL(例如http://127.0.0.1:6789/home.html(我用模擬器玩))。 HTTP服務送我形成如下圖所示:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>File Upload</title>
</head>
<body>
<form method="POST" action="UploadFile/" enctype="multipart/form-data">
<p>File1 Test:
<input type="file" name="myfile1" size="20">
<input type="submit" value="Upload file">
<input type="reset" value="Reset" name="someName">
</form>
</body>
我選擇一些文件,然後按提交。在此之後,服務器調用此方法:
@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext httpContext) throws HttpException, IOException {
Log.e("","INSIDE UPLOADER");
Log.e("Method",request.getRequestLine().getMethod());
Log.e("len",request.getRequestLine()+"");
for(Header h : request.getAllHeaders()){
Log.e("len", h.getName()+" = "+h.getValue());
}
}
它返回的logcat:
Content-Length = 4165941
Content-Type = multipart/form-data; boundary=----WebKitFormBoundarykvmpGbpMd6NM1Lbk
Method POST /UploadFile/ HTTP/1.1
等參數。 我的問題是我可以在哪裏獲得文件內容?我的意思是一些InputStream或其他東西。我知道HttpResponse的方法就像getContent()。但HttpRequest沒有這個。 謝謝。
謝謝。我明天會試試! – Nolesh 2012-03-06 14:19:25