シナリオは、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