将文件上传到URL?

时间:2014-02-02 14:54:06

标签: java android

我需要修改我的java类,以便我能够成功地将文件上传到URL内 我想使用网络浏览器的内置文件上传机制。

这是我到目前为止所做的: -

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web=(WebView)findViewById(R.id.web);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://encodable.com/uploaddemo/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:2)

使用此课程上传您的文件...希望这对您有所帮助

public class TryFile {
    public static void main(String[] ar)
           throws HttpException, IOException, URISyntaxException {
        TryFile t = new TryFile();
        t.method();
    }
    public void method() throws HttpException, IOException, URISyntaxException {
        String url = "http://encodable.com/uploaddemo/";
        String fileName = ""; //file name to be uploaded
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        FileBody fileContent = new FiSystem.out.println("hello");
        StringBody comment = new StringBody("Filename: " + fileName);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", fileContent);
        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
    }
}

答案 1 :(得分:1)

我相信你要做的不是Android平台对你的期望。您必须使用Android API上传您的内容。您可以在此主题中找到一些有用的信息:Android: upload file with filling out POST body together或其他类似的信息。