用文件表单上传了。<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">    <title>New Page 1</title>
  </head>
  <body>
    <form method="POST" enctype="multipart/form-data" action="uploda.jsp">
      <p><input type="file" name="file" size="20"><input type="submit" value="提交" name="B1"></p>
    </form>
  </body>
</html>//////upload.jsp
<%@ page language="java" import="java.io.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%
String filename=null;
String path=request.getRealPath("/");
String contentType=request.getContentType();
int boundaryIndex=contentType.indexOf("boundary=");
String boundary=contentType.substring(boundaryIndex+9);
int boundaryStrLength=boundary.length();

ServletInputStream servIn=request.getInputStream();

DataInputStream in=new DataInputStream(servIn);

String line;
while((line=in.readLine())!=null)
{
int index=line.indexOf("filename=");
if(index!=-1)
{
int indexsub=line.lastIndexOf("\\");
String temp=line.substring(indexsub+1);
int len=temp.length();
filename=temp.substring(0,len-1);
}
//getServletContext().log("Got line:"+line);
if(line.trim().length()==0)
break;
}

ByteArrayOutputStream byteOut=new ByteArrayOutputStream(request.getContentLength());
byte []buffer=new byte[4096];
int len;

while((len=in.read(buffer))>0)
{
byteOut.write(buffer,0,len);
}
byte []outBytes=byteOut.toByteArray();

//in.close();
ByteArrayInputStream bai=new ByteArrayInputStream(outBytes);

int seek=256;
if(outBytes.length<seek)
{
seek=outBytes.length;
}
byte []byteBuffer=new byte[seek];
bai.skip((long)(outBytes.length-seek));
bai.read(byteBuffer);
bai.close();
String show="";
show=new String(byteBuffer);
int index=boundary.lastIndexOf('-');
line=boundary.substring(0,index+1);
line=line+"--";
index=show.lastIndexOf(line);
int indexEnd=show.lastIndexOf("--");
String lastBoundary=show.substring(index,indexEnd);
index=show.indexOf(lastBoundary);
line=show.substring(index-2);
byte []byteDelete=line.getBytes();

if(filename==null)
filename=new String("file");
else
{
byte []tmp=filename.getBytes("ISO8859_1");
filename=new String(tmp);
}
FileOutputStream fileOut=new FileOutputStream(path+filename);
//报头附的字符:"-----------------------------7d32d03a9016a\r\nContent-Disposition: form-data; name="submit"\r\n\r\n????\r\n-----------------------------7d32d03a9016a--\r\n"
//out.println(outBytes.length+"=|"+new String(outBytes)+"|");
//out.println(byteDelete.length+"=|"+new String(byteDelete)+"|");
fileOut.write(outBytes,0,outBytes.length-byteDelete.length);
fileOut.close();
%>
<H3>文件<b><font color="#ff0000"><%=filename%></font></b>上传成功,上传<b><font color="#0000ff"><%=outBytes.length-byteDelete.length%></font></b>字节。

解决方案 »

  1.   

    用smartupload组件或apache的upload组件,参考下面的帖子:http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=33247
    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=135560
    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=54323
    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=55087
      

  2.   

    jspsmartupload
    http://www.jspsmart.com/下载下来的包里也有例子 看看就懂了 很简单
      

  3.   

    jspsmartupload
    http://www.jspsmart.com/下载下来的包里也有例子 看看就懂了 很简单