0

シナリオは、ftpサーバーからファイルを取得し、添付ファイルとして電子メールで送信することです。接続、ファイル名の成功について「org.apache.commons.net.ftp.FTPClient」を使用してFTPサーバーからファイルを取得したい。しかし、私はファイルに正しく変換する方法がわかりません。

これが私のコードです:

FTPClient ftp = new FTPClient();
byte bytes[]=new byte[1024];
int read = 0;

//connection,filename --success

 StringBuffer str = new StringBuffer("MyFiles");
      str.append("-XYZ-");
      str.append(new SimpleDateFormat("ddMMyyyy").format(new Date()));
      str.append(".pdf");
      System.out.println("getting file --> "+str.toString()); 
 System.out.println("getting stream ftp -->"+ ftp.retrieveFileStream(str.toString()));

InputStream input = ftp.retrieveFileStream(str.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((read = input.read(bytes)) != -1) { --> null pointer line 503
   out.write(bytes,0,read);
 }      
File temp = null;    
temp = new File(str.toString());
Utils.convertByteArrayToFile(temp, out.toByteArray());

public static void convertByteArrayToFile(File outputFile, byte[] inputArray) throws IOException{
    BufferedOutputStream bos=null;
    try{
        FileOutputStream fos=new FileOutputStream(outputFile);
        bos=new BufferedOutputStream(fos);
        bos.write(inputArray);
    }finally{
        if(bos!=null){
            try{
                bos.flush();
                bos.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

そして結果は..

ファイルの取得->MyFiles-XYZ-17012013.pdf

ストリームの取得ftp->org.apache.commons.net.io.SocketInputStream@409db838

com.java.EmailForm.sendEmail(EmailForm.java:503)でのjava.lang.NullPointerException

何か案は ?ありがとうMRizq

4

2 に答える 2

2

メソッドRetrieveFileを使用することをお勧めします。

File localFile = new File("/path/XYZ-17012013.pdf");
FileOutputStream fout = new FileOutputStream(localFile);

boolean success = fTPClient.retrieveFile(ftpServerFilePath, fout);

if (success) {
    fout.flush();
    fout.close();
} else {
    System.out.println("Retrieve failure");
}
于 2013-01-17T14:09:07.077 に答える
0

FTPClientから入力ストリームを取得しているように見えるため、nullポインタの原因にはなりません。私の推測では、バイト配列を初期化するのを忘れていてbytes、それはnullです。また、他の場所に保存する必要がない限り、添付ファイルとして使用する前に、入力ストリームの内容を実際にファイルにダンプする必要がない場合があります。おそらく、メモリ内のバイナリデータから直接作成できます。

于 2013-01-17T13:56:14.707 に答える