Use class:
Org.apache.commons.net.ftp.FTPClient
Problem Description:
When creating Chinese catalogs and uploading Chinese filenames, the Chinese characters in the directory name and file name are garbled.
Workaround:
Google on the Internet some information, FTP protocol, the specified file name is encoded as iso-8859-1, so the directory name or file name need to transcode.
So the solution for many people on the Web is:
Convert the Chinese directory or file name to iso-8859-1 encoded characters. Reference code:
String name= "directory name or file name";
Name=new String (name.getbytes ("GBK"), "iso-8859-1");
Many people to the above operation, found that after uploading Chinese is no longer garbled, thought to solve the problem
There are other ways to deal with this:
Ftpclient.setcontrolencoding ("GBK");
Ftpclientconfig conf = new Ftpclientconfig (FTPCLIENTCONFIG.SYST_NT);
Conf.setserverlanguagecode ("zh");
The above treatment method, I have tried, found in my own set up FTP server, upload the file Chinese is normal, not garbled, I thought the Chinese problem solved, but the test department of the colleagues in the file upload to their built FTP server, the document Chinese path is still garbled, So the above workaround is wrong.
The reason for the error of the above method is because the encoding format of the FTP server is not considered. I built the FTP server (windows2003 server) Support GBK encoding, so the above solution can, but the test department of the colleagues to build the FTP server (SERV-U) is supported UTF-8 format, so at this time the client encoding is GBK, And the installation of the FTP server settings are already utf-8 encoding, so there is no doubt that garbled problems.
So what is the correct solution, we can imitate FlashFXP, FileZilla and other FTP client connection tool to see how they are implemented, the following two graphs are FileZilla configuration information and the command information when connected.
Figure 1:filezilla Configuration information
Figure 2:filezilla Connection Information
we can see from Figure 2 that it sent the OPTs UTF8 on command to the server to turn on the server support for UTF-8. So we can also send this command to the server in the same way as FileZilla . If the server supports UTF-8 we use UTTF-8, otherwise we will use local code (GBK) to handle the Chinese file name.
Here is the Java code:
/** Local character encoding */
private static String Local_charset = "GBK";
FTP protocol, the specified file name is encoded as Iso-8859-1
private static String Server_charset = "Iso-8859-1";
private void Connectftpserver () {
if (ftpclient = = null) {
FtpClient = new FtpClient ();
}
if (ftpclient.isconnected ()) {
Return
}
String host = Getconfigvalue (ADDRESS);
int port = integer.valueof (Getconfigvalue (port));
String user = Getconfigvalue (user);
String password = getconfigvalue (password);
try {
Ftpclient.connect (host, Port);
if (Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {
if (ftpclient.login (user, password)) {
if (Ftpreply.ispositivecompletion (Ftpclient.sendcommand (
"OPTS UTF8", "on")) {//Turn on the server's support for UTF-8, and if the server supports it with UTF-8 encoding, use local encoding (GBK).
Local_charset = "UTF-8";
}
Ftpclient.setcontrolencoding (Local_charset);
Ftpclient.enterlocalpassivemode ();//Set Passive mode
Ftpclient.setfiletype (Gettransformodule ());//Set the mode of transmission
Return
} else {
throw New Filestorageexception (
"Connet Ftpserver error! Please check user or password ");
}
}
} catch (IOException e) {
Disconnectserver ();
throw New Filestorageexception (
"Connet Ftpserver error! Please check the Configuration ");
}
}
When uploading a file, the file name needs to be encoded for conversion
FileName = new String (Filename.getbytes (Local_charset),
Server_charset);
Through the above method, it can solve the problem of Chinese garbled-----------------------------------------------------------------------------above for reprint, Source: http ://www.linuxidc.com/linux/2014-10/107585.htm-----------------
servlet+jsp use FileZilla Server server to complete file upload and download.
Places to note: Upload: 1. Background to get the file path selected by the foreground JSP page (including Chinese), the path is encoded
Part of the code String FilePath = Request.getparameter ("FileName");
String name = filepath.substring (filepath.lastindexof ("\ \") +1);//Get file name
System.out.println (name+ "-----------" +filepath);
Set the encoding format for Chinese files, parse correctly
name = new String (name.getbytes ("iso-8859-1"), "Utf-8");
FilePath = new String (filepath.getbytes ("iso-8859-1"), "Utf-8");
File Srcfile = new file (FilePath);
FIS = new FileInputStream (srcfile);
Set upload Directory
Ftpclient.changeworkingdirectory ("/upload");
Ftpclient.setbuffersize (1024);
Set file type (binary)
Ftpclient.setfiletype (Ftpclient.binary_file_type);
Ftpclient.storefile (New String (Name.getbytes ("GBK"), "iso-8859-1"), FIS);
Download: 2. Available in two ways to download
String filename = request.getparameter ("FileName");
FileName = new String (filename.getbytes ("iso-8859-1"), "Utf-8");
ftpclient ftp = new FtpClient ();
FileOutputStream fos = null;
String LocalPath = Request.getrealpath ("") + File.separator + "Download" + file.separator;
File LocalFile = new file (LocalPath + "" + fileName);
Path to the download file
String Path = LocalPath + "" + fileName;
/*
* Use FTP Download: the FTP server above the file under the Tomcat project directory, specify the download path.
*/
try{
Ftp.connect ("127.0.0.1");
Ftp.login ("shmily", "shmily");
String remotefilename = "/download/" + "" + fileName;
Ftp.changeworkingdirectory (Remotefilename);
FOS = new FileOutputStream (path);
Ftp.retrievefile (New String (Remotefilename.getbytes ("GBK"), "iso-8859-1"), FOS);
}catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException ("FTP client error. ", e);
} finally {
ioutils.closequietly (FOS);
try {
Ftp.disconnect ();
} catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException ("Close FTP connection exception occurred. ", e);
}
}
/*
* Download using the browser: Under the Tomcat project path under the file to local, you can choose the download path.
*/
if (!localfile.exists ())
Localfile.createnewfile ();
Response.setcontenttype ("Application/x-download");//set to download Application/x-download
filename = urlencoder.encode (filename, "UTF-8");
Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);//file name displayed when downloading files
OutputStream output = Response.getoutputstream ();
FileInputStream fis = new FileInputStream (path);
Byte[] B = new byte[10240];
int i = 0;
while ((i = Fis.read (b)) > 0) {
Output.write (b, 0, I);
}
Output.flush ();
Fis.close ();
Output.close ();