package yuanyifileup;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext; public class yuanyifileup
{
private ServletRequest request;
private ServletResponse response;
private ServletConfig config;
ServletInputStream DATA;
int FormSize;
File f1;
FileOutputStream os;
DataInputStream is;
String filename;
byte[] b;
byte t;
boolean flag=false;
public yuanyifileup()
{ }
public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException
{
this.request=request;
this.response=response;
this.config=config;
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public void initialize(PageContext pageContext) throws IOException
{
request=pageContext.getRequest();
response=pageContext.getResponse();
config=pageContext.getServletConfig();
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public boolean setFilename(String s)
{
try
{
File f1=new File(s);
os=new FileOutputStream(f1);
}
catch(IOException e)
{return(false);}
return(true);
}
public void getByte()
{
int i=0;
try
{
is=new DataInputStream(DATA);
b=new byte[FormSize]; while (true)
{
try
{
t=is.readByte();
b[i]=t;
i++;
}
catch(EOFException e)
{ break;}
}
is.close();}
catch(IOException e)
{}
} public boolean save()
{
int i=0,start1=0,start2=0;
String temp="";
if (!flag)
{
getByte();
flag=true;
}
try
{
temp=new String(b,"ISO8859_1");
}
catch(UnsupportedEncodingException e)
{return(false);} start1=temp.indexOf("image/");
temp=temp.substring(start1); start1=temp.indexOf("\r\n\r\n"); temp=temp.substring(start1+4);
start2=temp.indexOf(";\r\n");
if (start2!=-1)
{
temp=temp.substring(0,start2);
}
try
{
byte[] img=temp.getBytes("ISO8859_1");
for (i=0;i<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);} return(true); }
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext; public class yuanyifileup
{
private ServletRequest request;
private ServletResponse response;
private ServletConfig config;
ServletInputStream DATA;
int FormSize;
File f1;
FileOutputStream os;
DataInputStream is;
String filename;
byte[] b;
byte t;
boolean flag=false;
public yuanyifileup()
{ }
public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException
{
this.request=request;
this.response=response;
this.config=config;
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public void initialize(PageContext pageContext) throws IOException
{
request=pageContext.getRequest();
response=pageContext.getResponse();
config=pageContext.getServletConfig();
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public boolean setFilename(String s)
{
try
{
File f1=new File(s);
os=new FileOutputStream(f1);
}
catch(IOException e)
{return(false);}
return(true);
}
public void getByte()
{
int i=0;
try
{
is=new DataInputStream(DATA);
b=new byte[FormSize]; while (true)
{
try
{
t=is.readByte();
b[i]=t;
i++;
}
catch(EOFException e)
{ break;}
}
is.close();}
catch(IOException e)
{}
} public boolean save()
{
int i=0,start1=0,start2=0;
String temp="";
if (!flag)
{
getByte();
flag=true;
}
try
{
temp=new String(b,"ISO8859_1");
}
catch(UnsupportedEncodingException e)
{return(false);} start1=temp.indexOf("image/");
temp=temp.substring(start1); start1=temp.indexOf("\r\n\r\n"); temp=temp.substring(start1+4);
start2=temp.indexOf(";\r\n");
if (start2!=-1)
{
temp=temp.substring(0,start2);
}
try
{
byte[] img=temp.getBytes("ISO8859_1");
for (i=0;i<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);} return(true); }
***************1.htm*******************
<HTML>
<BODY BGCOLOR="white"><H1>jspSmartUpload : Sample 1</H1>
<HR><FORM METHOD="POST" ACTION="sample1.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE2" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE3" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE4" SIZE="50"><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload" name="submit">
</FORM></BODY>
</HTML>
****************sample1.jsp**************************
<%@page contentType="text/html;charset=gb2312"%>
<%@page language="java" import="java.sql.*,java.util.Date" %>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="misTree" scope="session" class="extech.mis.tree"/>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
<BODY BGCOLOR="white">
<H1>jspSmartUpload : Sample 1</H1>
<HR>
<%
String menuid;
menuid=request.getParameter("menuid");
%>
<%
Date nowTime=new Date();
int yyyy=1900+nowTime.getYear();
int mm=nowTime.getMonth();
int dd=nowTime.getDate();
int hh=nowTime.getHours();
int mi=nowTime.getMinutes();
int ss=nowTime.getSeconds();
String sNowTime=yyyy+"-"+mm+"-"+dd+" "+hh+":"+mi+":"+ss;
%>
<% // Variables
int count=0; // Initialization
mySmartUpload.initialize(pageContext);
//1kb为1024个字节
mySmartUpload.setTotalMaxFileSize(10000000); // Upload
mySmartUpload.upload(); try { // Save the files with their original names in the virtual path "/upload"
// if it doesn't exist try to save in the physical path "/upload"
count = mySmartUpload.save("/mis/upload");
// Save the files with their original names in the virtual path "/upload"
// count = mySmartUpload.save("/upload", mySmartUpload.SAVE_VIRTUAL); // Display the number of files uploaded
out.println(count + " file(s) uploaded.<br><br>");
String sql;
ResultSet RS;
for(int i=0;i<count;i++)
{
String fileName;
fileName=mySmartUpload.getFiles().getFile(i).getFileName();
//mySmartUpload.getFiles().getFile(i).setFileName("saf");
//String uploadTime=new Date();
sql="insert into menu_content(menu_id,upload_file_name,upload_time,upload_member_name) values('"+menuid+"','"+fileName+"',to_date('"+sNowTime+"','yyyy-mm-dd hh24:mi:ss'),'"+session.getAttribute("userName")+"')";
out.println(sql);
RS=misTree.executeQuery(sql);
}
} catch (Exception e) {
out.println(e.toString());
}
%></BODY>
</HTML>
javabean自己下载吧