java-通过thumbnailator库重新调整ftp上的图像

时间:2016-02-04 10:30:21

标签: java image ftp

我是通过此代码从ftp上传网址的图片。 图片上传成功但当我尝试调整上传图片的大小时会出现以下错误。

String imageUrl = "http://www.totaldesign.ir/wp-content/uploads/2014/11/hamayesh.jpg";
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftp_server, ftp_port);
ftpClient.login(ftp_user, ftp_pass);

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();

// APPROACH #2: uploads second file using an OutputStream
URL url = new URL(imageUrl);

//**************select new name for image***********/
//get file extention
File file = new File(imageUrl);
String ext = getFileExtension(file);

//get file name from url
String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1, imageUrl.length());
String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.'));

//create new image name for upload
String img_name = "simjin.com_" + fileNameWithoutExtn;

//get current year time for image upload dir
Date date = new Date();
DateFormat yeae = new SimpleDateFormat("yyyy");
String current_year = yeae.format(date);

//create dirs if not exist
ftpClient.changeWorkingDirectory(current_year);
int dirCode = ftpClient.getReplyCode();

if (dirCode == 550) {
    //create dir
    ftpClient.makeDirectory(current_year);

    System.out.println("created folder: " + current_year);
}

//get current month time for image upload dir
DateFormat month = new SimpleDateFormat("MM");
String current_month = month.format(date);
//create dirs if not exist
ftpClient.changeWorkingDirectory("/" + current_year + "/" + current_month);
dirCode = ftpClient.getReplyCode();

if (dirCode == 550) {
    //create dir
    ftpClient.makeDirectory("/" + current_year + "/" + current_month);

    System.out.println("created folder: " + "/" + current_year + "/" + current_month);
}

String uploadDir = "/" + current_year + "/" + current_month;

//rename image file if exist
boolean exists;

String filePath = uploadDir + "/" + img_name + "." + ext;
exists = checkFileExists(filePath);

System.out.println("old file path=> " + exists);

//Rename file if exist
int i = 0;
while (exists) {
    i++;
    img_name = "simjin.com_" + fileNameWithoutExtn + i;
    filePath = uploadDir + "/" + img_name + "." + ext;
    exists = checkFileExists(filePath);
    //break loop if file dont exist
    if (!exists) {
        break;
    }
}
System.out.println("new file path=> " + filePath);

//set image name in array for return
result[0] = img_name;
//*************end select new name for image**********/

System.out.println("ftpClinet Replay Code=> " + ftpClient.getStatus());
//Start uploading second file

InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = ftpClient.storeFileStream(filePath);

System.out.println("outputStream Status=> " + outputStream);

byte[] bytesIn = new byte[10240];
int read = 0;

while ((read = inputStream.read(bytesIn)) != -1) {
    outputStream.write(bytesIn, 0, read);
}

inputStream.close();
outputStream.close();

boolean completed = ftpClient.completePendingCommand();

成功上传后。我想通过thumbnailator调整图片大小:

if (completed) {
   System.out.println("The file is uploaded successfully.");
   String new_img_name = uploadDir + "/" + img_name + "-150x150" + "." + ext;
   OutputStream os = ftpClient.storeFileStream(filePath);
   Thumbnails.of(image_url).size(150, 150).toOutputStream(os);
}
本节中的

得到此错误: OutputStream cannot be null

我哪里错了?以及如何解决它?

0 个答案:

没有答案