javaftp修改文件名|FTP上传时怎么解决中文路径和中文名称

javaftp修改文件名|FTP上传时怎么解决中文路径和中文名称的第1张示图

1. FTP上传时怎么解决中文路径和中文名称

java上传文件到ftp有两种实现方式,一种是使用sun公司提供的sun.net.ftp包里面的FtpClient,另一种是Apache组织提供的org.apache.commons.net.ftp包里的FTPClient,现在我来分别说下两种实现方式。sun的FtpClient:我们先来看如下代码public static boolean uploadFileBySun(StringBuffer fileContent,String server,String userName, String userPassword, String path, String fileName) { FtpClient ftpClient = new FtpClient(); try { //打开ftp服务器 ftpClient.openServer(server); //使用指定用户登录 ftpClient.login(userName, userPassword); //转到指定路径 ftpClient.cd(path); TelnetOutputStream os = null; //新建一个文件// os = ftpClient.put(new String(fileName.getBytes("GBK"), "iso-8859-1")); os = ftpClient.put(fileName); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); bw.write(fileContent.toString()); bw.flush(); bw.close(); } catch (Exception e) { System.out.println(e.getMessage()); return false; } finally { try { //关闭ftp连接 ftpClient.closeServer(); } catch (Exception e) { e.printStackTrace(); } } return true; }代码结束符!正如上面的代码,上传文件分为六步,第一步,打开ftp服务器,第二步,使用指定用户名以及密码登陆,第三步,转到指定文件路径,第四步,创建一个文件,第五步,往文件里面写东西,并关闭文件,第六步,释放ftp连接。最后一步释放ftp连接很重要,一般ftp服务器连接数都是有限的,所以不管文件上传成功或是失败都必须释放连接。上面这个例子上传的文件是字符串文本,必须要提的是,如果上传的字符串文本较长(我项目中上传的文本大概在160kb上下),使用上面的方法可能会出现字符串丢失的情况,原因不明,可能跟缓存有关,所以如果文本较长,建议用户使用字节流。还有一个问题,如果要上传的文件名是中文的话,上传的文件名将是乱码,乱码问题我尝试许多转码也解决不了,于是不得不使用下面的方法了。Apache的FTPClient:public static boolean uploadFileByApacheByBinary(StringBuffer fileContent,String server,String userName, String userPassword, String path, String fileName) { FTPClient ftpClient = new FTPClient(); try { InputStream is = null; is = new ByteArrayInputStream(fileContent.toString().getBytes()); ftpClient.connect(server); ftpClient.login(userName, userPassword); ftpClient.changeWorkingDirectory(path); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1") , is); is.close(); } catch (Exception e) { e.printStackTrace(); return false; } finally { if(ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } return true; }代码结束符!Apache上传文件的步骤跟sun的实现基本一致,只是方法名有些区别而已。在这里我将字符串文本转换成了ByteArrayInputStream字节缓冲流,这是个很有用的东西,常用来进行字符到流的转换。转换成字节上传就不会出现丢失文件内容的情况了。ftpClient.storeFile(new String(fileName.getBytes(“GBK”), “iso-8859-1″) , is)这句代码将is输入流的东西上传到ftp服务器的fileName文件中,在这里我们对fileName文件名进行了转码,经测试中文没有乱码(ftp服务器使用的是window,其他平台未测试),而如果我们使用sun的ftp实现,即使文件名进行这样类似的转码,依然是乱码。

2. java 上传ftp 传输过程中文件名为tmp后缀 如何实现

replace("tmp", "") <—-字元替换

3. 关于FTPClient文件夹重命名的问题

环境描述:1.服务端:采用-u11.3版本的服务器管理工具作为ftp服务端2.客户端:采用FTPClient 1.4.1组件作为客户端访问ftp服务器我们在用FTPClient组件上传文件时,当上传的文件名为中文时,有以下几种情况:一、当没有禁用serv-u服务端工具的上传下载编码时,即:没有将域限制和设置里默认的opts-utf8禁止,在:域限制和设置—FTP设置选项卡—全局属性—高级选项—第三个多选框。1.客户端代码中如果编码为:UTF-8,那么完全没有问题,中文命名的文件可以正常上传;参考以下代码:/** * FTP上传单个文件测试 */ public static void testUpload() { FTPClient ftpClient = new FTPClient(); FileInputStream fis = null; try { ftpClient.connect("此处填写服务器IP"); ftpClient.login("用户名", "密码"); File srcFile = new File("F:\\路由器配置.txt"); fis = new FileInputStream(srcFile); //设置上传目录 ftpClient.changeWorkingDirectory("/admin/pic"); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("UTF-8");//这里设置编码 //设置文件类型(二进制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); boolean temp = ftpClient.storeFile(new String("路由器配置.txt".getBytes("UTF-8"),"iso-8859-1"), fis);//编码转换 System.out.println("temp——-"+temp); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("FTP客户端出错!", e); } finally { IOUtils.closeQuietly(fis); try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } } 2.客户端代码中如果编码为:GB2312,可以上传,但是上传的文件名为乱码,代码同上,改变红颜色的字的编码为GB2312即可;二、一、当不选serv-u服务端工具的上传下载的默认编码时,即:在:域限制和设置—FTP设置选项卡—全局属性—高级选项—第三个多选框不要勾选时;1.客户端代码中如果编码为:UTF-8,文件不能上传,代码中,temp变量为false;2.客户端代码中如果编码为:GB2312,文件正常上传,并且不为乱码,代码中,temp变量为true;综合上述:将ftp服务器默认编码禁用,并且客户端代码中以GB2312 编码上传并转化较为合理,解决中文文件名的问题;另外,还有多种情况没有举例,大家可以测试一下,以便了解更为透彻。

4. 求用java写一个ftp服务器客户端程序。

import java.io.*;import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){ String initDir; initDir = "D:/Ftp"; ServerSocket server; Socket socket; String s; String user; String password; user = "root"; password = "123456"; try{ System.out.println("MYFTP服务器启动…."); System.out.println("正在等待连接…."); //监听21号端口 server = new ServerSocket(21); socket = server.accept(); System.out.println("连接成功"); System.out.println("**********************************"); System.out.println(""); InputStream in =socket.getInputStream(); OutputStream out = socket.getOutputStream(); DataInputStream din = new DataInputStream(in); DataOutputStream dout=new DataOutputStream(out); System.out.println("请等待验证客户信息…."); while(true){ s = din.readUTF(); if(s.trim().equals("LOGIN "+user)){ s = "请输入密码:"; dout.writeUTF(s); s = din.readUTF(); if(s.trim().equals(password)){ s = "连接成功。"; dout.writeUTF(s); break; } else{s ="密码错误,请重新输入用户名:";<br> dout.writeUTF(s);<br> <br> } } else{ s = "您输入的命令不正确或此用户不存在,请重新输入:"; dout.writeUTF(s); } } System.out.println("验证客户信息完毕…."); while(true){ System.out.println(""); System.out.println(""); s = din.readUTF(); if(s.trim().equals("DIR")){ String output = ""; File file = new File(initDir); String[] dirStructure = new String[10]; dirStructure= file.list(); for(int i=0;i<dirStructure.length;i++){ output +=dirStructure[i]+"\n"; } s=output; dout.writeUTF(s); } else if(s.startsWith("GET")){ s = s.substring(3); s = s.trim(); File file = new File(initDir); String[] dirStructure = new String[10]; dirStructure= file.list(); String e= s; int i=0; s ="不存在"; while(true){ if(e.equals(dirStructure[i])){ s="存在"; dout.writeUTF(s); RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r"); byte byteBuffer[]= new byte[1024]; int amount; while((amount = outFile.read(byteBuffer)) != -1){ dout.write(byteBuffer, 0, amount);break; }break; } else if(i<dirStructure.length-1){ i++; } else{ dout.writeUTF(s); break; } } } else if(s.startsWith("PUT")){ s = s.substring(3); s = s.trim(); RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw"); byte byteBuffer[] = new byte[1024]; int amount; while((amount =din.read(byteBuffer) )!= -1){ inFile.write(byteBuffer, 0, amount);break; } } else if(s.trim().equals("BYE"))break; else{ s = "您输入的命令不正确或此用户不存在,请重新输入:"; dout.writeUTF(s); } } din.close(); dout.close(); in.close(); out.close(); socket.close();}catch(Exception e){ System.out.println("MYFTP关闭!"+e); }}}

5. 如何直接修改FTP上的文件

先下载FTP软件

点击进入FTP,

先配置好站点网站。如下图步骤:

5.然后点击鼠标右键编辑文件就可以了。可以在里面编辑代码文件。

6. C#ftp上传之后怎么修改文件的名称

保存操作时把文件名改成你需要的就行了,发个代码给你看看吧,fileName是什么就随你自己改就可以了,只要不和其它名字重复就可以了,一般是用日期加一个随机数保证唯一性string sPath = System.Web.HttpContext.Current.Request.MapPath("../../uploadfile/MyPic/") + fileName;fiuAvatarPic.SaveAs(sPath);

7. linux下的java通过ftp读取另一linux下的文件名出现中文乱码。

需要转一下编码,你的java的class文家中是GBK的编码,对面linux下是iso8859-1编码String fileNameTmp = new String(files[i].getBytes("iso-8859-1"), "GBK");//将从linux取得的内文件名转容换为GBK编码 String filename=fileNameTmp .substring(regStr.length()+1,fileNameTmp .length());然后再把转完编码的文件名按你的要求进行截取

8. java 实现ftp上传如何创建文件夹

这个功能我也刚写完,不过我也是得益于同行,现在我也把自己的分享给大家,希望能对大家有所帮助,因为自己的项目不涉及到创建文件夹,也仅作分享,不喜勿喷谢谢!

interface:packagecom.sunline.bank.ftputil;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importorg.apache.commons.net.ftp.FTPClient;publicinterfaceIFtpUtils{/***ftp登录*@paramhostname主机名*@paramport端口号*@paramusername用户名*@parampassword密码*@return*/publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword);/***上穿文件*@paramhostname主机名*@paramport端口号*@paramusername用户名*@parampassword密码*@paramfpathftp路径*@paramlocalpath本地路径*@paramfileName文件名*@return*/(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName);/***批量下载文件*@paramhostname*@paramport*@paramusername*@parampassword*@paramfpath*@paramlocalpath*@paramfileName源文件名*@paramfilenames需要修改成的文件名*@return*/publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames);/***修改文件名*@paramlocalpath*@paramfileName源文件名*@paramfilenames需要修改的文件名*/(Stringlocalpath,StringfileName,Stringfilenames);/***关闭流连接、ftp连接*@paramftpClient*@parambufferRead*@parambuffer*/publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer);}impl:packagecom.sunline.bank.ftputil;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importcommon.Logger;{privatestaticLoggerlog=Logger.getLogger(FtpUtilsImpl.class);FTPClientftpClient=null;Integerreply=null;@OverridepublicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword){ftpClient=newFTPClient();try{ftpClient.connect(hostname,port);ftpClient.login(username,password);ftpClient.setControlEncoding("utf-8");reply=ftpClient.getReplyCode();ftpClient.setDataTimeout(60000);ftpClient.setConnectTimeout(60000);//设置文件类型为二进制(避免解压缩文件失败)ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//开通数据端口传输数据,避免阻塞ftpClient.enterLocalActiveMode();if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){log.error("连接FTP失败,用户名或密码错误");}else{log.info("FTP连接成功");}}catch(Exceptione){if(!FTPReply.isPositiveCompletion(reply)){try{ftpClient.disconnect();}catch(IOExceptione1){log.error("登录FTP失败,请检查FTP相关配置信息是否正确",e1);}}}returnftpClient;}@Override@SuppressWarnings("resource")(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName){booleanflag=false;ftpClient=loginFtp(hostname,port,username,password);BufferedInputStreambuffer=null;try{buffer=newBufferedInputStream(newFileInputStream(localpath+fileName));ftpClient.changeWorkingDirectory(fpath);fileName=newString(fileName.getBytes("utf-8"),ftpClient.DEFAULT_CONTROL_ENCODING);if(!ftpClient.storeFile(fileName,buffer)){log.error("上传失败");returnflag;}buffer.close();ftpClient.logout();flag=true;returnflag;}catch(Exceptione){e.printStackTrace();}finally{closeFtpConnection(ftpClient,null,buffer);log.info("文件上传成功");}returnfalse;}@OverridepublicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames){ftpClient=loginFtp(hostname,port,username,password);booleanflag=false;=null;if(fpath.startsWith("/")&&fpath.endsWith("/")){try{//切换到当前目录this.ftpClient.changeWorkingDirectory(fpath);this.ftpClient.enterLocalActiveMode();FTPFile[]ftpFiles=this.ftpClient.listFiles();for(FTPFilefiles:ftpFiles){if(files.isFile()){System.out.println("=================="+files.getName());FilelocalFile=newFile(localpath+"/"+files.getName());bufferRead=newBufferedOutputStream(newFileOutputStream(localFile));ftpClient.retrieveFile(files.getName(),bufferRead);bufferRead.flush();}}ftpClient.logout();flag=true;}catch(IOExceptione){e.printStackTrace();}finally{closeFtpConnection(ftpClient,bufferRead,null);log.info("文件下载成功");}}modifiedLocalFileName(localpath,fileName,filenames);returnflag;}@Override(Stringlocalpath,StringfileName,Stringfilenames){Filefile=newFile(localpath);File[]fileList=file.listFiles();if(file.exists()){if(null==fileList||fileList.length==0){log.error("文件夹是空的");}else{for(Filedata:fileList){Stringorprefix=data.getName().substring(0,data.getName().lastIndexOf("."));Stringprefix=fileName.substring(0,fileName.lastIndexOf("."));System.out.println("index==="+orprefix+"prefix==="+prefix);if(orprefix.contains(prefix)){booleanf=data.renameTo(newFile(localpath+"/"+filenames));System.out.println("f============="+f);}else{log.error("需要重命名的文件不存在,请检查。。。");}}}}}@OverridepublicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer){if(ftpClient.isConnected()){try{ftpClient.disconnect();}catch(IOExceptione){e.printStackTrace();}}if(null!=bufferRead){try{bufferRead.close();}catch(IOExceptione){e.printStackTrace();}}if(null!=buffer){try{buffer.close();}catch(IOExceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsIOException{Stringhostname="xx.xxx.x.xxx";Integerport=21;Stringusername="edwftp";Stringpassword="edwftp";Stringfpath="/etl/etldata/back/";StringlocalPath="C:/Users/Administrator/Desktop/ftp下载/";StringfileName="test.txt";Stringfilenames="ok.txt";FtpUtilsImplftp=newFtpUtilsImpl();/*ftp.modifiedLocalFileName(localPath,fileName,filenames);*/ftp.downloadFileList(hostname,port,username,password,fpath,localPath,fileName,filenames);/*ftp.uploadLocalFilesToFtp(hostname,port,username,password,fpath,localPath,fileName);*//*ftp.modifiedLocalFileName(localPath);*/}}

9. 诸位大神谁有java 实现FTP客户端的源码

您好,/ ** *创建日期:2008年12月23日 *类名:Ftp.java *类路径:组织结构 *更改日志: * / 包组织结构; 进口的java.io.File; 进口java.io.FileInputStream中; 进口java.io.FileOutputStream中; 进口的java。 io.IOException; 进口sun.net.TelnetInputStream; 进口sun.net.TelnetOutputStream; 进口sun.net.ftp.FtpClient; > / ** * @作者南山地狱 * @说明FTP操作 * / 公共类的Ftp { / ** * BR />获取FTP目录* / 公共无效getftpList(){字符串服务器=“IP地址 /输入FTP服务器/>弦乐用户=”“;/ / FTP服务器的登录用户名字符串密码=“”;/ /登录FTP服务器的用户名字符串路径密码=“”;/ / FTP路径上的服务器尝试{ > FtpClient的FTP客户端=新FtpClient的();/ /创建FtpClient的对象 ftpClient.openServer(服务器);/ /连接到FTP服务器 ftpClient.login(用户名,密码);/ / FTP服务器 BR />如果(path.length()= 0){ ftpClient.cd(路径); } TelnetInputStream是= ftpClient.list(); 诠释三; 而{ System.out.print((char)的C)((C = is.read())= -1!); } 掉} is.close (); ftpClient.closeServer();/ /退出FTP服务器}赶上(IOException异常前){ System.out.println(ex.getMessage()); } } / ** * * / 公共无效getFtpFile(){字符串服务器=“”;/ / IP地址中输入FTP服务器弦乐用户=“”;/ / FTP服务器的登录用户名字符串密码=“”;/ /登录密码为FTP服务器的用户名字符串路径=“路径字符串文件名“;/ /上=的FTP服务器”“;/ /下载文件名称尝试{ FtpClient的FTP客户端=新FtpClient的(); ftpClient.openServer(服务器); ftpClient.login(用户名,密码); 如果(路径。长度()= 0) ftpClient.cd(路径);! ftpClient.binary(); TelnetInputStream是= ftpClient.get(文件名); 文件file_out =新的文件(文件名); 文件输出流OS =新的文件输出流(file_out); 字节[]字节=新字节[1024]; 诠释三; 而((C = is.read(字节))= -1){ os.write (字节,0,C); }! 掉} is.close(); os.close(); ftpClient.closeServer(); }赶上(IOException异常前){ System.out.println (ex.getMessage()); } FTP} / ** *文件上传到FTP * / 公共无效putFtpFile() {字符串服务器=“”;/ /输入IP地址对服务器字符串用户的地址=“”;/ / FTP服务器的登录用户名字符串密码=“”;/ / FTP服务器登录用户名密码字符串路径=“”就 / FTP服务器/>字符串文件名=“”;/ /上传的文件名FtpClient的FTP客户端=新的try { FtpClient的(); ftpClient.openServer(服务器); ftpClient.login(用户名,密码); 如果(!path.length()= 0) ftpClient.cd (路径); ftpClient.binary(); TelnetOutputStream OS = ftpClient.put(文件名); 文件file_in =新的文件(文件名); 文件输入流是=新的文件输入流(file_in); 字节[]字节=新字节[1024]; 诠释三; 同时(! (C = is.read(字节))= -1){操作系统。写(字节,0,C); } 掉} is.close(); os.close(); ftpClient.closeServer(); }赶上(IOException异常前){ System.out.println(ex.getMessage()); } } }

10. ftp 服务器上更改文件名总是报12003错误找不到路径,m_pConnection->Rename("12.txt","7012.txt")

你设置了当前目录了吗,不设置当前目录需要用绝对路径(完整路径)

未经允许不得转载:山九号 » javaftp修改文件名|FTP上传时怎么解决中文路径和中文名称

赞 (0)