我使用此代码并尝试从服务器下载png图片。这是代码
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.net.ftp.FTPClient;
public class NetTest {
public static void main(String[] args){
FTPClient client = new FTPClient( );
OutputStream outStream;
try {
client.connect( "server" );
client.login("noman@mindzone.co.in", "pass");
String remoteFile = "Pages/page0001.png";
outStream = new FileOutputStream( "C:\\asd.png" );
client.retrieveFile(remoteFile, outStream);
} catch(IOException ioe) {
ioe.printStackTrace();
System.out.println( "Error communicating with FTP server." );
} finally {
try {
client.disconnect( );
} catch (IOException e) {
System.out.println( "Problem disconnecting from FTP server" );
}
}
}
}
此代码没有给我任何错误但是当我打开图像时它会显示无效图像。
答案 0 :(得分:2)
这个主题是你的解决方案:
http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/
答案 1 :(得分:2)
您的图像已损坏,因为FTPClient默认使用TEXT_FILE_TYPE。因此,您必须将文件类型设置为二进制,如下所示:
client.setFileType(FTP.BINARY_FILE_TYPE);
答案 2 :(得分:0)
您既没有刷新输出流也没有在finally块中关闭。试试吧。
答案 3 :(得分:0)
try {
Log.e("downloadFTP login : ", "Success");
OutputStream desFileStream = new BufferedOutputStream( new FileOutputStream(desFilePath));
Log.d(TAG,"FileOutputStream");
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
Log.d(TAG, "4");
desFileStream.close();
Log.e("downloadFTP status : ", "" + status);
Log.d(TAG, "5");
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
return status;
}