我有一个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
...全文
46 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Broadsea 2003-10-02
  • 打赏
  • 举报
回复
Thx for lzq437(偶然)
plz 2003-09-10
  • 打赏
  • 举报
回复
调试lzq437的程序出现的问题!
500 Servlet Exception
Note: sun.tools.javac.Main has been deprecated.
/jw/system/upload.jsp:6: Class _system.upload not found.
upload upload = new upload();
^
/jw/system/upload.jsp:6: Class _system.upload not found.
upload upload = new upload();
^
2 errors, 1 warning



--------------------------------------------------------------------------------
Resin 2.0.5 (built Fri Jan 11 08:29:05 PST 2002)
qiao99 2003-07-23
  • 打赏
  • 举报
回复
建议你用jspsmart的类。比较全面
orant 2003-07-23
  • 打赏
  • 举报
回复
我也完成任务了
one_bird 2003-07-23
  • 打赏
  • 举报
回复
我也正在调上传,研究一下~
amortal 2003-07-23
  • 打赏
  • 举报
回复
用JspSmart吧,看这个玩意儿多累
lzq437 2003-07-23
  • 打赏
  • 举报
回复
你按照这个做一下.........
出现问题贴出来.......
lzq437 2003-07-23
  • 打赏
  • 举报
回复
一个文件上传JAVABEAN
package com.upload;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;

public class upload{
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";

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;
}

public void setContentType(String s){
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1){
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}

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;
//得到文件名
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 = getFileName(filename);

String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

//File(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory, filename);

//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
FileOutputStream 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)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
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;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}

byte byte0;
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(Linebyte, 0, ai[0] - newline.length() * byte0);

fileoutputstream.close();
}
}

private String readLine(byte Linebyte[], int ai[],
ServletInputStream servletinputstream,
String CharacterEncoding){
try{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
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{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception _ex){
return null;
}
}
/*
public int readLine(byte[] buffer,
int offset,
int length) throws java.io.IOException
从POST来的数据中读一行
参数:
buffer - buffer to hold the line data
offset - offset into the buffer to start
length - maximum number of bytes to read.
Returns:
number of bytes read or -1 on the end of line.
*/
}


upload.jsp
<%@page import="com.upload.upload"%>
<%
String Dir = "/home/tonywan/upload";

upload upload = new upload();
upload.setUploadDirectory(Dir);
upload.uploadFile(request);
out.print("<center><font color=red>成功上载文件至" + Dir + "</font></center>");
%>


upload.html
<center>File upload test</center>
<br>
<table><tr>
<form method="post" enctype="multipart/form-data" action="upload.jsp">
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=Submit value=Upload> </td> </form>
</tr></table>
ltf_ty 2003-07-23
  • 打赏
  • 举报
回复
Mark

81,092

社区成员

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

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