发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

用java+ftp实现文件上传的问题?

2005-07-02 00:06
赞助商链接

我想用java实现一个文件上传的类,但现在如果客户端上传一个文件时,如果在服务器上相同的路径下如果没有该文件则上传不成功,该怎么办啊,希望大大们帮帮忙小弟感激不尽^_^。代码如下:

package com.test.fileup;

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

import sun.net.ftp.FtpClient;


public class ftpUp {

public ftpUp() {
}

public static FtpClient m_client;

public static void disconnect()
{
if (m_client != null)
{
try
{
m_client.closeServer();
}
catch (IOException ex)
{
}

m_client = null;
}
}

public static boolean connect(String sHost, String user,String password ,String sDir)
{
try
{
m_client = new FtpClient(sHost);
m_client.login(user, password);
m_client.cd(sDir);
m_client.binary();
}
catch (Exception ex)
{
return false;
}

return true;
}

public static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile)
{
if (m_sLocalFile.length()==0)
{
return false;
}

byte[] buffer = new byte[10240];

try
{
File f = new File(m_sLocalFile);
int size = (int)f.length();
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(m_sHostFile);

int counter = 0;
while(true)
{
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
}

out.close();
in.close();
}
catch (Exception ex)
{
return false;
}

return true;
}


public static boolean putFile(String pathname,String ftpServer, String ftpUser,
String ftpPasswd, String ftpPath)
{
if (!connect(ftpServer,ftpUser,ftpPasswd,ftpPath))
{
return false;
}

int pos = pathname.lastIndexOf("/");
int len = pathname.length();

String filename = pathname.substring(pos+1,len);

if (!putFiletoServer(pathname,filename))
{
return false;
}

disconnect();
return true;
}

}

下面是jsp网页上传页面(ftp服务器在192.168.0.1上):
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.sbwl.fileup.*" errorPage="" %>
<jsp:useBean id="fileUpload" scope="page" class="com.sbwl.test.ftpUp" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%
if (request.getParameter("action").equals("up")){
try {
if (fileUpload.connect("192.168.0.1","fileftp","123456","I:/fileup")){
out.println("connected");
}
else {
out.println("disconnected");
}

String realpath = new String(request.getParameter("filefu").getBytes("ISO-8859-1"));
if (fileUpload.putFile(""+realpath+"","192.168.0.1","fileftp","123456","I:/fileup")) {
out.println("uploaded");
}
else {
out.println("unuploaded");
}

}
catch (Exception e){
out.print(e);
}
} //action=up
else {
%>
<table width="652" height="87" border="1">
<tr>
<td height="81"><form action="index.jsp?action=up" method="post" name="form1" target="_blank">
<input name="filefu" type="file" id="filefu">
<input type="submit" name="Submit" value="提交">
</form></td>
</tr>
</table>
<%
}
%>
</body>
</html>

2005-07-02 16:14

>服务器上相同的路径下如果没有该文件
操作系统创建权限没有给予Java运行吧?

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com