2

我正在使用此代码并尝试从服务器下载 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" );
      }
    }

  }
}

这段代码没有给我任何错误,但是当我打开图像时,它显示我的图像无效。

4

4 回答 4

4

您的图像已损坏,因为 FTPClient 默认使用 TEXT_FILE_TYPE。因此,您必须将文件类型设置为二进制,如下所示:

client.setFileType(FTP.BINARY_FILE_TYPE);
于 2013-01-04T03:52:59.067 回答
2

这个话题是你的解决方案:

http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/

于 2012-06-02T06:25:56.703 回答
0

您既没有刷新输出流,也没有在 finally 块中关闭。试试看。

于 2012-06-02T06:21:47.267 回答
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;

}

于 2019-10-03T07:07:37.513 回答