⑴ 用java写的FTP上传文件,上传到服务器liunx系统,写好的存放路径却放不进去
这是服务器权限制定的时候设置可能有问题,重新设置打开文件的目录权限即可。
⑵ 你好,请问你java上传文件到FTP服务器为空的问题怎么解决的
问题描述不够详细。把异常也放上来。检查ftp服务器是否允许上传,用户是否具备写文件权限。
⑶ ftp上传权限设置,
建议用Serv-U架设FTP服务器,教程可以看这个“ http://www.pconline.com.cn/pce/soft/lan/fwqrj/10211/109140_1.html”
⑷ 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;
@Override
publicFTPClientloginFtp(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;
}
@Override
publicbooleandownloadFileList(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("需要重命名的文件不存在,请检查。。。");
}
}
}
}
}
@Override
publicvoidcloseFtpConnection(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);*/
}
}
⑸ java ftp上传问题
很遗憾的告诉你不能上传到ftp,你需要先了解ftp协议,应该用socket就可以连接ftp服务器,然后发送ftp命令
⑹ java上传文件到 ftp出550 Permission denied怎么解决
ftp登录的用户没有写入的权限,加上就是了
⑺ 设置FTP特定用户有上传新文件的权限
设置FTP特定用户有上传新文件的权限的具体方法步骤如下:
1,在桌面上右击“我的电脑”,执行“管理”命令,在“计算机管理”窗口的左窗格中依次展开“系统工具”→“本地用户和组”目录,单击选中“用户”选项。在右侧窗格中单击右键,执行“新用户”命令。在打开的“新用户”对话框中填写用户名(如hanjiang),并设定密码。然后取消“用户下次登录时需更改密码”复选框,并勾选“用户不能更改密码”和“密码永不过期”复选框,单击“创建”按钮完成该用户的添加。重复这一过程添加其他用户,最后单击“关闭”按钮即可。
2,为方便对这些用户的管理,最好将他们放入一个专门的组中。创建一个“FTPUsers”组:在“计算机管理”窗口的目录树中单击选中“组”选项,然后在右侧窗格中单击右键,执行“新建组”命令,并将该组命名为“FTPUsers”。接着依次单击“添加”→“高级”→“立即查找”按钮,将刚才创建的用户全部添加进来,最后依次单击“创建”→“结束”按钮。
3,因为上述创建的用户默认隶属于“Users”组,也就是说他们拥有对大部分资源的浏览权限。为了实现对特定资源的有效管理,需要将这些用户从“Users”组中删除。在“计算机管理”窗口的右侧窗格中双击“Users”选项,用鼠标拖选所有刚添加的用户并单击“删除”按钮即可。
⑻ java程序可以成功连接Ftp服务器,但无法上传文件,怎么回事,报错如下,(已设置连接超时时间200s)
我感觉有俩问题,1、连接地址和帐号不是一回事,你最好不要用域名做连接地址,可以用IP地址;2、你是在不行通过空间服务商进入线上后台,如果还不行就联系空间商,可能是他们的问题.果是VPS的话好像要开通ftp某些权限才可以,你只是开通了帐号,能连接,并没有给ftp上传下载的权限,这个我在空间商里面看过教程的,在这没网络不让发连接,你可以在网络搜一下试试,希望能帮助你。
⑼ java代码 经已连接成功linux ftp服务器,但是就是上传不了文件
你是想从服务器下载文件吧?那应该用retrieveFile(String remote, OutputStream local),而不是storefile啊,自己看看API
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html
⑽ 如何设置ftp上传者权限
ftp不能设置上传文件的权限,原因是:
ftp只是将文件流上传到服务器上,而文件的读写属性是不包括在内的,他是由您连接的服务器上的操作系统决定的,例如linux。如果你要设置权限,只能是通过服务器的操作系统来设置,而不能通过ftp来设置文件上传权限。