struts上传文件的方法

     

用到的类import org.apache.struts.upload.FormFile;

Jsp上传页面:

<%@ page contentType="text/html; charset=GBK" %>

<html>

<head>

<title>

strutsUploadForm

</title>

</head>

<body bgcolor="#ffffff">

<h1>
上传测试
</h1>

<form action="uploadTestAction.do" method="post" enctype="multipart/form-data" name="form1">
 <p>文本
    <input name="theText" type="text" id="theText">
</p>
  <p>文件
   <input name="theFile" type="file" id="theFile">
</p>
<p>保存到
    <input name="saveTo" type="text" id="saveTo">
</p>
  <p>
    <input type="submit" name="Submit" value="提交">
</p>
</form>
</body>
</html>

actionForm

package hehe;

import org.apache.struts.action.*;

import javax.servlet.http.*;

import org.apache.struts.upload.FormFile;

public class UploadTestForm extends ActionForm {

  private String saveTo;

  private String theText;

  private org.apache.struts.upload.FormFile theFile;//文件框对应的是formFile类型

  public String getSaveTo() {

    return saveTo;

  }

  public void setSaveTo(String saveTo) {

    this.saveTo = saveTo;

  }

  public org.apache.struts.upload.FormFile getTheFile() {

    return theFile;

  }

  public void setTheFile(org.apache.struts.upload.FormFile theFile) {

    this.theFile = theFile;

  }

  public String getTheText() {

    return theText;

  }

  public void setTheText(String theText) {

    this.theText = theText;

  }

  public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    /**@todo: finish this method, this is just the skeleton.*/

    if(!this.getTheFile().getContentType().equals("image/pjpeg")){

      System.out.println("不是jpg");

    }//可以判断类型

    if(this.getTheFile().getFileSize()>100){

      System.out.println("长度大于1000");

    }//可以判断大小

    return null;

  }

  public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    saveTo = null;

    theFile = null;

    theText = null;

  }

}

Action

package hehe;

import org.apache.struts.action.*;

import org.apache.struts.upload.FormFile;

import javax.servlet.http.*;

import java.io.*;

public class UploadTestAction

    extends Action {

  public ActionForward execute(ActionMapping actionMapping,

                               ActionForm actionForm,

                               HttpServletRequest request,

                               HttpServletResponse response) {

    UploadTestForm uploadForm = (UploadTestForm) actionForm;

    FormFile file = uploadForm.getTheFile();

    String path = uploadForm.getSaveTo();

    String theText = uploadForm.getTheText();

    try {

      InputStream input = file.getInputStream();//能从FormFile中获得输入流

      OutputStream output = new FileOutputStream(path);

      int bytesReader = 0;

      byte[] readbuffer = new byte[8192];

      while ( (bytesReader = input.read(readbuffer, 0, 8192)) != -1) {

        output.write(readbuffer, 0, bytesReader);

      }

      output.close();

    }

    catch (Exception e) {

      e.printStackTrace();

    }

    request.setAttribute("theText", theText);

    request.setAttribute("fileName", file.getFileName());//上传的文件名

    request.setAttribute("fileSize", new Integer(file.getFileSize()));//文件大小

    request.setAttribute("fileType", file.getContentType());//文件类型

    return actionMapping.findForward("success");

  }

}

posted @ 2008-02-21 09:18 Have a try 阅读(145) 评论(5)  编辑 收藏 所属分类: 技术专题

  回复  引用  查看    
#1楼 [楼主] 2008-02-21 09:18 | Have a try      
这个方法还是挺简单的,不过我研究了半天还是不知道文件上传到哪了?
  回复  引用    
#2楼  2008-02-21 11:28 | hya [未注册用户]
在struts中上传文件,默认用的是apache的fileupload组件,添加struts后,会看到commons-fileupload.jar,这就是fileupload组件.其灵活度很高的,建议看一下http://java.chinaitlab.com/Struts/716569.html
  回复  引用  查看    
#3楼  2008-02-22 22:02 | 青龙张二      
呵呵,不顶不行~
  回复  引用  查看    
#4楼  2008-02-23 23:29 | 青龙张二      
胡总发的是关于struts2的啊,太先进了吧
  回复  引用  查看    
#5楼 [楼主] 2008-02-25 17:36 | Have a try      
胡总我看了,很好!收藏起来!

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-02-25 17:40 编辑过
 
另存  打印