0

how to open stream file from Http Request in browser and transfer from pc to android devices

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) {

}

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • 1
    Please provide more details. Do you have an Android app? Where did HTML come from? Are you really running an HTTP server off an Android device? – Seva Alekseyev Oct 24 '12 at 15:29
  • http://code.google.com/p/krvarma-android-samples/source/browse/trunk/aws/src/com/varma/android/aws/?r=80 I'm getting help from above . But I did not find anything about file transfer . – Ahmad Aghazadeh Oct 24 '12 at 15:37
  • Can you please just answer my questions in 3-4 sentences? I don't feel like going through the whole project, and no one probably does. – Seva Alekseyev Oct 24 '12 at 15:45
  • Please review code and answer my question . – Ahmad Aghazadeh Oct 24 '12 at 16:16

1 Answers1

1

Looks like you have an Android-based Web server that accepts a form with a file field that's being posted from a desktop Web client.

First, change the form line to:

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

All guides out there say that you need a multipart form (as opposed to urlencoded) if you want to submit files; what would happen if don't specify enctype, I'm not sure; the browser might just quietly change the content type of the upload to multipart.

To retrieve the entity data from a HttpRequest object form, do the following:

Entity en = ((BasicHttpEntityEnclosingRequest)request).getEntity();
InputStream ins = en.getContent(); //Lets you read from the entity

The stream is not for the uploaded file - it's for the whole entity. It includes all form fields, of which the file is one.

Now the tricky part starts. Android does not have a built-in parser of multipart forms. There are some free open parsers out there, check out

http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html

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

All else failing, you can write your own. But before you do, try integrating a ready made one.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281