Thứ Bảy, 6 tháng 12, 2014

FTPUploader Java

//public class FTPUploader {
//
//    FTPClient ftp = null;
//
//    public FTPUploader(String host, String user, String pwd) throws Exception {
//        ftp = new FTPClient();
//        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
//        int reply;
//        ftp.connect(host);
//        reply = ftp.getReplyCode();
//        if (!FTPReply.isPositiveCompletion(reply)) {
//            ftp.disconnect();
//            throw new Exception("Exception in connecting to FTP Server");
//        }
//        ftp.login(user, pwd);
//        ftp.setFileType(FTP.BINARY_FILE_TYPE);
//        ftp.enterLocalPassiveMode();
//    }
//
//    public void uploadFile(String url, String fileName, String hostDir)
//            throws Exception {
//        InputStream input = new URL(url).openStream();
//        BufferedImage originalImage = ImageIO.read(input);
////        BufferedImage originalImage = ImageIO.read(new File(input));
//        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
////        logger.info(originalImage.getHeight() * Config.THUMB_WIDTH / originalImage.getWidth());
//        BufferedImage resizedImage = new BufferedImage(100, originalImage.getHeight() * 100 / originalImage.getWidth(), type);
//        Graphics2D g = resizedImage.createGraphics();
//        g.drawImage(originalImage, 0, 0, resizedImage.getWidth(), resizedImage.getHeight(), null);
//        g.dispose();
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        ImageIO.write(resizedImage, "jpg", os);
//        InputStream input1 = new ByteArrayInputStream(os.toByteArray());
//        this.ftp.makeDirectory(hostDir);
//        this.ftp.storeFile(hostDir + fileName, input1);
////        try (InputStream input = new FileInputStream(new File("/home/chieu/75x75.png"))) {
////            this.ftp.makeDirectory(hostDir);
////            this.ftp.storeFile(hostDir + fileName, input);
////        }
//    }
//
//    public void disconnect() {
//        if (this.ftp.isConnected()) {
//            try {
//                this.ftp.logout();
//                this.ftp.disconnect();
//            } catch (IOException f) {
//                // do nothing as file is already saved to server
//            }
//        }
//    }
//
//    public static void main(String[] args) throws Exception {
//        System.out.println("Start");
//        FTPUploader ftpUploader = new FTPUploader("ftp.aocuoitphcm.com", "chieu91.H1703@aocuoitphcm.com", "AA51ZeBt351");
//        //FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload
//        // files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
//        ftpUploader.uploadFile("http://s2.img.edn.vn/2013/d/9/d9ab6349753c140dbe6df0c5034d037d_200x200.jpg",
//                "test1.jpg", "/upload/a/");
//        ftpUploader.disconnect();
//        System.out.println("Done");
//    }
//}

Không có nhận xét nào:

Đăng nhận xét