FTP upload files name Chinese garbage problem

Studied this question yesterday afternoon, that is resolved, suddenly discovered this morning, in fact, does not address, so after a morning spent time and access to relevant information is now considered completely resolved. To note: Yesterday I put a simple filename turn into new String (filename.getBytes ( "GBK"), "ISO-8859-1"), did find after uploading the Chinese are no longer garbled, so I thought I had solved this problem , but later I found that if you set ftp server is already encoding utf-8's words will continue to error, so I went online to find some information and found a command to set the ftp support UTF-8, is this ftpClient. sendCommand ( "OPTS UTF8", "oN"), so we tried it and the file name so new String (filename.getBytes (LOCAL_CHARSET), SERVER_CHARSET), Well, this time to solve the garbage problem, here's the code:

FTPFileUtil class {public
// This is the FTP profile information
static the Map <String, String> EIP = UtilProperties.getPropretiesByKey ();
// native character encoding
static String LOCAL_CHARSET = "GBK";

// inside the FTP protocol, a predetermined file name 8859-1-encoded as ISO
static String SERVER_CHARSET = "ISO-8859-1";

public static FTPClient ftpClient = null;

/ ***
* initialize ftp server
* @author panfei
* @date 2018 Nian 7 Yue 18 Ri
* /
public void initFtpClient static () {
ftpClient FTPClient new new = ();
the try {
ICFLoggerUtils.info ( "FTP server Connecting ...:" + eip.get ( "ftp.username" ) + ":" + eip.get ( "ftp .port "));
// ftp server connected
ftpClient.connect (eip.get (" ftp.uploadpath ") , Integer.valueOf(eip.get("ftp.port")));
// Log in ftp server
ftpClient.login (eip.get ( "ftp.username"), eip.get ( "ftp.password"));
// successful login server
int replyCode = ftpClient.getReplyCode ();
IF (! FTPReply.isPositiveCompletion (replyCode)) {
ICFLoggerUtils.info ( "Connect failed ... FTP server:" + eip.get ( "ftp.username" ) + ":" + eip.get ( "ftp.port"));
}
ICFLoggerUtils.info ( "Connect Success ... FTP server:" + eip.get ( "ftp.username") + ":" + eip.get ( "ftp.port"));
// open server UTF- support 8, if the server supports UTF8 encoding to use, otherwise using local coding (GBK).
IF (FTPReply.isPositiveCompletion (ftpClient.sendCommand ( "OPTS UTF8", "the oN"))) {
LOCAL_CHARSET = "UTF . 8 ";
}
ftpClient.setControlEncoding (LOCAL_CHARSET);
}catch (MalformedURLException e) {
e.printStackTrace();
the catch} (IOException E) {
e.printStackTrace ();
}
}

/ *****
* Upload ftp server
* @param pathname FTP server save directory
* @param fis stream
file name to be deleted * @param filename
* @return
@author panfei *
* @date 2018 Nian 7 Yue 18 Ri
* /
public static boolean CreateFile (String pathname, FIS InputStream, String filename) {
boolean Flag = false;
the try {
ICFLoggerUtils.info ( "start upload file");
initFtpClient ( );
ftpClient.makeDirectory (pathname);
ftpClient.enterLocalPassiveMode ();
ftpClient.changeWorkingDirectory (pathname);
ftpClient.setFileType (FTPClient.BINARY_FILE_TYPE);
= ftpClient.storeFile In Flag (new new String (filename.getBytes (LOCAL_CHARSET),
SERVER_CHARSET), FIS);
IF (In Flag) {
ICFLoggerUtils.info ( "file uploaded successfully!");
} the else {
ICFLoggerUtils.info ( "no file upload success! ");
}
fis.close ();
ftpClient.logout ();
} the catch (Exception E1) {
ICFLoggerUtils.info (" failed to upload file ");
e1.printStackTrace ();
} a finally {
IF (ftpClient.isConnected ()) {
the try {
ftpClient.disconnect ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
IF (FIS = null) {!
the try {
fis.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
}
Return Flag;
}

/ ***
* Delete Files
* @param pathname FTP server save directory *
* @param filename the file name to be deleted *
* @return
* @author panfei
* @date 2018 Nian 7 Yue 18 Ri
* /
public boolean deleteFile static (String pathname, String filename) {
boolean Flag = false;
the try {
ICFLoggerUtils.info ( "start deleting files");
initFtpClient ();
ftpClient.enterLocalPassiveMode ();
// switch the FTP directory
ftpClient.changeWorkingDirectory (pathname) ;
Flag = ftpClient.deleteFile (new new String (filename.getBytes (LOCAL_CHARSET),
SERVER_CHARSET));
ftpClient.logout ();
IF (Flag) {
ICFLoggerUtils.info ( "delete files success!");
}else{
ICFLoggerUtils.info("删除文件失败!");
}
} catch (Exception e) {
ICFLoggerUtils.info("删除文件失败");
e.printStackTrace();
} finally {
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
return flag;
}


public static void main(String[] args) throws IOException {
//InputStream inputStream = new FileInputStream("C:/Users/icfjk888/Workspaces/ICFInterfaceSrever-3.0/src/main/webapp/download/木兰.zip");
//createFile("tempDownload",inputStream,"木兰.zip");
//deleteFile("tempDownload", "木兰.zip");
}
}

Original link: https: //blog.csdn.net/pan_fei/article/details/81109635

Guess you like

Origin www.cnblogs.com/axin85/p/11741576.html