我想在struts中实现表彰和一个文件的同时上传,怎么实现?????????

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【LOVEYOULOVE】截止到2008-06-25 17:38:23的历史汇总数据(不包括此帖):
    发帖数:16                 发帖分:730                
    结贴数:16                 结贴分:730                
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    在表单中使用添加属性enctype值设置为"multipart/form-data",相当于告诉struts控制器,有多媒体上传,需要处理
    然后在表单对应的FormBean中用FormFile类型对象接受,这样就可以在Form中获得文件了,这个对象有很多方法,你自己用一下,可以感受更深,其中有一个getFileData()方法可以获得上传文件的数据。
    希望对你有用!
      

  3.   

    只要在ActionForm中定义一个变量为FormFile类型,然后set get相应属性在然后应用FormFile来操作上传的文件,用流写入就ok了表单其他项都直接从ActionForm中取就可以了
      

  4.   

    chengjf0526 说得对,而且还可以一次上传多个文件呢
      

  5.   

    1----------编写jsp
    /***************  <body>    文件止传
        <html:form action="htmlForm" enctype="multipart/form-data" >
        <html:file property="file"></html:file>
    //里面还有别的东西
        <html:submit></html:submit>
        </html:form>
      </body>
      
      *******************/
      
     2 -----------------编写FORM
     /********************
      /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.lovo.struts.form;import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;/** 
     * MyEclipse Struts
     * Creation date: 04-28-2008
     * 
     * XDoclet definition:
     * @struts.form name="htmlForm"
     */
    public class HtmlForm extends ActionForm {
    /*
     * Generated Methods
     */
        //可能还有别的属性 
    FormFile file;
    public FormFile getFile() {
    return file;
    } public void setFile(FormFile file) {
    this.file = file;
    } /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    // TODO Auto-generated method stub
    return null;
    } /** 
     * Method reset
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    // TODO Auto-generated method stub
    }
    }*********************************/
    3.编写action
    /*************************************
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.lovo.struts.action;import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;import com.lovo.struts.form.HtmlForm;/** 
     * MyEclipse Struts
     * Creation date: 04-29-2008
     * 
     * XDoclet definition:
     * @struts.action path="/htmlForm" name="htmlForm" scope="request" validate="true"
     */
    public class HtmlFormAction extends Action {
    /*
     * Generated Methods
     */ /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    HtmlForm htmlForm = (HtmlForm) form;// TODO Auto-generated method stub
    FormFile file = htmlForm.getFile();
    String dir = servlet.getServletContext().getRealPath("/upload");
    String fname = file.getFileName();
    OutputStream out = null;
    InputStream in = null;
    try {
    in = file.getInputStream();
    out = new FileOutputStream(dir+"/"+fname);
    int b = 0;
    while((b = in.read())!=-1){
    out.write(b);

    }
    out.flush();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    in.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    out.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return null;
    }
    }************************************************/
    4.在WebRoot下建一个文件
    ************************************************/名叫upload
    本文件上传是用struts框架
      

  6.   

    我也遇到了这个问题,没有用form 直接在action里获取表单内容,但是获取不到。