当前位置:首页 » 文件管理 » ftpstorefile

ftpstorefile

发布时间: 2023-09-29 16:29:07

1. 采用ftpclient.storeFile(String, Inputstream)将流写到服务器,没报错但服务器上没有文件,这是怎么回事

//创建一个FtpClient对象

FTPClient ftpClient = new FTPClient();
//上传文件 - 读取本地文件 file:需要上传的文件地址
FileInputStream inputStream = new FileInputStream(file);
//将流写到服务器
ftpclient.storeFile(String, inputStream)
其中String为保存后的文件名,inputStream就是上面获取的文件流

向上面说的服务器上没有文件,
1、可能是你String前面加了地址,但是你的ftp服务器中没有这个文件夹导致的,
2、在以有的文件夹下上传保存,在String前面加/文件夹名,
例:ftpClient.storeFile("/***"+String, inputStream);

(多层文件夹时)有的时候你需要给ftp文件夹设置权限(右击文件夹选择属性--安全--编辑--永许完全控制),可以试一试。最好只用当前层文件夹,否则每层都要设置
3、
ftpClient.makeDirectory("/文件名");//创建文件夹
ftpClient.changeWorkingDirectory("/文件名");改变保存路径
这种的最好只用一层文件夹路径
本人彩笔一枚,大佬请喷。喷完了请把解决思路说一下!!!

2. ftp上传文件的时候老是连接超时。

出现此问题的原因:传输模式错误。

解决的方法和操作步骤如下:

准备工具:FlashFXP5。

1、首先,在桌面上找到“
FlashFXP5”,然后双击以打开FTP软件,如下图所示,然后进入下一步。

3. FtpClient.storeFile作用

作用是将流写进服务器

FTP就是文件传输协议。用于互联网双向传输,控制文件下载空间在服务器复制文件从本地计算机或本地上传文件复制到服务器上的空间。

4. java下利用ftpClient.storeFile上传文件后不能返回

: ftpClient.setControlEncoding("GBK"); 这个改成下面试试 ftpClient.setControlEncoding("UTF-8");

5. 大哥 ,采用ftpclient.storeFile(String, Inputstream)将流写到服务器,没报错但服务器上没有文件,这个问

FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。 PORT(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请 求,服务器接受连接,建立一条命令链路。当需要传送数据时,客户端在命令链路上用PORT 命令告诉服务器:“我打开了XXXX端口,你过来连接我”。于是服务器从20端口向客户端的 XXXX端口发送连接请求,建立一条数据链路来传送数据。 PASV(被动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请 求,服务器接受连接,建立一条命令链路。当需要传送数据时,服务器在命令链路上用PASV 命令告诉客户端:“我打开了XXXX端口,你过来连接我”。于是客户端向服务器的XXXX端口 发送连接请求,建立一条数据链路来传送数据。

我当时因为连的是别人的服务器,那边换了服务器就出现这问题,后来我通过FTPClient有一个ftpclient.enterLocalPassiveMode()方法,设置后就没有这问题了,不知道你是不是跟我一样

6. 为什么ftp用FileZilla可以正常上传图片,使用java代码走到client.storefile();就

既然你可以用filezilla连接并实现上传, 说明服务器是没有问题的, 那么就要检查一下你的windows是否有问题了. 首先看一下windows的防火墙是不是开了, 如果打开了, 关闭一下, 再尝试一下上传功能, 看能够成功. 如果不行, 看一下你的ftpclient相关的jar包版本和jdk版本是否匹配, 这里也可能出问题.
希望能够帮到你.

7. JAVA 如何实现FTP远程路径

package nc.ui.doc.doc_007;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import nc.itf.doc.DocDelegator;
import nc.vo.doc.doc_007.DirVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;

public class FtpTool {

private FTPClient ftp;

private String romateDir = "";

private String userName = "";

private String password = "";

private String host = "";

private String port = "21";

public FtpTool(String url) throws IOException {
//String url="ftp://user:password@ip:port/ftptest/psd";
int len = url.indexOf("//");
String strTemp = url.substring(len + 2);
len = strTemp.indexOf(":");
userName = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);

len = strTemp.indexOf("@");
password = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
host = "";
len = strTemp.indexOf(":");
if (len < 0)//没有设置端口
{
port = "21";
len = strTemp.indexOf("/");
if (len > -1) {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
strTemp = "";
}
} else {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("/");
if (len > -1) {
port = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
port = "21";
strTemp = "";
}
}
romateDir = strTemp;
ftp = new FTPClient();
ftp.connect(host, FormatStringToInt(port));

}

public FtpTool(String host, int port) throws IOException {
ftp = new FTPClient();
ftp.connect(host, port);
}

public String login(String username, String password) throws IOException {
this.ftp.login(username, password);
return this.ftp.getReplyString();
}

public String login() throws IOException {
this.ftp.login(userName, password);
System.out.println("ftp用户: " + userName);
System.out.println("ftp密码: " + password);
if (!romateDir.equals(""))
System.out.println("cd " + romateDir);
ftp.changeWorkingDirectory(romateDir);
return this.ftp.getReplyString();
}

public boolean upload(String pathname, String filename) throws IOException, BusinessException {

int reply;
int j;
String m_sfilename = null;
filename = CheckNullString(filename);
if (filename.equals(""))
return false;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
System.exit(1);
}
FileInputStream is = null;
try {
File file_in;
if (pathname.endsWith(File.separator)) {
file_in = new File(pathname + filename);
} else {
file_in = new File(pathname + File.separator + filename);
}
if (file_in.length() == 0) {
System.out.println("上传文件为空!");
return false;
}
//产生随机数最大到99
j = (int)(Math.random()*100);
m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名
is = new FileInputStream(file_in);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.storeFile(m_sfilename, is);
ftp.logout();

} finally {
if (is != null) {
is.close();
}
}
System.out.println("上传文件成功!");
return true;
}

public boolean delete(String filename) throws IOException {

FileInputStream is = null;
boolean retValue = false;
try {
retValue = ftp.deleteFile(filename);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
return retValue;

}

public void close() {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static int FormatStringToInt(String p_String) {
int intRe = 0;
if (p_String != null) {
if (!p_String.trim().equals("")) {
try {
intRe = Integer.parseInt(p_String);
} catch (Exception ex) {

}
}
}
return intRe;
}

public static String CheckNullString(String p_String) {
if (p_String == null)
return "";
else
return p_String;
}

public boolean downfile(String pathname, String filename) {

String outputFileName = null;
boolean retValue = false;
try {
FTPFile files[] = ftp.listFiles();
int reply = ftp.getReplyCode();

////////////////////////////////////////////////
if (!FTPReply.isPositiveCompletion(reply)) {
try {
throw new Exception("Unable to get list of files to dowload.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/////////////////////////////////////////////////////
if (files.length == 0) {
System.out.println("No files are available for download.");
}else {
for (int i=0; i <files.length; i++) {
System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
outputFileName = pathname + filename + ".pdf";
//return outputFileName;
File f = new File(outputFileName);

//////////////////////////////////////////////////////
retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));

if (!retValue) {
try {
throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

/////////////////////////////////////////////////////////////
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retValue;
}

}

8. ftp上传文件,storeFile()为何总返回false

环境:服务器和客户端都是本机的
public void upload(String sourcePath, String targetPath) {try {FTPClient client = getFTPClient(Constants.ftpIP, 21, Constants.ftpUser, Constants.ftpPwd);
client.enterLocalPassiveMode();
InputStream inputStream = null;
File file = new File(sourcePath);
File [] files = file.listFiles();
for(File f : files) {
String fileName = f.getName();
inputStream = new FileInputStream(sourcePath+fileName);
boolean flag = client.storeFile(targetPath+fileName, inputStream);
System.out.println(flag);//为何此处的值是false}logger.info(上传完毕);
client.disconnect();
} catch (Exception e) {
logger.warn(向服务器上上传文件失败, e);}}//测试代码:
fod.upload(f:/b/, f:/a/);//这个结果是false

9. java 实现ftp上传的问题

我以前碰到过,不知道是不是和你的一样的问题:

我的是英文名称的文件可以上传,中文名不行(也不报错,运行正确,就是FTP文件服务器上没文件), 后来我发现是编码问题,我把我的项目代码贴上来:

package omsejb..upload;

import java.io.*;

import java.util.ArrayList;
import java.util.List;
import omsejb.system.OMSJDBCBase;
import tellhow.exception.sysexception.JDBCDAOSysException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

/**
* 上报文件(上传文件,如:地调上报省调数据,采用E语言文件形式上报)
* @author ZouLiXing
*
*/
public class UpLoadFile extends OMSJDBCBase
{
private String ftpHost = "";
private int ftpPort;
private String ftpUserName = "";
private String ftpPassword = "";
private String webServicePoint = "";
private String webServiceName = "";
private String ftpPath="";
private String localPath="";

public UpLoadFile(){

UploadDaoEntity ude = new UploadDaoEntity();
ftpHost = ude.getDicValue("ftphost");
ftpPort = Integer.parseInt(ude.getDicValue("ftpport"));
ftpUserName = ude.getDicValue("ftpusername");
ftpPassword = ude.getDicValue("ftppassword");
webServicePoint = ude.getDicValue("webservicepoint");
webServiceName = ude.getDicValue("webservicename");
ftpPath = ude.getDicValue("ftppath");
localPath = ude.getDicValue("filepath");
}
/**
* true表示FTP上传文件成功;否则失败。
*
* @param hostName
* FTP地址
* @param port
* 端口 默认21
* @param userName
* @param password
* @param path
* @param servicefileName
* 上传名
* @param input
* 本地文件输入流
* @return
*/
public boolean ftpUpLoadEFile(String filename) {
FTPClient ftpClient = new FTPClient();
int reply;
try {
ftpClient.connect(ftpHost, ftpPort);
boolean l=ftpClient.login(ftpUserName, ftpPassword);
if(l){
System.out.println("login success(成功登陆FTP)!");
}else{
System.out.println("login success(登陆FTP不成功)!");
return false;
}
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return false;
}
File file = new File(localPath+filename);
InputStream input = new FileInputStream(file);
boolean b = ftpClient.storeFile(GBKToiso8859(filename), input);
input.close();
ftpClient.logout();
if(!b){
System.out.println("上传文件不成功!");
return false;
}else{
System.out.println("上传文件成功-_-!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return true;
}
/**
* 转码[GBK -> iso-8859-1] 不同的平台需要不同的转码
*
* @param obj
* @return
*/
private String GBKToiso8859(Object obj) {
try {
if (obj == null)
return "";
else
return new String(obj.toString().getBytes("GBK"), "iso-8859-1");
} catch (Exception e) {
return "";
}
}

public static void main(String[] agrs){
// UpLoadFile uf = new UpLoadFile();
//
// String ftpHost = "10.61.12.233";
// int ftpPort = 21;
// String userName = "omshz";
// String password = "omshz11";
// String ftpPath = "";
////
//// String fileName = "";
//// String filePath = "";
//// String selectID="";//选择上传的文件ID,多个文件用","隔开;
//// List lsFilePath = uf.getFilePath(selectID);
//// for(int i=0;i<lsFilePath.size();i++){
// //上传E文件
//// filePath = lsFilePath.get(i).toString();
//// fileName = uf.getFileName(filePath);
// File file = new File("d:\\日购网电量上报_西安局_2006-03-06.YB");
// try {
// InputStream input = new FileInputStream(file);
// uf.ftpUpLoadEFile(ftpHost, ftpPort, userName, password, ftpPath,
// "日购网电量上报_西安局_2006-03-06.YB", input);
// } catch (FileNotFoundException fileNoe) {
// fileNoe.printStackTrace();
// }
//
}
}

热点内容
查看存储过程权限 发布:2024-05-18 17:18:33 浏览:191
php类self 发布:2024-05-18 17:15:03 浏览:894
手机2b2t的服务器地址是多少 发布:2024-05-18 17:14:56 浏览:188
戴尔8490哪个配置比较合理 发布:2024-05-18 17:14:51 浏览:168
删除sqlserver服务 发布:2024-05-18 16:47:06 浏览:323
密码盒的密码是多少钱 发布:2024-05-18 16:43:52 浏览:95
linux哪个c语言编译器好用 发布:2024-05-18 16:30:03 浏览:469
搜狐视频无法缓存 发布:2024-05-18 16:30:03 浏览:310
小鸟云服务器值不值得买 发布:2024-05-18 16:30:01 浏览:899
durbin算法 发布:2024-05-18 16:29:57 浏览:556