struts混合文件上传怎么写啊!
我以前用的是SmartUpload不知道
struts怎么写

解决方案 »

  1.   

    混合文件是什么意思?
    struts上传用FormFile
      

  2.   

    楼主跟我遇到的问题类似,估计也是imput= text,areatext,file这样混杂在一起的一个form上传吧,
    我的帖子现在还挂在首页呢,根据这两天我找的资料,struts不支持file类型的文件上传,用struts2吧,有自带的上传功能,
    大概说下就是定义个arraylist数组,用inputStrean OutputStream流的形式完成,具体的Struts2由于是个新东西我也不太懂
    楼主可以去看看我的帖子:? 使用smartupload上传组件时,出现乱码.如何解决? 
    互相交流下,用SmartUpload+struts上传附件好象不行
      

  3.   

    sunlights 你好我知道怎么做了
    先写一个FormBean
    import org.apache.struts.upload.FormFile;把upload.FormFile包导入
    public class AddgoodsForm extends ActionForm {
    /*
     * Generated fields
     */
        private FormFile formFile;
    /** img property */
    private String img; /** price property */
    private String price;
        
    private String timeprice;
    /** begintime property */
    private String begintime; /** depict property */
    private String depict; /** endtime property */
    private String endtime; /** gdsname property */
    private String gdsname; /*
     * Generated Methods
     */ /** 
     * 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
    } /** 
     * Returns the img.
     * @return String
     */
    public String getImg() {
    return img;
    } /** 
     * Set the img.
     * @param img The img to set
     */
    public void setImg(String img) {
    this.img = img;
    } /** 
     * Returns the price.
     * @return String
     */
    public String getPrice() {
    return price;
    } /** 
     * Set the price.
     * @param price The price to set
     */
    public void setPrice(String price) {
    this.price = price;
    } /** 
     * Returns the begintime.
     * @return String
     */
    public String getBegintime() {
    return begintime;
    } /** 
     * Set the begintime.
     * @param begintime The begintime to set
     */
    public void setBegintime(String begintime) {
    this.begintime = begintime;
    } /** 
     * Returns the depict.
     * @return String
     */
    public String getDepict() {
    return depict;
    } /** 
     * Set the depict.
     * @param depict The depict to set
     */
    public void setDepict(String depict) {
    this.depict = depict;
    } /** 
     * Returns the endtime.
     * @return String
     */
    public String getEndtime() {
    return endtime;
    } /** 
     * Set the endtime.
     * @param endtime The endtime to set
     */
    public void setEndtime(String endtime) {
    this.endtime = endtime;
    } /** 
     * Returns the gdsname.
     * @return String
     */
    public String getGdsname() {
    return gdsname;
    } /** 
     * Set the gdsname.
     * @param gdsname The gdsname to set
     */
    public void setGdsname(String gdsname) {
    this.gdsname = gdsname;
    } public FormFile getFormFile() {
    return formFile;
    } public void setFormFile(FormFile formFile) {
    this.formFile = formFile;
    } public String getTimeprice() {
    return timeprice;
    } public void setTimeprice(String timeprice) {
    this.timeprice = timeprice;
    }
    }
    然后再写Action
    import org.apache.struts.upload.FormFile;同样把upload.FormFile包导入
    public ActionForward addgoods(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws ServletException, ParseException {
    AddgoodsForm addgoodsForm = (AddgoodsForm) form;// TODO Auto-generated method stub
        ActionMessages errors=new ActionMessages();
    if(request.getSession().getAttribute("users")==null)//验证用户是否已经登录
    {
    return mapping.findForward("saleList");
    }
    //所添加的商品不能为空
        if(addgoodsForm.getGdsname().equals("")||addgoodsForm.getPrice().equals(""))
    {
       errors.add("goods",new ActionMessage("errors.goodsEx"));
       super.saveErrors(request, errors);
       return mapping.getInputForward();
    }

        FormFile formfile=addgoodsForm.getFormFile();//取得上传的文件
        List<String> list=new ArrayList<String>();
        list.add("jpg");
        list.add("jpeg");
        list.add("gif");
        list.add("bmp");
        fileName=formfile.getFileName();// 获得文件名
        String ext=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
          // 获取文件类型,即扩展名,通过String类的substring方法截取字符串,lastIndexOf获取某个字符串最后出现的索引。
        ext=ext.toLowerCase();      // 文件后缀转换成小写
       
        if(!list.contains(ext))//判断上传文件类型
        {
           errors.add("fileEx",new ActionMessage("errors.fileExcption"));
           super.saveErrors(request, errors);
           return mapping.getInputForward();
        }
        
        try {
      InputStream stream=formfile.getInputStream();// 把文件读入
      String filepath=request.getRealPath("/images");// 上传到指定的images包中
      OutputStream bos=new FileOutputStream(filepath+"/"+formfile.getFileName());// 建立一个上传文件的输出流
      int bytesRead=0;
      byte[] buffer=new byte[8192];
      while((bytesRead = stream.read(buffer,0,8192))!=-1){
          bos.write(buffer,0,bytesRead);// 将文件写入服务器
      }
       bos.close();
       stream.close();
    }catch (Exception e) {
    // TODO: handle exception
          System.err.print(e);
    }
    if(savegoods(addgoodsForm,request))
         return mapping.findForward("saleList");
    return null;
     }
    然后界面
    <html:form action="/addgoods?opr=addgoods" method="post" enctype="multipart/form-data">
           <table border="1" align="center">
             <tr>
               <td align="right">商品名:</td>
               <td><html:text property="gdsname"/></td>
             </tr>
             <tr>
               <td align="right">起拍价:</td>
               <td><html:text property="price"/><html:errors property="goods"/></td>
             </tr>
             <tr>
               <td align="right">幅度增长价:</td>
               <td><html:text property="timeprice"/></td>
             </tr>
             <tr>
               <td align="right">图片:</td>
               <td><html:file  property="formFile" onchange="PreviewImg(this)" /><html:errors property="fileEx"/>
              </tr>
              <tr><td colspan="2" align="center"><div id="newPreview"></div></td></tr>
             <tr>
               <td align="right">描述:</td>
               <td><html:textarea property="depict"></html:textarea></td>
             </tr>
             <tr>
               <td align="right">开拍时间:</td>
               <td><html:text property="begintime"/></td>
             </tr>
             <tr>
               <td align="right">截至时间:</td>
               <td><html:text property="endtime"/></td>
             </tr>
             <tr><td colspan="2" align="center"><html:submit value="提   交"/></td></tr>
           </table>
         </html:form>希望对大家有用谢谢!
      

  4.   

    恩,最近我也一直在看这一块的内容
        try { 
      InputStream stream=formfile.getInputStream();// 把文件读入 
      String filepath=request.getRealPath("/images");// 上传到指定的images包中 
      OutputStream bos=new FileOutputStream(filepath+"/"+formfile.getFileName());// 建立一个上传文件的输出流 
      int bytesRead=0; 
      byte[] buffer=new byte[8192]; 
      while((bytesRead = stream.read(buffer,0,8192))!=-1){ 
          bos.write(buffer,0,bytesRead);// 将文件写入服务器 
      } 
    最主要还是上面只一段,就像我前面说的核心内容是用InputStream  OutputStream 流的形式做,其实smartupload组件我们也是用的他具体的几个类而已,只不过是人家已经弄好的.给我们用import导入java.io.*   java.util*之类的包里的类很相似