1.首先下载jar包,加入到工程的lib中。 2.上传页面: upload.jsp <table border="0" align="center" cellpadding="0" cellspacing="0" class="zw14"> <tr> <td>请选择上传文件</td> <td height="45" align="center" valign="middle"> <form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1"> <input type="file" name="file"></td></tr> <tr><td>文件描述</td><td align="left"><input type="text" name="desc"></td></tr> <tr><td colspan="2" align="center"><input type="submit" name="Submit" onClick="return check();" value="上传"></td></tr> </form> </table> uploadimage.jsp <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%> <% SmartUpload mySmartUpload =new SmartUpload(); long file_size_max=4000000; String fileName2="",ext="",testvar=""; String url="upload/"; //应保证在根目录中有此目录的存在 //初始化 mySmartUpload.initialize(pageContext); //只允许上传此类文件 try { mySmartUpload.setAllowedFilesList("jpg,gif,txt,doc,rar");//此处的文件格式可以根据需要自己修改 //上传文件 mySmartUpload.upload(); } catch (Exception e){ %> <SCRIPT language=javascript> alert("只允许上传.jpg,gif,txt,doc,rar类型文件"); window.location='upload.htm'; </script> <% } try{ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); if (myFile.isMissing()){%> <SCRIPT language=javascript> alert("请先选择要上传的文件"); window.location='upload.jsp'; </script> <%} else{ //String myFileName=myFile.getFileName(); //取得上传的文件的文件名 ext= myFile.getFileExt(); //取得后缀名 int file_size=myFile.getSize(); //取得文件的大小 String saveurl=""; if(file_size<file_size_max){ //更改文件名,取得当前上传时间的毫秒数值 Calendar calendar = Calendar.getInstance(); String filename = String.valueOf(calendar.getTimeInMillis()); saveurl=application.getRealPath("/")+url; saveurl+=filename+"."+ext; //保存路径 myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL); String u="/upload/"+filename+"."+ext; String desc=mySmartUpload.getRequest().getParameter("desc"); out.println("<TABLE BORDER=1 class='zw12'>"); out.println("<TR><TD>表单项名(FieldName)</TD><TD>" + myFile.getFieldName() + "</TD></TR>"); out.println("<TR><TD>文件长度(Size)</TD><TD>" + myFile.getSize() + "</TD></TR>"); out.println("<TR><TD>文件名(FileName)</TD><TD>" + myFile.getFileName() + "</TD></TR>"); out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>" + myFile.getFileExt() + "</TD></TR>"); out.println("<TR><TD>文件全名(FilePathName)</TD><TD>" + myFile.getFilePathName() + "</TD></TR>"); out.println("</TABLE><BR>"); //下面是把上传文件的描述和路径存放在数据库中 try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ssj?user=root&password=root&characterEncoding=gb2312"); PreparedStatement ps=con.prepareStatement("insert into upload(desce,url) values(?,?)");//插入数据库 ps.setString(1, desc); ps.setString(2, u); ps.execute(); ps.close(); con.close();//关闭连接 } catch (Exception e2) { out.print(e2.toString()); } } } }catch (Exception e){ out.print(e.toString()); } %> down.jsp 下载页面 <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%> <% String id=request.getParameter("id");//得到下载文件的id int idd=Integer.valueOf(id);//转为int try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ssj?user=root&password=root&characterEncoding=gb2312"); PreparedStatement ps=con.prepareStatement("select * from upload where id=?");//插入数据库 ps.setInt(1, idd); ResultSet rs=ps.executeQuery(); rs.next(); String xx=rs.getString(3); ps.close(); con.close();//关闭连接 out.clear(); out=pageContext.pushBody();//防止出现getOutstream错误 SmartUpload su = new SmartUpload(); // 初始化 su.initialize(pageContext); // 设定contentDisposition为null以禁止浏览器自动打开文件, //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为 //doc时,浏览器将自动用word打开它。扩展名为pdf时, //浏览器将用acrobat打开。 su.setContentDisposition(null); // 下载文件 su.downloadFile(xx);//xx为从数据库中得到的路径 } catch (Exception e2) { out.print(e2.toString()); } %>