一样使用呀,struts结构里面的servlet的用法跟在jsp中是一个样子

解决方案 »

  1.   

    为什么要用Startupload,Apache的upload组件也可以用啊。Struts自带的
      

  2.   

    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.jspsmart.upload.*;
    public class UpLoad extends HttpServlet {
        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();
            java.io.File file=null;
            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", "test", "test");
                conn.setAutoCommit(false); //设置手动提交,以提高性能。
                Statement stmt = conn.createStatement();
                // Initialization
                mySmartUpload.initialize(config, request, response);
                mySmartUpload.setMaxFileSize(500 * 1024);
                // Upload
                mySmartUpload.upload();
                //取得text框中的数据
                id = mySmartUpload.getRequest().getParameter("id");
                //取得文件和文件名
                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 + "====");
                    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 + "====");
                    //将数据插入到表中
                    String strSql = "insert into 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();
                    if(file!=null)//删除文件
                        file.delete();
                    out.println("upload sucess");
                } else {
                    out.println("no file");
                }
            } catch (Exception ex) {
                if(file!=null)
                    file.delete();
                out.println("upload fail");
                ex.printStackTrace();
            }
            System.out.println("============UpLoad end=============");
        }
    }
    自己修改一下就可以用了
      

  3.   

    楼上的朋友,我想要关于struts的例子,这个例子我已经在网上找到了。就是在jsp页面上用<html:file......>的,在action里处理文件上传的例子。哪位有的,给我一份。[email protected]
      

  4.   

    为什么要用jspsmartupload呢?对内存不好啊。建议你看看struts包里自带的,你看看struts-upload.war吧(就在你下载回的struts包里就有)。很简单的。