<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspsmart.upload.*,java.sql.*,DBConnectionManager"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="RSTR" scope="page" class="myrand.RandomStrg" />
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<% 
DBConnectionManager connMgr;
connMgr = DBConnectionManager.getInstance();
 Connection con = connMgr.getConnection("db");
 if (con == null) out.println("不能获取数据库连接.");
 Statement mySQL= con.createStatement();%>
</head><body>
<% 
 try{mySmartUpload.initialize(pageContext);String path="no photo";
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()){
RSTR.setCharset("a-zA-Z0-9");
RSTR.generateRandomObject();
path="/news/newsimages/"+RSTR.getRandom()+myFile.getFileName();
myFile.saveAs(path);}String bbstopic=mySmartUpload.getRequest().getParameter("bbstopic");
String content=mySmartUpload.getRequest().getParameter("content");java.util.Date d=new java.util.Date();
java.text.SimpleDateFormat dformat=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateandtime=dformat.format(d);String cmd="insert into news(content,topic,dateandtime,image,hits) values('"+content+"','"+bbstopic+"','"+dateandtime+"','"+path+"',0)";
mySQL.executeUpdate(cmd);
mySQL.close();connMgr.freeConnection("db", con);out.println("<A href='../index.jsp'>返回首页</a>");
}
catch(Exception e)
{out.println(e);
}
%>
</body>
</html>

解决方案 »

  1.   

    下载一个jspsmartupload
    http://www.jspsmart.com/
      

  2.   


    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.jspsmart.upload.*;
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class UpLoad extends HttpServlet{
        public UpLoad() {
        }
        private ServletConfig config;
        /**
        * Init the servlet
        */
        final public void init(ServletConfig config) throws ServletException {
                this.config = config;
        }    final public ServletConfig getServletConfig() {
                return config;
        }    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            System.out.println("============UpLoad begin ddd=============");
            String id=null;
            SmartUpload mySmartUpload = new SmartUpload();
            PrintWriter out = response.getWriter();
            try  {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            }catch(java.lang.ClassNotFoundException  e)  {
                System.err.print("ClassNotFoundException: "+e.getMessage());
            }
            try{
                //Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","dms","dms");            conn.setAutoCommit(false);//关闭自动提交,以提高性能。
                Statement stmt=conn.createStatement();
                // Initialization
                mySmartUpload.initialize(config, request, response);            mySmartUpload.setMaxFileSize(500 * 1024);            // Upload
                mySmartUpload.upload();            Enumeration enumer=mySmartUpload.getRequest().getParameterNames();
                while(enumer.hasMoreElements()){
                    String key=(String) enumer.nextElement();
                    String[] values=mySmartUpload.getRequest().getParameterValues(key);
                    if(key.equals("id")){ //得到各个的内容
                        id=values[0];
                    }
                }            //取得文件和文件名
                com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
                String fileName=myFile.getFileName();            if(!myFile.isMissing()){
                    //save data
                    //myFile.saveAs("/upload/" + fileName);
                    //mySmartUpload.save("/upload/" + fileName);
                    myFile.saveAs(fileName,mySmartUpload.SAVE_PHYSICAL);
                    System.out.println
    java.io.File file=new java.io.File(fileName);
    java.io.InputStream inStream=new java.io.FileInputStream(file);                int fileSize=myFile.getSize();
                    byte[] bytes = new byte[fileSize];
    String strSql="insert into zyw_test(id,name,content) values('"+id+"','"+fileName+"',empty_blob())";
                   stmt.execute(strSql);               ResultSet rs=stmt.executeQuery("select content from zyw_test where id='"+id+"' for update ");
                   if (rs.next()){
                       oracle.sql.BLOB blob = ((oracle.jdbc.OracleResultSet)rs).getBLOB("content");
                       OutputStream outStream = blob.getBinaryOutputStream();
                       inStream.read(bytes);
                       for(int i = 0; i < bytes.length; i++){
                           System.out.print(bytes[i]+" ");
                       }
                       outStream.write(bytes);
                       outStream.flush();
                       stmt.execute("commit");
                       outStream.close();
                   }
                   inStream.close();
                   stmt.close();
             out.println("upload sucess");
                }else{
                    out.println("no file");
                }
            }catch(Exception ex){
                out.println("upload fail");
                ex.printStackTrace();
            }
            System.out.println("============UpLoad end=============");
        }
    }
      

  3.   

    http://expert.csdn.net/Expert/topic/1266/1266200.xml?temp=.870434