服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

struts的文件上传

最近刚做完一个项目,用struts1.1做的。从不懂,到熟练使用,都靠参考csdn的一些文档。但是文章上讲的并不一定适合自己,所以我把我自己做的一些东西拿上来给大家看看,互相交流一下。如果您有跟好的方法,可以和我联系。
msn:whw_dream (at) hotmail.com

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

选择上传文件页面:selfile.jsp


<%@ taglib uri="/web-inf/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsaction.do" enctype="multipart/form-data">
<html:file property="thefile"/>
<html:submit/>
</html:form>
</html:html>

uploadaction.java
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.formfile;

/**
 * <p>title:uploadaction</p>
 * <p>description: qrrsmms </p>
 * <p>copyright: copyright (c) 2004 jiahansoft</p>
 * <p>company: jiahansoft</p>
 * @author wanghw
 * @version 1.0
 */

public class uploadaction extends action {
  public actionforward execute(actionmapping mapping,
                               actionform form,
                               httpservletrequest request,
                               httpservletresponse response)
      throws exception {
    if (form instanceof uploadsform) {//如果form是uploadsform
        string encoding = request.getcharacterencoding();
        if ((encoding != null) && (encoding.equalsignorecase("utf-8")))
        {
            response.setcontenttype("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
        }
        uploadform theform = (uploadform ) form;
        formfile file = theform.getthefile();//取得上传的文件
        try {
          inputstream stream = file.getinputstream();//把文件读入
          string filepath = request.getrealpath("/");//取当前系统路径
          bytearrayoutputstream baos = new bytearrayoutputstream();
          outputstream bos = new fileoutputstream(filepath + "/" +
                                                  file.getfilename());//建立一个上传文件的输出流
          //system.out.println(filepath+"/"+file.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){
          system.err.print(e);
        }
        //request.setattribute("dat",file.getfilename());
        return mapping.findforward("display");
    }
    return null;
  }
}



uploadform.java

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 uploadform 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;

  }
}
//这是相对应的form,还有其他属性可以设置,具体可以参考struts的上传例子。



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="uploadsform" type="uploadform" />
  </form-beans>
  <action-mappings>
    <action name="uploadsform" type="uploadaction" path="/uploadsaction">
      <forward name="display" path="/display.jsp" />
    </action>
  </action-mappings>
</struts-config>
<!--display.jsp就是随便写一个成功页-->

扫描关注微信公众号