上传Android文件

时间:2011-11-15 08:02:42

标签: android file-upload

我是Android新手。我想通过wifi和gprs上传文件,但问题是如何在Android中发送多部分数据。

3 个答案:

答案 0 :(得分:1)

            // Enable HTTP parameters
            HttpParams sslparams = new BasicHttpParams();
            HttpProtocolParams.setVersion(sslparams, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(sslparams, HTTP.UTF_8);

            // Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object.
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https",sslFactory, 443));

            // Create a new connection manager using the newly created registry and then create a new HTTP client
            // using this connection manager
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(sslparams, registry);
            httpclient = new DefaultHttpClient(ccm,sslparams);

            HttpPost httppost = new HttpPost(url);
            /*httppost.setEntity(new UrlEncodedFormEntity(nameValue));
            httppost.setEntity(new InputStreamEntity(new FileInputStream(file), file.length()));
            httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");*/
            MultipartEntity entity = new MultipartEntity();
            //entity.addPart("fileName",);
            entity.addPart("file", new InputStreamBody(new FileInputStream(file), ContentType.APPLICATION_OCTET_STREAM));
            entity.addPart("UserID", new StringBody(parameter.getUsreId()));
            entity.addPart("UserName",new StringBody(parameter.getUserName()));
            entity.addPart("HostName", new StringBody(parameter.getHostName()));
            entity.addPart("ProtectionType", new StringBody(parameter.getProtectionType()));
            entity.addPart("FolderPath", new StringBody(parameter.getFolderPath()));
            entity.addPart("osid", new StringBody(parameter.getOsId()));
            //entity.addPart("file", new FileBody(file,ContentType.MULTIPART_FORM_DATA));
            httppost.setEntity(entity);
            //httppost.setEntity(new StringEntity(file));
            //httppost.setHeader("Content-Type", "text/xml");

            HttpParams httpparams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpparams, 15000);
            HttpConnectionParams.setSoTimeout(httpparams, 30000);
            httppost.setParams(httpparams);
            HttpResponse response = httpclient.execute(httppost);

答案 1 :(得分:0)

您应该详细了解link。这是类似的问题。

答案 2 :(得分:0)

创建HttpConnection并在服务器上传递流。请遵循此link