多文件上传

来源:互联网 发布:东方财富 数据 编辑:程序博客网 时间:2024/06/11 12:51

struts,DynaActionForm,数组,文件上传

源代码 2007-11-16 14:50:23 阅读44 评论0 字号:

 

1. JSP页,文件上传,表单中有同名的属性

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
 <head>
  <title>JSP for DynaActionForm form</title>
 </head>
 <body>
  <html:form action="/user.do" method="POST" enctype="multipart/form-data">
   userId : <html:text property="userId"/><br/>
   salary : <html:text property="salary"/><br/>
  <html:multibox property="hob" value="Computer" />Computer
  <html:multibox property="hob" value="Ball" />Ball
  <html:multibox property="hob" value="Book" />Book<br>
  <html:file property="file"></html:file>
   <html:submit/><html:cancel/>
  </html:form>
 </body>
</html>

2. struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
  <data-sources />
  <form-beans >
    <form-bean type="org.apache.struts.action.DynaActionForm">
      <form-property type="java.lang.String" />
      <form-property type="java.lang.Double" />
      <form-property type="java.lang.String[]" />
      <form-property type="org.apache.struts.upload.FormFile"/>
    </form-bean>
  </form-beans>
 <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userForm"
      input="/user.jsp"
     
      path="/user"
      scope="request"
      type="com.sun.form.struts.action.UserAction">
      <forward path="/ok.jsp" />
    </action>

  </action-mappings>

  <message-resources parameter="com.sun.form.struts.ApplicationResources" />
</struts-config>

3. UserAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.sun.form.struts.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.ModuleException;

/**
 * MyEclipse Struts
 * Creation date: 11-02-2006
 *
 * XDoclet definition:
 * @struts.action path="/user" input="/user.jsp" scope="request" validate="true"
 * @struts.action-forward path="/ok.jsp"
 */
public class UserAction extends Action {

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DynaActionForm userForm = (DynaActionForm) form;// TODO Auto-generated method stub
  String userId=userForm.getString("userId");
  
  double salary=Double.parseDouble(userForm.get("salary").toString());
  
  String []phone=userForm.getStrings("hob");
  String dir=servlet.getServletContext().getRealPath("/photo");
  System.out.println(phone[0]);
  System.out.println(phone[1]);
  
  FormFile file=(FormFile)userForm.get("file");
  InputStream streamIn;
  try {
   streamIn = file.getInputStream();
   OutputStream streamOut=new FileOutputStream(dir+"/"+file.getFileName());
   int bytesRead=0;
   byte[] buffer=new byte[8192];
   while((bytesRead=streamIn.read(buffer,0,8192))!=-1)
   {
    streamOut.write(buffer,0,bytesRead);
   }
   streamOut.close();
   streamIn.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   new ModuleException("error.exception");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   new ModuleException("error.exception");
  }
  file.destroy();
  //System.out.println(userId);
  return null;
 }
}

原创粉丝点击