java实现的FTP文件夹上传只能传tomcat服务器上的文件夹

ssy_shandong 2013-10-16 04:33:13
最近用java实现FTP文件夹上传时,只能传tomcat服务器上的文件夹,而不能传web客户端的文件夹,请问是什么原因,如何修改?先谢谢了。
上传文件夹的核心代码如下:
	/**
* 上传文件或文件夹
*/
public void upload(File file, ProgressListener listener) throws Exception
{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);

/**
* File.isDirectory():
* Tests whether the file denoted by this abstract pathname is a directory
* 测试当前file对象表示的文件是否是一个路径
*/

if(file.isDirectory())
{
/**
* File.getName()
* Returns the name of the file or directory denoted by this abstract pathname
* 返回表示当前对象的文件名
*/
ftpClient.makeDirectory(file.getName());

ftpClient.changeWorkingDirectory(file.getName());

/**
* File.list()
* Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname
* 返回当前File对象指定的路径文件列表
*/
String[] files = file.list();

for(int i=0; i<files.length; i++)
{
/**
* File.getPath()
* Converts this abstract pathname into a pathname string.
* 输出:D:\a\x.flv
*/
File file1 = new File(file.getPath()+"\\"+files[i]);

if(file1.isDirectory())
{
upload(file1, listener);
ftpClient.changeToParentDirectory();
}
else
{

File file2 = new File(file.getPath()+"\\"+files[i]);
System.out.println("else分支中 " + file.getPath()+"\\"+files[i]);
System.out.println("else分支中 file2.getName() = " + file2.getName());

InputStream input = new FileInputStream(file2);

int n = -1;
/**
* InputStream.available()
* Returns an estimate of the number of bytes that can be read(or slipped over) from this input stream
* without blocking by the next invocation of a method for this input stream
*/
long pContentLength = input.available();
System.out.println("else分支中 pContentLength= " + pContentLength);
System.out.println("--------------------------------------------");

long trans = 0;

/**
* bufferSize = 1024
*/
int bufferSize = ftpClient.getBufferSize();

byte[] buffer = new byte[bufferSize];

OutputStream os = ftpClient.storeFileStream(file2.getName());

/**
* FileInputStream.read(byte[] b)
* Reads up to b.length bytes of data from this input stream into an array of bytes
*/
while((n = input.read(buffer)) != -1)
{
/**
* OutputStream.write(byte[] b, int off, int len)
* Writes len bytes from the specified byte array starting at offset off to this output stream
*/
os.write(buffer, 0, n);
trans += n;
listener.update(trans, pContentLength, i+1);
}

input.close();
os.flush();
os.close();

ftpClient.completePendingCommand();
}
}
}
else
{
File file3 = new File(file.getPath());

InputStream input = new FileInputStream(file3);

int n = -1;
/**
* InputStream.available()
* Returns an estimate of the number of bytes that can be read(or slipped over) from this input stream
* without blocking by the next invocation of a method for this input stream
*/
long pContentLength = input.available();

long trans = 0;

/**
* bufferSize = 1024
*/
int bufferSize = ftpClient.getBufferSize();

byte[] buffer = new byte[bufferSize];

OutputStream os = ftpClient.storeFileStream(file3.getName());

/**
* FileInputStream.read(byte[] b)
* Reads up to b.length bytes of data from this input stream into an array of bytes
*/
while((n = input.read(buffer)) != -1)
{
/**
* OutputStream.write(byte[] b, int off, int len)
* Writes len bytes from the specified byte array starting at offset off to this output stream
*/
os.write(buffer, 0, n);
trans += n;
listener.update(trans, pContentLength, 1);
}

input.close();
os.flush();
os.close();

ftpClient.completePendingCommand();
}
}
...全文
198 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落夏天 2013-10-17
  • 打赏
  • 举报
回复
引用 2 楼 ssy_shandong 的回复:
提示FileNotFoundException,Tomcat部署在Windows Server 2003中,如何查看写入的目录有没有权限?
上传文件到了服务器,服务器接收到的是输入流,把输入流转化为文件。 这里就是一个地址的问题,你输出一下你的存文件的地址看一下,服务器端的路径结果和本地不一定一样的。
etnet 2013-10-17
  • 打赏
  • 举报
回复
引用 2 楼 ssy_shandong 的回复:
提示FileNotFoundException,Tomcat部署在Windows Server 2003中,如何查看写入的目录有没有权限?
这种问题你直接google一下都有了,还要人一口口喂饭给你吃吗?
chen870201 2013-10-17
  • 打赏
  • 举报
回复
大部分情况是因为权限问题
ssy_shandong 2013-10-17
  • 打赏
  • 举报
回复
提示FileNotFoundException,Tomcat部署在Windows Server 2003中,如何查看写入的目录有没有权限?
etnet 2013-10-16
  • 打赏
  • 举报
回复
错误是什么呢?异常信息如果是FileNotFoundException的话,你先看看你的写入目录有没有权限。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧