struts04:多文件上传、文件上传与国际化

第一:概念

1.表单的enctype属性指定的是表单数据的编码格式,该属性有三个值:
 ---1,application/x-www-from-urlencoded:这是默认的编码方式,他只处理表单域里的value属性,采用这种编码方式的表单会将表单域中的值处理成url编码方式。
      注释:以这种方式提交带图片的表单的时候,请求参数是用二进制的形式读到的三个请求参数以及对应的值。实际上web服务器替我们处理了这个二进制流并将二进制流
        转换成了对应的请求参数的值。但是即使通过底层的二进制输入流,一样可以读到请求的内容:一个普通的字符串,这个字符串包含了三个请求参数,分别是file、wawa、dd.
        这是通过HttpServletRequest和getparameter来获得正确的参数。
---2.multipart/from-data:这种编码方式会以二进制流的形式来处理表单数据,这种编码方式会把文件域指定文件的内容页封装到请求参数里。
      注释:一旦设置了enctype=multipart/from-data就无法通过HttpServletRequest对象的getparameter方法获取请求参数
---3.text/plain:这种编码方式当表单的action属性为mailto:URL的形式时比较方便,这种方式主要适用于直接通过表单发送邮件的方式。

 

2,多文件上传:
---1.action层的属性都变成了数组
----2.在struts.xml中配置允许上传的最大长度。
<constant name="struts.multipart.maxSize" value="10701096"/>
<constant name="struts.custom.i18n.resources" value="lang"/>

 

 

第二:多文件上传

1.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <form action="uploadAction.action" method="post"  enctype="multipart/form-data">
      img:<input type="file" name="img" value=""><br/>
      img:<input type="file" name="img" value=""><br/>
      img:<input type="file" name="img" value=""><br/>
      img:<input type="file" name="img" value=""><br/>
     <input type="submit" name="sub" value=" 上 传 "><br/>
    </form>
  </body>
</html>

2.struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <package name="upload" namespace="" extends="struts-default">
  <action name="uploadAction" class="action.UploadAction">
   <result name="success">/success.jsp</result>
  </action>
 </package>
</struts>   

 

 

3.action层

package action;

import java.io.File;
import java.util.Locale;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

 private File img[];
 private String imgFileName[];
 private String imgContentType[];
 
 
 public File[] getImg() {
  return img;
 }


 public void setImg(File[] img) {
  this.img = img;
 }


 public String[] getImgFileName() {
  return imgFileName;
 }


 public void setImgFileName(String[] imgFileName) {
  this.imgFileName = imgFileName;
 }


 public String[] getImgContentType() {
  return imgContentType;
 }


 public void setImgContentType(String[] imgContentType) {
  this.imgContentType = imgContentType;
 }


 public String execute()
 {

  String path = ServletActionContext.getServletContext().getRealPath("img");
  try
  {
   System.out.println(img);

   for(int x=0;x<img.length;x++)
   {
    String fname=imgFileName[x];
    
    //在服务器上创建相应的文件对象
    File f = new File(path+"/"+fname);
    
    //获得上传的内存中的文件对象
    File oldFile = img[x];
    
    //将上传的文件拷贝到服务器的文件中
    FileUtils.copyFile(oldFile, f);
    
    
   }
 
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  
  return "success";
 }
}

 

第三:文件上传与国际化

1.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
  <span style="color: red"><s:fielderror/></span>
   <form action="upload.action" method="post" enctype="multipart/form-data"><%--
   <!-- 显示资源文件的value的一种方法 -->
  <s:i18n name="UploadAction">
  <s:text name="t"></s:text>
 </s:i18n><br>
 <!--显示资源文件的值的第二种方法-->
  <s:text name="t"></s:text> <br>
      --%>
      <s:text name="t"/>:<input type="text" name="titel"><br/>
      <s:text name="i"/>:<input type="file" name="upload"><br/>
      <input type="submit" value="上传">                             
   </form>
   <s:debug></s:debug>
  </body>
</html>

 

 

2.struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!-- 指定国际化资源文件baseName为globalMessages -->
 <constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
 <!-- 给全局国际化资源文件配置的 -->
 <constant name="struts.custom.i18n.resources" value="lang"></constant>
 <!-- 设置该应用试验的解码集 -->
 <constant name="struts.i18n.encoding" value="UTF-8"></constant>
 <package name="lee" namespace="/" extends="struts-default">
     <action name="upload" class="action.UploadAction">
               <interceptor-ref name="fileUpload" ><!-- 这是jar包中的类名 ,struts2的拦截器类-->
             <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg </param><!--  设置允许上传的文件的类型 -->
             <param name="maximumSize">200</param> <!-- 设置允许上传文件的文件大小 -->
        </interceptor-ref>    
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <!-- 动态设置action的属性,设置图片保存路径。也就是保存在tomcat中项目的根目录下的upload文件夹中 -->
        <param name="savePath">/upload</param>
        <result name="input">/index.jsp</result><!-- 上传失败重新上传 -->
        <result>/success.jsp</result>
     </action>
    </package>
</struts>   

 

 

3.action层:

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
 private String title;//封装文件标题请求参数
 private File upload;//封装文件上传域,也就是文件上传到哪了。*
 private String uploadContentType;//获得上传文件类型。*
 private String uploadFileName;//获得上传文件名*
 private String savePath;//直接在struts.xml中配置的属性。依赖注入
/* //由于需要过滤,因此加属性
 private String allowTypes;//允许上传的类型
 
 
 
 public String getAllowTypes() {
  return allowTypes;
 }
 public void setAllowTypes(String allowTypes) {
  this.allowTypes = allowTypes;
 }*/
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public File getUpload() {
  return upload;
 }
 public void setUpload(File upload) {
  this.upload = upload;
 }
 public String getUploadContentType() {
  return uploadContentType;
 }
 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }
 public String getUploadFileName() {
  return uploadFileName;
 }
 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }
 public String getSavePath() {
  //return savePath;
  System.out.println("输出保存位置:"+ServletActionContext.getServletContext().getRealPath(savePath));
  return ServletActionContext.getServletContext().getRealPath(savePath);
 }
 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }
 
 /*
 //过滤上传的文件,types是允许上传的文件类型
  public String filterType(String[] types){
   System.out.println("该文件的类型:"+uploadContentType);
   //获取当前正在上传的文件类型
   String filetype=this.getUploadContentType();
   for(String type:types){
    if(type.equals(filetype)){
     return null;
    }
   }
   return INPUT;
  }*/
 @Override
 public String execute() throws Exception {
  System.out.println("---------"+uploadContentType);
  /*System.out.println("========="+this.getAllowTypes());
  System.out.println(getAllowTypes().split(","));
  //将允许上传的文件类型的字符串以逗号分开,放入字符串中,从而判断当前文件类型是否允许上传。filterRequest是接收filterType方法的返回值
  String filterRequest=this.filterType(this.getAllowTypes().split(","));
  System.out.println("看一看:"+filterRequest);
  if(filterRequest!=null){
   ActionContext.getContext().put("typeError", "你要上传的文件类型不正确");
   return filterRequest;
  }
  */
  
  //以服务器的文件保存地址和原文件名建立上传文件的输出流
  FileOutputStream fos=new FileOutputStream(getSavePath()+"/"+getUploadFileName());//调用了两个get方法
  FileInputStream fis=new FileInputStream(this.getUpload());
  byte[] buffer=new byte[1024];
  int len=0;
  while((len=fis.read(buffer))>0){
   fos.write(buffer, 0, len);
  }
  return SUCCESS;
 }

}

 

 

你可能感兴趣的:(struts04:多文件上传、文件上传与国际化)