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

デスクトップWebクライアントから投稿されているファイルフィールドを持つフォームを受け入れるAndroidベースの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

ストリームはアップロードされたファイル用ではなく、エンティティ全体用です。これには、ファイルが1つであるすべてのフォームフィールドが含まれます。

ここで、注意が必要な部分が始まります。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 に答える