首页 > 代码库 > 文件上传的操作

文件上传的操作

使用第三方utils做文件上传。
            
    public void fileupload(View v){
        try{
        EditText et_filepath = (EditText) findViewById(R.id.et_filepath);
        //获取输入的文件地址
        String filepath = et_filepath.getText().toString().trim();
        
        //使用开源Utils做上传操作
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        
        RequestParams params = new RequestParams();
        params.put("filename", new File(filepath));
        //url : 请求服务器的url
        asyncHttpClient.post("http://192.168.13.83:8080/itheima74/servlet/UploaderServlet", params, new AsyncHttpResponseHandler() {
            
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(statusCode == 200){
                    Toast.makeText(MainActivity.this, "上传成功", 0).show();
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers,
                    byte[] responseBody, Throwable error) {
            }
        });
        
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

文件上传的操作