当前位置: 代码迷 >> Android >> 从FTP下载的图片变为这样了
  详细解决方案

从FTP下载的图片变为这样了

热度:102   发布时间:2016-04-28 07:16:08.0
从FTP下载的图片变成这样了

下载的线程:
Runnable run = new Runnable() {
public void run() {
ContinueFTP ftp = new PhotoPre.ContinueFTP();
try {
ftp.connect("125.88.22.107", 21, "system", "&UJ");
List<String> path = new ArrayList<String>();
path = ftp.getListFiles(ftpPath);
System.out.println("-path-" + ftpPath + path);
StringBuffer sb = new StringBuffer();
for (String str : path) {
Log.v(TAG, str);
System.out.println("--" + ftpPath + str);
ftp.download(ftpPath + str, "/mnt/sdcard/");
sb.append(str);
}
if (local != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(local + ".jpg",
options);
Log.v(TAG, bm.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] target = new byte[4];
target = baos.toByteArray();
Message message = Message.obtain();
message.arg1 = 1;
message.obj = target;
messageHandler.sendMessage(message);
} else {
System.out.println("faultsable");

}
} catch (Exception e) {
e.printStackTrace();
}
}
};

这个是download的方法:
public DownloadStatus download(String remote, String local)
throws IOException {
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;
FTPFile[] files = ftpClient.listFiles(new String(remote
.getBytes("UTF-8"), "iso-8859-1"));

if (files.length != 1) {
System.out.println("远程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}
long lRemoteSize = files[0].getSize();

List<String> Path = new ArrayList<String>();
Path = getListFiles(ftpPath);
String[] filename = new String[Path.size()];
for (int i = 0; i < Path.size(); i++) {
filename[i] = Path.get(i);
}
String path = Environment.getExternalStorageDirectory() + "/"
+ filename;
File f = new File(path);
f.getParentFile().mkdir();
if (f.exists()) {
long localSize = f.length();
if (localSize >= lRemoteSize) {
System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}
// FileOutputStream out = new FileOutputStream(
// Environment.getExternalStorageDirectory() + "/"
// + filename, true);
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("UTF-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
process = localSize / step;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0)
System.out.println("下载进度:" + process);
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
result = DownloadStatus.Download_From_Break_Success;
} else {
result = DownloadStatus.Download_From_Break_Failed;
}
} else {
OutputStream out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("UTF-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
long localSize = 0L;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0)
System.out.println("下载进度:" + process);
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if (upNewStatus) {
  相关解决方案