jbuild中request的问题,百思不得其解,请高人指点,感激感激感激!!!!!

hexingjie1980 2002-05-15 10:08:26
我写了一段代码,想要实现从本机上传文件到固定的FTP,代码如下:

up.html:
<html>
<head>
<title>文件上载</title>
</head>
<body>
<form action="upload.jsp" enctype="MULTIPART/FORM-DATA" method=post>
<h3>
选择要上载的文件的路径:
<h3/>
<input type="file" name="filename" />
<br />
<input type="submit" value="上载" />
</form>
</body>
</html>

upload.jsp:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.util.*"%>

<html>
<head>
<title>
Jsp2
</title>
</head>
<jsp:useBean id="BeanId" scope="page" class="up.uploadBean" />

<body>
<%!String filepathname;%>
<%!String pwd="hexingjie";%>
<%!String uid="hexingjie";%>
<%! boolean justify;%>
<%! String hostname="10.111.169.101";%>
<%
BeanId.sethostname(hostname);
BeanId.setpwd(pwd);
BeanId.setuid(uid);

filepathname=request.getParameter("filename");

justify=BeanId.connect();
if(justify==true) /*如果连接成功,传文件*/
{
BeanId.sendFile(filepathname);
}
else
{
System.out.println("can not connect ftp");
}
%>
</body>
</html>

uploadBean.java:
package up;

import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class uploadBean
{
FtpClient aftp;
DataOutputStream outputs ;
TelnetOutputStream outs;
public String a=" ";
public int ch;
public String hostname="";
public String pwd="";
public String uid="";

public void sethostname(String hostname)/*设置FTP的用户名等等*/
{
this.hostname=hostname;
}

public void setpwd(String pwd)
{
this.pwd=pwd;
}

public void setuid(String uid)
{
this.uid=uid;
}

public boolean connect()/*实现与FTP的连接*/
{
try{
aftp =new FtpClient(hostname);
aftp.login(uid,pwd);
aftp.binary();
}catch(FtpLoginException e){
a="无权限与主机:"+hostname+"连接!";
}catch (IOException e){
a="连接主机:"+hostname+"失败!";
return false;
}catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!";
return false;
}
return true;
}

public void sendFile(String filepathname)/*向FTP传文件*/
{
if (aftp != null)
{
try{
a="粘贴成功!";
String fg =new String("\\");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index+1);
File localFile ;
localFile = new File(filepathname) ;
RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");
sendFile.seek(0);
outs = aftp.put(filename);
outputs = new DataOutputStream(outs);
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
}
outs.close();
sendFile.close();
}catch(IOException e){
a = "粘贴失败!";
}
}
}
}

运行后在upload.jsp中出现错误:
Exception in: R( + /upload.jsp + null) - javax.servlet.ServletException
java.lang.NullPointerException
at up.uploadBean.sendFile(uploadBean.java:72)
好像是request有问题,百思不得其解,请高人指点,感激感激感激!!!!!
...全文
64 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复

还没有回复,快来抢沙发~

相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2002-05-15 10:08
社区公告
暂无公告