wuxw
2020-02-26 e60f89f66d9a1354c1776dec2bc085c51f62b923
java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java
@@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.util.UUID;
@@ -25,6 +26,59 @@
    private static String SERVER_CHARSET = "ISO-8859-1";
    private final static String localpath = "F:/";//下载到F盘下
    private final static String fileSeparator = System.getProperty("file.separator");
    private final static String DEFAULT_IMG_SUFFIX = ".jpg";
    private final static String IMAGE_DEFAULT_PATH = "img/";
    /*
     *图片上传工具方法
     * 默认上传至 img 文件下的当前日期下
     */
    public static String upload(String imageBase64, String server, int port,
                                String userName, String userPassword, String ftpPath) {
        String fileName = "";
        try {
            // request.setCharacterEncoding("utf-8");
            ftpClient.connect(server, port);
            ftpClient.login(userName, userPassword);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpPath = ftpPath + IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/";
            mkDir(ftpPath);// 创建目录
            // 设置上传目录 must
            ftpClient.changeWorkingDirectory(ftpPath);
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
                LOCAL_CHARSET = "UTF-8";
            }
            fileName = UUID.randomUUID().toString() + DEFAULT_IMG_SUFFIX;
            FTPFile[] fs = ftpClient.listFiles(fileName);
            if (fs.length == 0) {
                System.out.println("this file not exist ftp");
            } else if (fs.length == 1) {
                System.out.println("this file exist ftp");
                ftpClient.deleteFile(fs[0].getName());
            }
            ByteArrayInputStream is = new ByteArrayInputStream(Base64Convert.base64ToByte(imageBase64));
            boolean saveFlag = ftpClient.storeFile(fileName, is);
            is.close();
            if (!saveFlag) {
                throw new IllegalArgumentException("存储文件失败");
            }
        } catch (Exception e) {
            logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("关闭ftpClient 失败", e);
            }
        }
        return IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName;
    }
    /*
     *文件上传工具方法
@@ -76,17 +130,19 @@
    /*
     *文件下载工具方法
     */
    public static byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) throws Exception {
        ftpClient.connect(server, port);
        ftpClient.login(userName, userPassword);
        ftpClient.enterLocalPassiveMode();
        if (remotePath != null && !remotePath.equals("")) {
            ftpClient.changeWorkingDirectory(remotePath);
            System.out.println("file success");
        }
    public static byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
        byte[] return_arraybyte = null;
        if (ftpClient != null) {
            try {
        try {
            ftpClient.connect(server, port);
            ftpClient.login(userName, userPassword);
            ftpClient.enterLocalPassiveMode();
            if (remotePath != null && !remotePath.equals("")) {
                ftpClient.changeWorkingDirectory(remotePath);
                System.out.println("file success");
            }
            if (ftpClient != null) {
                FTPFile[] files = ftpClient.listFiles();
                for (FTPFile file : files) {
                    String f = new String(file.getName().getBytes("iso-8859-1"), "utf-8");//防止乱码
@@ -111,11 +167,14 @@
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeConnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("从ftp读取文件失败", e);
        } finally {
            closeConnect();
        }
        return return_arraybyte;
    }