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: 上\u4F20文件到\u6570据中。形式在form中加上 ENCTYPE="multipart/form-data" </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);//\u8BBE置自\u52A8提交,以提高性能。
            Statement stmt=conn.createStatement();
            // Initialization
            mySmartUpload.initialize(config, request, response);            mySmartUpload.setMaxFileSize(500 * 1024);            // Upload
            mySmartUpload.upload();            //取得text框中的\u6570据
            Enumeration enumer=mySmartUpload.getRequest().getParameterNames();
            while(enumer.hasMoreElements()){
                String key=(String) enumer.nextElement();
                String[] values=mySmartUpload.getRequest().getParameterValues(key);
                if(key.equals("id")){ //得到text框中的value值
                    id=values[0];
                }
            }            //取得文件和文件名
            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
            String fileName=myFile.getFileName();            if(!myFile.isMissing()){
                //save data
                myFile.saveAs(fileName,mySmartUpload.SAVE_PHYSICAL);
                System.out.println("============fileName="+fileName+"====");
                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];
                System.out.println("============fileSize="+fileSize+"====");               //\u5C06\u6570据插入到\u6570据\u5E93中
               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);
                   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=============");
    }
}

解决方案 »

  1.   

    to jianten():可否谈一下你的思路。
    我对 socket编程还不熟悉。
      

  2.   

    看看这篇文档!
    自己编写JAVA环境下的文件上传组件
    http://www.csdn.net/develop/Read_Article.asp?Id=16432
      

  3.   

    tomcat下将jspSmartUpload 拷贝到 WEBINF/lib  下可
    jbuilder 下添加外部类库即可
      

  4.   

    我下载的jspSmartUpload是ZIP文件
    有没有JAR文件?
      

  5.   

    zip文件跟jar文件同样的用法,把路径加入到classpath里就可以调用里面的api了