URLConnectionを使用してFTPサーバーにファイルをアップロードしようとしています。例外はありません。私はサーバーに接続できるようです。私はそれを(手動で)(FTPクライアントを使って)アップロードすれば、問題なくファイルをダウンロードすることができます。URLConnectionに接続しているBufferedOutputStreamに書き込めません。
私はBufferedOutputStream.write(int)を使用します。ログは、ローカルファイル( "Hej hej"を含む)を読み取り、その中のバイトにアクセスできることを示しています。問題は、サーバーファイルに何も書き込まれていないことです。サーバー上のファイルを読むと、アップロード前のコンテンツがまだ含まれています。ファイルは更新されません。私はBufferedOutputStream.write(String.getBytes())を試したが、それは役に立たなかった。
static public void upload(String string, String file) {
File file_ = new File(Environment.getExternalStorageDirectory() + "/Android/data/org.sionlinkoping.pingstprayer/files/thanks.txt");
try {
upload("ftphome.mah.se", user, passwd, "thanks.txt", file_);
}
catch(Exception e) { Log.d("",e.toString()); }
}
static public void upload(String ftpServer, String user, String password,
String fileName, File source) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && source != null)
{
StringBuffer sb = new StringBuffer("ftp://");
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append(user);
sb.append(':');
sb.append(password);
sb.append('@');
}
sb.append(ftpServer);
sb.append('/');
sb.append(fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL(sb.toString());
URLConnection urlc = url.openConnection();
bos = new BufferedOutputStream(urlc.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(source));
int i;
// read byte by byte until end of stream
while ((i = bis.read()) != -1)
{
Log.d("","i:"+i);
bos.write(i);
}
bos.flush();
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println("Input not available.");
}
}
ログを示しています。
01-07 14:00:50.662: DEBUG/(6925): i:239
01-07 14:00:50.662: DEBUG/(6925): i:187
01-07 14:00:50.662: DEBUG/(6925): i:191
01-07 14:00:50.662: DEBUG/(6925): i:72
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.662: DEBUG/(6925): i:32
01-07 14:00:50.662: DEBUG/(6925): i:104
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.672: DEBUG/(6925): i:10
を参照してください。ただし、ファイルはすでに存在しています。私はちょうどそれに追加したいです。ライブラリが必要ですか? – JonatanEkstedt
FTPを使って何かをするには、ライブラリが必要です(または、あなたがマゾキストの場合は、FTPプロトコル仕様を利用して独自に書き込みます)。 –
ああ!それはJava固有の事実ですか?私が開発しているこのプログラムはAndroidアプリであるはずです。私はObjective-Cの以前のiPhone用の同じアプリを書いていましたが、余分なライブラリは必要ありませんでしたか?たぶんObjective-Cにはその標準的な機能がありますか? – JonatanEkstedt