有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否将文件上载到URL?

我需要修改我的java类,以允许我成功地将文件上载到其中的URL
我想使用web浏览器的内置文件上载机制

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

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.view.Menu;
import 安卓.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) 个答案

  1. # 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();
        }
    }