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有问题,百思不得其解,请高人指点,感激感激感激!!!!!
...全文
108 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

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

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