`
雨辰RainTime
  • 浏览: 112409 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts1 文件上传 FormFile

阅读更多
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/myAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>

package com.j2ee.zz.action;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.j2ee.zz.actionform.*;
import org.apache.struts.upload.*;
import java.io.*;

public class myAction extends Action {
  public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    /**@todo: complete the business logic here, this is just a skeleton.*/
    myActionForm myActionForm = (myActionForm) actionForm;
    if(actionForm instanceof myActionForm){
      String encoding = request.getCharacterEncoding();
      if(encoding != null && encoding.equalsIgnoreCase("UTF-8")){
        response.setContentType("text/html; charset=gb2312");
      }
      FormFile file = myActionForm.getTheFile();
      try{
        InputStream input = file.getInputStream();
        String path = request.getRealPath("/");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = new FileOutputStream(path+"/"+file.getFileName());

        System.out.println(path+"/"+file.getFileName());
        int i = 0;
        byte[] buff = new byte[8192];
        while((i = input.read(buff, 0, 8192)) != -1){
          out.write(buff,0,i);
        }
        out.close();
        input.close();
      }catch(Exception ex){
        System.out.println(ex.getMessage());
      }
      return actionMapping.findForward("succ");
    }
    return null;

  }
}

package com.j2ee.zz.actionform;


import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;

/**
 * <p>Title:UpLoadForm</p>
 * <p>Description: QRRSMMS </p>
 * <p>Copyright: Copyright (c) 2004 jiahansoft</p>
 * <p>Company: jiahansoft</p>
 * @author wanghw
 * @version 1.0
 */

public class myActionForm extends ActionForm {
  public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
  protected FormFile theFile;
  public FormFile getTheFile() {
      return theFile;
  }
  public void setTheFile(FormFile theFile) {
      this.theFile = theFile;
  }
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
  {
      ActionErrors errors = null;
      //has the maximum length been exceeded?
      Boolean maxLengthExceeded = (Boolean)
              request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
      if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
      {
          errors = new ActionErrors();
          errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
      }
      return errors;

  }
}



分享到:
评论

相关推荐

    struts 单文件上传和多文件上传带进度条

    struts 单文件上传和多文件上传带进度条

    基于Struts文件上传(FormFile)详解

    下面小编就为大家带来一篇基于Struts文件上传(FormFile)详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    struts框架中formFile批量上传图片

    struts formFile 批量上传图片 源代码

    struts2文件上传下载

    struts2文件上传与下载, &lt;s:fielderror&gt;&lt;/s:fielderror&gt; 文件"&gt;&lt;/s:file&gt; &lt;s:submit id="submit" value="sbumit"&gt;&lt;/s:submit&gt; &lt;/s:form&gt;

    解决struts组件上传附件速度很慢的问题

    解决struts组件上传附件速度很慢的问题

    Struts的文件上传处理

    Struts的文件上传 本文用的是Struts的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。

    struts1.x上传实例

    struts文件上传,struts upload组件文件上传 最基本的实例,将文件上传到应用服务器里 1.upload.jsp内容 &lt;form action="fileup.do" method="post" enctype="multipart/form-data" &gt; 文件:...

    struts 制作的文件上传小项目

    用struts制作的文件上传然后显示出来的小项目,主要运用了FormFile

    java 文件上传 SmartUpload.pdf SmartUpload.jar包

    java 文件上传 文件下载 Struts项目中提供FormFile类来处理文件上传

    FormFile上传图片

    早前我做JSP项目的时候,用过jspSmartUpload这个组件来上传和下载图片,但是现在做的是SSH整合的项目,而struts又提供了一个FormFile类来做文件的上传和下载,如果还用jspSmartUpload来做的话,有点说不过去,于是试...

    struts上传图片

    * 使用org.apache.strtus.upload.FormFile实现文件的单一上传 */ private void singleUploadFile(IndexActionForm objForm) { if(objForm.getFile().getFileSize() == 0|| BUFFER_SIZE ().getFileSize()...

    ssh框架集成,formFile批量上传完整版

    ssh框架集成,批量上传文件,简单易理解,使用struts自带jar包。

    strust文件上传

    /**//*截取上传文件的后缀名,此时得到了文件的类型*/ type = type.substring(i+1); } // 限制上传类型为jpg,txt,rar; if (!type.equals("jpg") && !type.equals("txt")&& !type.equals("bmp")) {//当上传的...

    Android实现上传文件到服务器实例详解

    一、Android端实现文件上传 1)、新建一个Android项目命名为androidUpload,目录结构如下: 2)、新建FormFile类,用来封装文件信息 package com.ljq.utils; import java.io.File; import java.io.FileInputStream...

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    extjs中本地照片预览、blob数据在oracle中存取

    环境为java,struts2,extjs...没有完全实现的是extjs用uploadfild控件实现的blob数据上传预览,并且尝试了用formFile上传文件、和smart组件上传,但两者在我的环境里还有问题,大家可以改进一下,本人觉得有总比没有强哈

Global site tag (gtag.js) - Google Analytics