首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts1下传多个文件

2012-11-08 
struts1上传多个文件html:注意:form action*??mothedpost?enctypemultipart/form-data的设置ac

struts1上传多个文件

html:

注意:<form action="*"??mothed="post"?enctype="multipart/form-data">的设置

action类:

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadFileSampAction extends Action {

?public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response) {

??// 获得上传的文件信息
??Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
??String dirPath = getServlet().getServletContext().getRealPath("/")
????+ "upload";

??if (fileh != null) {
???// 如果目录不存在,则创建
???File fpath = new File(dirPath);
???if (!fpath.exists()) {
????fpath.mkdir();
???}
???// 解析上传文件
???for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
????String key = (String) it.next();
????try {
?????FormFile formfile = (FormFile) fileh.get(key);
?????String filename = formfile.getFileName().trim(); // 文件名
?????if (!"".equals(filename)) {

??????InputStream ins = formfile.getInputStream();
??????OutputStream os = new FileOutputStream(dirPath
????????+ File.separatorChar + filename);
??????System.out.println("=======file dirPath========="
????????+ dirPath);
??????System.out.println("=======file separatorChar========="
????????+ File.separatorChar);
??????System.out.println("=======file filename========="
????????+ filename);
??????int bytesRead = 0;
??????byte[] buffer = new byte[8192];
??????while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
???????os.write(buffer, 0, bytesRead);
??????}
??????os.close();
??????ins.close();

?????}
????} catch (Exception ex) {
?????ex.printStackTrace();
????}
???}
??}

??return mapping.findForward("success");
?}
}

struts-config.xml配置:

<?xml version="1.0" encoding="gb2312" ?>
<!DOCTYPE struts-config PUBLIC
????????? "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
????????? "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
?<form-beans>
?? <form-bean type="test.UploadFileSample"/>
?</form-beans>
?
? <action-mappings>

??<action??? path="/uploadfile"
?????? type="test.UploadFileSampAction"
??????
?????? scope="request">
???<forward ??? path="/test.html"/>
??</action>

? </action-mappings>


</struts-config>

?

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
???
??? <servlet>
??????? <servlet-name>action</servlet-name>
??????? <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
??????? ?<init-param>
????? ???<param-name>config</param-name>
????? ???<param-value>
?????/WEB-INF/struts-config.xml
????</param-value>
??? ??</init-param>
??? ?<load-on-startup>2</load-on-startup>
??? </servlet>

?<servlet-mapping>
??? ?<servlet-name>action</servlet-name>
??? ?<url-pattern>*.do</url-pattern>
?</servlet-mapping>


</web-app>

以上是主要的代码以及配置,最后需要部署到应用服务器上运行试试!!祝大家好运。

热点排行