ftp发送文件包括中文名

public void sendwordToftp() {
        try {
            Json json = new Json();
            String fileName = getRequest().getParameter("url");
            fileName = fileName.substring(fileName.indexOf("=") + 1);
            String remotePath = ConfigReadUtil.getInstance().getConfigItem(
                    "FileSavePath")
                    + "/" + fileName;
            List<Ftpconfig> ftpList = ftpManager.getFtpByName("景点预报");
            for (Ftpconfig ftp : ftpList) {
                String ip = ftp.getIp();
                String port = ftp.getPort();
                String username = ftp.getUsername();
                String password = ftp.getPassword();
                String inpath = ftp.getInpath();
                String outpath = ftp.getOutpath();
                boolean flag = connect(inpath, ip, port, username, password);
                if (flag) {
                    if (inpath.startsWith("//")) { // 共享目录
                        List<String> fileNames = TrvalSavedAction
                                .getFileNamesFromSmb("smb:" + remotePath);
                        for (String name : fileNames) {
                            String localFile = "D:/Temp";
                            File f = new File(localFile);
                            if (!f.exists()) {
                                f.mkdirs();
                            }
                            File file = TrvalSavedAction.readFromSmb("smb:"
                                    + remotePath.trim() + name, localFile);
                            upload(file, inpath);
                        }
                    } else {
                        File file = new File(remotePath);
                        upload(file, inpath);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private boolean connect(String inpath, String ip, String port,
            String username, String password) {
        boolean result = false;
        try {
            ftp = new FTPClient();
            int reply;
            int p = Integer.parseInt(port);
            ftp.connect(ip, p);
            ftp.login(username, password);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(inpath);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

/**
     *
     * @param file
     *            上传的文件或文件夹
     * @param localPath
     *            服务器上的路径
     * @throws Exception
     */
    private void upload(File file, String localPath) throws Exception {
        if (file.isDirectory()) {
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file1 = new File(file.getPath() + "\" + files[i]);
                String fileName = file1.getName();
                String filePath = file1.getAbsolutePath();
                if (file1.isDirectory()) {
                    upload(file1, localPath);
                    ftp.changeToParentDirectory();
                } else {
                    File file2 = new File(localPath + "\" + files[i]);
                    FileInputStream input = new FileInputStream(file2);
                    // est新建文本文档.txt
                    // est1.docx
                    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                    String name = new String(file2.getName().getBytes("GB2312"),"ISO-8859-1");
                    ftp.storeFile(file2.getName(), input);
                    input.close();
                }
            }
        } else {
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            String name = new String(file2.getName().getBytes("GB2312"),"ISO-8859-1");
            ftp.storeFile(name, input);
            input.close();
        }
    }

原文地址:https://www.cnblogs.com/guolsblog/p/6409834.html