gpt4 book ai didi

java - 如何在android中通过url从ftp服务器下载图像/文件?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:20 36 4
gpt4 key购买 nike

   private String ftpUrl = "ftp://%s:%s@%s/%s;";
private static String host = "www.krishnas.com.np";
private static String user = "tryagn@krishnas.com.np";
private static String pass = "technology_krixnas";
private static String filePath = "112.jpg";

private static String savePath =
Environment.getExternalStorageDirectory() + "/" +
Environment.DIRECTORY_DCIM +"/abe.jpg";

我在 asynctask 中的 doInBackground() 是:

   protected String doInBackground(String... f_url) {
int count;
try {

ftpUrl = String.format(ftpUrl, user, pass, host, filePath);


//System.out.println("URL: " + ftpUrl)
Log.d("-----------", ""+ftpUrl);

// try {
//URL url = new URL(ftpUrl);
//URLConnection conn = url.openConnection();
// InputStream inputStream = conn.getInputStream();

URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();

// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);

// Output stream
OutputStream output = new FileOutputStream(savePath);

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
i=(int)((total*100)/lenghtOfFile);
publishProgress(""+(int)((total*100)/lenghtOfFile));

// i = (int)((total*100)/lenghtOfFile);

// writing data to file
output.write(data, 0, count);
}

// flushing output
output.flush();

// c
// losing streams
output.close();
input.close();

} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}

return null;
}

我想从我的 FTP 服务器下载文件/图像,它在链接中显示图像,但不下载到内部存储中。我正在学习ftp服务器下载器而不是http请求!不知道哪里出了问题?

最佳答案

您正在使用外部存储来存储文件Environment.getExternalStorageDirectory()

因此,如果您想将其存储在内部存储中,您可以使用 getFilesDir()getCacheDir():

File file = new File(context.getFilesDir(), filename);

欲了解更多信息,请参阅 this链接:

关于java - 如何在android中通过url从ftp服务器下载图像/文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44432548/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com