你可以继承ActionServlet.java这个类,然后扩展它。曾经有一个项目修改时就是这么做的,不过既然你已经使用struts了,为什么不使用struts自带的上传组件?给你一个例子:
  //大字段上传
  public void blobUpload(DBAccess dba,FormFile file) throws ObjectException{
    try{
      InputStream stream = file.getInputStream();
      ResultSet rs=null;      if(file.getFileSize()>0){                         //有内容
        byte[] bytes=file.getFileData();
        dba.prepareStatement(SQL_INSERT_BLOB_UPLOAD);
        if(strInsertReturnValue!=null){
          dba.setString(1,strInsertReturnValue);
          rs=dba.executeQuery();
          if(rs.next()){                                //有返回结果
            oracle.sql.BLOB blob=((oracle.jdbc.OracleResultSet)rs).getBLOB("M_FILE");
            OutputStream outStream = blob.getBinaryOutputStream();
            outStream.write(bytes);                     //写入大字段
            outStream.flush();
          }
        }else{
          throw new ObjectException("上传大字段时没有取得m_id的值!");
        }
      }
    }catch(Exception ex){
      ex.printStackTrace();
      throw new ObjectException(ex.getMessage());
    }
  }  //大字段 下载
  public void blobDownload(DBAccess dba,String m_id,HttpServletResponse response) throws ObjectException{
    try{
      dba.prepareStatement(SQL_PROCESS_BLOB_DOWNLOAD);
      dba.clearParameters();
      dba.setString(1,m_id);
      ResultSet rs=dba.executeQuery();      if(rs.next()){
        InputStream inStream=rs.getBinaryStream("M_FILE");
        String fileType=rs.getString("M_FILETYPE");
        response.setContentType(fileType);
        byte[] b=new  byte[1024];
        int  len;
        while((len=inStream.read(b))>0){
          response.getOutputStream().write(b,0,len);
        }
        response.getOutputStream().flush();
        response.getOutputStream().close();
      }
    }catch(Exception ex){
      ex.printStackTrace();
      throw new ObjectException("大字段 下载时出错");
    }
  }
actionform这样:
import org.apache.struts.upload.FormFile;
private FormFile file=null;               //附件

解决方案 »

  1.   

    我已经说过了:
    因为已经将jspsmartupload打包(包含了很多其他功能),并多年使用,所以不想用struts的上传组件,还得再次重写很多东西。
    我看到论坛有人解决了,但没有说具体解决方法!
      

  2.   

    action类中pageContext = (PageContext) httpServletRequest.getSession().getAttribute("pageContext");应改为:action类的execute()函数中用pageContext = (PageContext) request.getSession().getAttribute("pageContext");
      

  3.   

    另外:
    SmartUpload mySmartUpload = new SmartUpload();
          mySmartUpload.initialize(getServlet().getServletConfig(),httpServletRequest,httpServletResponse);
          mySmartUpload.upload();
      

  4.   

    kui(kui):
    1、pageContext = (PageContext) httpServletRequest.getSession().getAttribute("pageContext");我是写在execute()方法里的2、SmartUpload mySmartUpload = new SmartUpload();
          mySmartUpload.initialize(getServlet().getServletConfig(),httpServletRequest,httpServletResponse);
          mySmartUpload.upload();
    用了你这个方法,执行到mySmartUpload.upload();时就死掉了,一直停在这里,cpu占用率也一直是98%以上,只有重启weblogic!是什么原因??请大家继续帮忙!!
      

  5.   

    对,kui说得对。cpu战用高的问题我没遇到过
      

  6.   

    将SmartUpload反编译,在SmartUpload里增加个方法
      public final void initialize(
          HttpServletRequest httpservletrequest,
          HttpServletResponse httpservletresponse)
          throws ServletException
      {
        m_application = httpservletrequest.getSession().getServletContext();
        m_request = httpservletrequest;
        m_response = httpservletresponse;
      }
    /*
    只要初始化  m_application,m_request, m_response这3个成员变量就可以了;
    这样就可以在execute方法里用了
    我已经试过可以上传*/
      

  7.   

    我有编译好的,如果你没做,可以写信给我,[email protected],我给你
    我修改过的SmartUpload,和在struts里的例子
      

  8.   

    nearsun(蓝冰) ,你的例子我试过了,
    页面如果按照普通的方式提交是可以的,如下是你的例子:
    <form method="post" enctype="multipart/form-data" action="uploadfilesAction.do">
      <p>
        <input type="file" name="f2">
        <br>
        <input type="submit" name="Submit" value="Submit">
        <input type="reset" value="Reset">
      </p>
    </form>但如果按照struts结构提交,则会陷入死循环:如下
    <html:form method="post" action="/Upload/uploadAction.do" enctype="multipart/form-data">
    <br><br>
      <html:text property="email"/>
    <input type="file" name="ff">  
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" value="Reset">
    </html:form>也就是把form标签换为<html:form...就不行了
    在up.upload() ;这一句进入死循环,cpu占用98%以上如果不设置up.setTotalMaxFileSize(10*1024*1024) ;这一句
    则报错为:Total File Size exceeded (1110);后来我看了源代码和网上有人的讨论,发现upload方法中这段程序如果j=-1就会出现死循环,按照网友提示的方法,加了一句:
        for (; i < m_totalBytes; i += j)
        {
          try
          {
            m_request.getInputStream();
            j = m_request.getInputStream().read(m_binArray, i, m_totalBytes - i);
            if(j==-1) throw new Exception();//这一句是加的
          }
          catch (Exception exception)
          {
            throw new SmartUploadException("Unable to upload.");
          }
        }
    现在不出现死循环,但永远报错:Unable to upload.无法上传文件,不知是怎么回事?难道不能改为<html:form..,因为我是要普通html标签和struts标签混和使用,所以必须改为这个.有没有其他的办法呢??
      

  9.   

    你可以用<html:form...,但struts里不要用类似if (actionForm instanceof InfoForm) {     contentForm = (InfoForm) actionForm;  这一句, 取得其它数据统统用      String id = httpServletRequest.getParameter("id");或
    String username = myUpload.getRequest().getParameter("username"); 就行了.
      

  10.   

    action 类里我没有用到actionFormaction类 代码如下:package commontools.upload;import org.apache.struts.action.*;
    import javax.servlet.http.*;
    import com.jspsmart.upload.*;public class UploadAction extends  Action {
      private String savepath = "D:/";
      public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        /**@todo: complete the business logic here, this is just a skeleton.*/
        //throw new java.lang.UnsupportedOperationException("Method perform() not yet implemented.");
        SmartUpload up = new SmartUpload();
        try
        {
          up.initialize(httpServletRequest, httpServletResponse);
          up.setMaxFileSize(10*1024*1024) ;
          up.setTotalMaxFileSize(10*1024*1024) ;
          up.upload() ;
          up.save(savepath) ;
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
        return actionMapping.findForward("success");
      }
    }
    struts-config.xml如下:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
    <struts-config>
      <form-beans>
        <form-bean name="uploadActionForm" type="commontools.upload.UploadActionForm" />
      </form-beans>
      <action-mappings>
        <action name="uploadActionForm" path="/Upload/uploadAction" scope="request" type="commontools.upload.UploadAction">
          <forward name="success" path="/Upload/up.jsp" />
        </action>
      </action-mappings>
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
      </plug-in>
    </struts-config>up.jsp如下:<%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <html>
    <head>
    <title>
    up
    </title>
    </head>
    <body bgcolor="#ffffff">
    <h1>
    JBuilder Generated JSP
    </h1>
    <html:form method="post" action="/Upload/uploadAction.do" enctype="multipart/form-data">
    <br><br>
      <html:text property="email">
      </html:text>
    <input type="file" name="ff">  
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" value="Reset">
    </html:form></body>
    </html>actionForm如下:<%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <html>
    <head>
    <title>
    up
    </title>
    </head>
    <body bgcolor="#ffffff">
    <h1>
    JBuilder Generated JSP
    </h1>
    <html:form method="post" action="/Upload/uploadAction.do" enctype="multipart/form-data">
    <br><br>
      <html:text property="email">
      </html:text>
    <input type="file" name="ff">  
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" value="Reset">
    </html:form></body>
    </html>
      

  11.   

    actionFrom:package commontools.upload;import org.apache.struts.action.*;
    import javax.servlet.http.*;public class UploadActionForm extends ActionForm {
      private String email;
      public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
        /**@todo: finish this method, this is just the skeleton.*/
        return null;
      }
      public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
      }
      public String getEmail() {
        return email;
      }
      public void setEmail(String email) {
        this.email = email;
      }
    }希望能在action类中通过actionFrom来取得页面中struts标签的值,普通的html标签的值通过
    String username = myUpload.getRequest().getParameter("username"); 这样的方法的到.但现在得到struts标签的值是正常的,但得到的普通html标签的值是空值.文件也无法上传.真搞不懂是什么原因!
      

  12.   

    up.initialize(httpServletRequest, httpServletResponse);改为
       up.initialize(getServlet().getServletConfig(),
                              httpServletRequest, httpServletResponse);   我的struts标签的值和普通html标签的值均可以得到,你看看是不是先up.upload()再获取普通值.
      

  13.   

    你试过改为<html:form>后能上传文件吗??
      

  14.   

    struts标签的值和普通html标签的值我也可以获得,但是不能上传文件呀!!!!
      

  15.   

    我发现在
    <html:form method="post" action="/Upload/uploadAction.do" enctype="multipart/form-data">
    以post方式提交时,一直报下面的错误:<Warning> <HTTP> <sder> <FSWebServer> <ExecuteThread: '12' for queue: 'weblogic.kernel.Default'> <<anonymous>> <> <BEA-101138> <ServletContext(id=13498482,name=CommonWeb,context-path=/CommonWeb) One of the getParameter family of methods called after reading from the ServletInputStream. Not merging post parameters.> 这个错误是什么意思?不能上传文件是否与此有关??
    我用的是weblogic8.1 sp2
    IDE是JBX