我有一个JSP写的文件上传程序,但有问题谁能帮我调试一下?请看源代码
orant 2003-07-23 10:13:27 //上传文件的javabean
package com;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
public class FileUpload
{
private static String newline="\n";
private String uploadDirectory=".";
private String ContentType="";
private String CharacterEncoding="";
//从filename文件域获取的参数中提取文件名
private String getFileName(String s)
{
int i=s.lastIndexOf("\\");
if(i<0||i>=s.length()-1);
{
i=s.lastIndexOf("/");
if(i<0||i>=s.length()-1)
return s;
}
return s.substring(i+1);
}
//设置上传目录
public void setUploadDirectory(String s)
{
uploadDirectory=s;
}
//设置ContentType
public void setContentType(String s)
{
ContentType=s;
int j;
if((j=ContentType.indexOf("boundary="))!=-1)
{
ContentType=ContentType.substring(j+9);
ContentType="--"+ContentType;
}
}
//设置CharacterEncoding
public void setCharacterEncoding(String s)
{
CharacterEncoding=s;
}
//作上传文件的准备工作
public void uploadFile(HttpServletRequest req) throws ServletException,IOException
{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}
//上传文件
public void uploadFile(ServletInputStream servletinputstream) throws ServletException,IOException
{
String s5=null;
String filename=null;
byte Linebyte[]=new byte[4096];
byte outLinebyte[]=new byte[4096];
int ai[]=new int[1];
int ai1[]=new int[1];
String line;
byte byte0;
String sContentType;
FileOutputStream fileoutputstream;
//从servletinputstream中读取filename
while((line=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null)
{
int i=line.indexOf("filename=");
if(i>=0)
{
line=line.substring(i+10);
if((i=line.indexOf("\""))>0)
line=line.substring(0,i);
break;
}
}
filename=line;
if(filename!=null&&!filename.equals("\""))
{
//从filename文本域获取的参数中提取文件名
filename=getFileName(filename);
sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding);
if(sContentType.indexOf("Content-Type")>=0)
readLine(Linebyte,ai,servletinputstream,CharacterEncoding);
//在上载目录下生成一个和源文件同名的空的新文件
//File(String parent,String child)
File file=new File(uploadDirectory,filename);
//生成一个写目标文件的文件输出流
//FileOutputStream(File file)
fileoutputstream=new FileOutputStream(file);
while((sContentType=readLine(Linebyte,ai,servletinputstream,CharacterEncoding))!=null)
{
if(sContentType.indexOf(ContentType)==0&&Linebyte[0]==45)
break;
if(s5!=null)
{
//向文件输出流中写入数据
//write(byte[] b,int off,int len)
fileoutputstream.write(outLinebyte,0,ai1[0]);
fileoutputstream.flush();
}
s5=readLine(outLinebyte,ai1,servletinputstream,CharacterEncoding);
if(s5==null||(s5.indexOf(ContentType)==0&&outLinebyte[0]==45))
break;
//向文件输出流中写入数据
//write(byte[] b,int off,int len)
fileoutputstream.write(Linebyte,0,ai[0]);
fileoutputstream.flush();
}
if(newline.length()==1)
byte0=2;
else
byte0=1;
if(s5!=null&&outLinebyte[0]!=45&&ai1[0]>newline.length()*byte0)
fileoutputstream.write(outLinebyte,0,ai1[0]-newline.length()*byte0);
if(sContentType!=null&&Linebyte[0]!=45&&ai[0]>newline.length()*byte0)
fileoutputstream.write(outLinebyte,0,ai[0]-newline.length()*byte0);
fileoutputstream.close();
}
}
//从ServletinputStream中读取一行数据
private String readLine(byte Linebyte[],int ai[],ServletInputStream servletinputstream,String CharacterEncoding)
{
try{
//从POST来的数据中读取一行数据
//readLine(byte[] buffer,int offset,int length)
ai[0]=servletinputstream.readLine(Linebyte,0,Linebyte.length);
if(ai[0]==-1)
return null;
}
catch(IOException ex)
{
return null;
}
try{
if(CharacterEncoding==null)
{
//用缺省的编码方式把给定的byte数组转换成自符串
//String(byte[] bytes,int offset,int length)
return new String(Linebyte,0,ai[0]);
}
else
{
//用给定的编码方式把给定的数组转换为字符串
//String(byte[] bytes,int offset,int length,String enc)
return new String(Linebyte,0,ai[0],CharacterEncoding);
}
}
catch(Exception ex)
{
return null;
}
}
}
以上是上传文件的JavaBean
<jsp:useBean id="upload" class="com.cf.FileUpload"/>
<html>
<head>
<title>文件上传成功</title>
</head>
<body>
<%
String Dir="c:\\";
upload.setUploadDirectory(Dir);
upload.uploadFile(request);
out.print("<center><h2>成功将文件上传至"+Dir+"</h2></center>");
%>
</body>
</html>
这是upload.jsp
<html>
<head>
<title>文件上传</title>
</head>
<body>
<div align="center">
<h2>文件上传</h2>
<form method="post" enctype="multipart/form-date" action="upload.jsp">
<input type=file size=20 name="filename">
<input type=Submit value="文件上载">
</form>
</div>
</body>
</html>
第三个是upload.htm