Java 使用隐藏的登录名和密码连接到ftp服务器

Java 使用隐藏的登录名和密码连接到ftp服务器,java,php,android,mysql,ftp,Java,Php,Android,Mysql,Ftp,我需要什么:我需要从android应用程序连接到我的ftp服务器,因为我想从android应用程序将图像上载到此服务器,同时我还想将一些数据(字符串…)上载到此服务器上的mySQL DB 问题是:如果我想连接到ftp,我需要用java代码写我的登录名并传递到我的ftp或mySQL数据库的访问权限,如以下示例所示>,但我不想写我的登录名,传递java代码,因为任何人都可以反转我的应用程序,然后他可以编辑我的数据库atc,这不是一个好方法 问题是:我如何使用或使用什么来进行身份验证 我在使用什么:我

我需要什么:我需要从android应用程序连接到我的ftp服务器,因为我想从android应用程序将图像上载到此服务器,同时我还想将一些数据(字符串…)上载到此服务器上的mySQL DB

问题是:如果我想连接到ftp,我需要用java代码写我的登录名并传递到我的ftp或mySQL数据库的访问权限,如以下示例所示>,但我不想写我的登录名,传递java代码,因为任何人都可以反转我的应用程序,然后他可以编辑我的数据库atc,这不是一个好方法

问题是:我如何使用或使用什么来进行身份验证

我在使用什么:我在使用Apache服务器和php和mySQL数据库


如果有任何帮助,我将不胜感激。

您需要在apache服务器上创建数据源。大多数编程语言都支持使用数据源获取数据库连接。这样,服务器管理员就可以通过应用程序控制数据库连接的详细信息

您可以在此处找到更多信息:-


我有一个raspberry pi作为运行Apache、PHP和MySQL DB的小型服务器运行,用于保存Android设备上运行的GCM应用程序的注册ID。注册设备后,它会将regID发送到数据库

该表称为注册,它有列设备、项目、所有者、注册

我应该强调,Android手机上的以下代码都封装在一个AsyncTask中,并在doInBackground方法中调用

String script =  "register_phone.php";
String serverUrl = SERVER_URL + script;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("devId", devId);
params.put("projId", projId);
params.put("owner", owner);

// some other largely irrelevant stuff
post(serverUrl, params);




private static void post(String endpoint, Map<String, String> params)
            throws IOException {
        String url = null;
        url = endpoint;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();

        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            String key = param.getKey();
            String value = param.getValue();
            nameValuePairs.add(new BasicNameValuePair(key, value));
        }

        HttpResponse response;
        final HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, NETWORK_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, NETWORK_TIMEOUT);
        HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpPost httpPost = new HttpPost(url);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            response = httpClient.execute(httpPost);
             //int status = httpClient.getResponseCode();
            Log.d(GCMTAG, "response " + response.getEntity());
            String responseBody = "empty";
            responseBody = EntityUtils.toString(response.getEntity());
            Log.d(GCMTAG, "responsebody " + responseBody);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e){
            e.printStackTrace();
        }
    }

}
String script=“register\u phone.php”;
字符串serverUrl=服务器\ URL+脚本;
Map params=新的HashMap();
参数put(“regId”,regId);
参数put(“devId”,devId);
参数put(“projId”,projId);
参数put(“所有者”,所有者);
//其他一些基本上无关的东西
post(服务器URL,参数);
私有静态void post(字符串端点、映射参数)
抛出IOException{
字符串url=null;
url=端点;
List nameValuePairs=新的ArrayList();
迭代器迭代器=params.entrySet().Iterator();
while(iterator.hasNext()){
Entry param=iterator.next();
String key=param.getKey();
字符串值=param.getValue();
添加(新的BasicNameValuePair(键,值));
}
HttpResponse响应;
最终HttpParams HttpParams=新的基本HttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,网络超时);
HttpConnectionParams.setSoTimeout(httpParams,网络超时);
HttpConnectionParams.SetStaleCheckEnabled(httpParams,true);
HttpClient HttpClient=新的默认HttpClient(httpParams);
HttpPost HttpPost=新的HttpPost(url);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
response=httpClient.execute(httpPost);
//int status=httpClient.getResponseCode();
Log.d(GCMTAG,“response”+response.getEntity());
字符串responseBody=“empty”;
responseBody=EntityUtils.toString(response.getEntity());
日志d(GCMTAG,“响应库”+响应库);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}
}
}
在服务器上,我有:

<?php
require 'vars.php';
$projid=$_POST['projId'];
$devid=$_POST['devId'];
$owner=$_POST['owner'];
$regid=$_POST['regId'];

//echo $owner;  echo $projid; echo $devid;
$mysqli = new mysqli("localhost", $user, $pwd, $database);

$regidIN = mb_convert_encoding($regid, "UTF-8");
$projectIN = mb_convert_encoding($projid, "UTF-8");
$deviceIN = mb_convert_encoding($devid, "UTF-8");
$ownerIN = mb_convert_encoding($owner, "UTF-8");

$queryone="replace into registrations (device, project, owner, regid)
           values ('$deviceIN', '$projectIN', '$ownerIN', '$regidIN')";

if (!($stmt = $mysqli->prepare( $queryone))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->close();
$mysqli->close();
?>

thx用于提示,但问题是Im使用托管ftp服务器,我无法编辑etc apache服务器。我只能使用DB和php来部分控制服务器。我认为只有在php中,我才能创建一些可能会进行身份验证的服务。你肯定想通过FTP而不是Apache下的HTTP进行传输吗?通过FTP传输是我的想法,我在那里没有任何经验。我欢迎任何其他想法和方式,我可以上传数据和图像到我的服务器。但正如我在下面的评论中所说,我使用的是托管ftp服务器,无法编辑etc apache服务器。我只能使用DB和php来部分控制服务器。我有一个标准的托管域。好的,我将发布一个使用HTTP(Apache默认协议)的缩减示例。可能需要一点时间
<?php
   $user="hidden";
   $pwd="secret";
   $database="also_secret";
?>