网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  分享:用struts上传多个文件的方法     
  文章作者:未知  文章来源:水木森林  
  查看:104次  录入:管理员--2007-11-17  
 
  最近在做struts项目时遇到了上传多个文件的问题。在网上查了不少资料,也没有找到用struts上传多个文件的例子。我经过几天的研究,实现了用struts上传多个文件的功能。现在贴出来让大家共享!
  
  一。建立actionform
  
  package com.cnehu.struts.form;
  
  import javax.servlet.http.httpservletrequest;
  
  import org.apache.struts.action.actionerror;
  
  import org.apache.struts.action.actionerrors;
  
  import org.apache.struts.action.actionform;
  
  import org.apache.struts.action.actionmapping;
  
  import org.apache.struts.upload.formfile;
  
  import org.apache.struts.upload.multipartrequesthandler;
  
  /**
  
  * <p>
  
  * title:uploadform
  
  * </p>
  
  * <p>
  
  * copyright: copyright (c) 2005 techyang http://blog.csdn.net/techyang
  
  * </p>
  
  * @author techyang
  
  * @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;
  
  protected formfile thefile2;
  
  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;
  
  }
  
  /**
  
  * @return returns the thefile2.
  
  */
  
  public formfile getthefile2()
  
  {
  
  return thefile2;
  
  }
  
  /**
  
  * @param thefile2 the thefile2 to set.
  
  */
  
  public void setthefile2(formfile thefile2)
  
  {
  
  this.thefile2 = thefile2;
  
  }
  
  }
  
  二。建立actionservlet
  
  package com.cnehu.struts.action;
  
  import java.io.*;
  
  import javax.servlet.http.*;
  
  import org.apache.struts.action.*;
  
  import org.apache.struts.upload.formfile;
  
  import com.cnehu.struts.form.uploadform;
  
  /**
  
  * <p>
  
  * title:uploadaction
  
  * </p>
  
  * <p>
  
  * copyright: copyright (c) 2005 techyang http://blog.csdn.net/techyang
  
  * </p>
  
  * @author techyang
  
  * @version 1.0
  
  */
  
  public class uploadaction extends action
  
  {
  
  public actionforward execute(actionmapping mapping, actionform form,
  
  httpservletrequest request, httpservletresponse response)
  
  throws exception
  
  {
  
  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();//取得上传的文件
  
  formfile file2=theform.getthefile2();
  
  try
  
  {
  
  /*
  
  * 取当前系统路径d:/tomcat5/webapps/coka/ 其中coka 为当前context
  
  */
  
  string filepath = this.getservlet().getservletcontext()
  
  .getrealpath("/");
  
  inputstream stream = file.getinputstream();//把文件读入
  
  bytearrayoutputstream baos = new bytearrayoutputstream();
  
  /*
  
  * 建立一个上传文件的输出流 如果是linux系统请把uploadfiles后的"//"换成"/"
  
  */
  
  outputstream bos = new fileoutputstream(filepath +
  
  "uploadfiles//"+file.getfilename());
  
  //d:/tomcat5/webapps/coka/uploadfiles/dsc01508.jpg
  
  /* system.out.println(filepath +
  
  "uploadfiles//"+file.getfilename());
  
  system.out.println(filepath);*/
  
  request.setattribute("filename",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();
  
  inputstream stream2 = file2.getinputstream();//把文件读入
  
  bytearrayoutputstream baos2 = new bytearrayoutputstream();
  
  outputstream bos2 = new fileoutputstream(filepath +
  
  "uploadfiles//"+file2.getfilename());//建立一个上传文件的输出流
  
  int bytesread2 = 0;
  
  byte[] buffer2 = new byte[8192];
  
  int i=0;
  
  while ((bytesread2 = stream2.read(buffer2, 0, 8192)) != -1)
  
  {
  
  bos2.write(buffer2, 0, bytesread2);//将文件写入服务器
  
  }
  
  bos2.close();
  
  stream2.close();
  
  } catch (exception e)
  
  {
  
  system.err.print(e);
  
  }
  
  return mapping.findforward("display");
  
  }
  
  }
  
  三。建立上传用的jsp文件 upload.jsp
  
  <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
  
  <html:html>
  
  <head>
  
  <title>用struts上传文件</title>
  
  </head>
  
  <body>
  
  <html:form action="/uploadsaction" enctype="multipart/form-data">
  
  <html:file property="thefile"/>
  
  <html:file property="thefile2"/>
  
  <html:submit/>
  
  </html:form>
  
  </body>
  
  </html:html>
  
  四。配置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>
  
  <data-sources />
  
  <form-beans >
  
  <form-bean name="uploadsform" type="com.cnehu.struts.form.uploadform" />
  
  </form-beans>
  
  <global-exceptions />
  
  <global-forwards >
  
  </global-forwards>
  
  <action-mappings >
  
  <action name="uploadsform" type="com.cnehu.struts.action.uploadaction" path="/uploadsaction">
  
  <forward name="display" path="/display.jsp" />
  
  </action>
  
  </action-mappings>
  
  </struts-config>
 
 
上一篇: struts标签中的多层logic:iterator详解    下一篇: struts异常机制(exception handling)
  相关文档
java入门之开始面向对象的编程 11-17
weblogic使用jmx监控应用程序内、外部的状况 11-17
用java实现利用搜索引擎收集网址的程序 11-16
servlets简介 11-17
java 进阶:分享 struts 多模块的技巧 11-17
enterprise javabeans查询语言(1) 11-17
从c语言转向java 有哪些需要注意的事项 11-16
count 属性 11-16
(转贴)java的数据库应用 11-17
sun认证java程序员考试大纲 11-16
如何实现applet之间跨浏览器窗口的通信 11-17
eclipse netbeans intellij的实用对比 11-16
spring中的template和callback模式 11-17
linux 平台的 jvm 性能评测 11-17
java虚拟机类装载:原理、实现与应用 11-17
disksuite做raid 0的note 11-17
java 理论与实践: 哈希 11-17
学一个简单的jfreechart使用方法 11-17
使用java生成pdf文档 11-17
java设计模式之memento(恢复对象原状态) 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息