Servlet实现文件上传(多文件)(三)

1、上传文件的页面fileUpload2.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 'fileUpload.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="fileUpload2.action" method="post" enctype="multipart/form-data">
 
    username: <input type="text" name="username"><br>
    file: <input type="file" name="file"><br>
    file2: <input type="file" name="file"><br>
    file3: <input type="file" name="file"><br>
 
    <input type="submit" value="submit">
 
    </form>
 
  </body>
</html>
2、UploadAction2.java用于处理上传的action
package com.shengsiyuan.struts2;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class UploadAction2 extends ActionSupport
{
    private String username;
 
    private List<File> file;  //以List方式定义,以为是多文件上传,泛型定义为File
 
    private List<String> fileFileName;  //文件的名称
 
    private List<String> fileContentType;  //文件的类型
 
    public String getUsername()
    {
        return username;
    }
 
    public void setUsername(String username)
    {
        this.username = username;
    }
 
    public List<File> getFile()
    {
        return file;
    }
 
    public void setFile(List<File> file)
    {
        this.file = file;
    }
 
    public List<String> getFileFileName()
    {
        return fileFileName;
    }
 
    public void setFileFileName(List<String> fileFileName)
    {
        this.fileFileName = fileFileName;
    }
 
    public List<String> getFileContentType()
    {
        return fileContentType;
    }
 
    public void setFileContentType(List<String> fileContentType)
    {
        this.fileContentType = fileContentType;
    }
 
    @Override
    public String execute() throws Exception
    {
        for(int i = 0; i < file.size(); i++)
        {
            InputStream is = new FileInputStream(file.get(i));  //先定义输入流
 
            String root = ServletActionContext.getRequest().getRealPath("/upload"); //定义上传位置
 
            File destFile = new File(root, fileFileName.get(i));  //定义File必须的路径和名称
 
            OutputStream os = new FileOutputStream(destFile);  //定义输出流
 
            byte[] buffer = new byte[400];
 
            int length = 0;
 
            while(-1 != (length = is.read(buffer)))
            {
                os.write(buffer, 0, length);
            }
 
 
            is.close();
            os.close();
        }
 
        return SUCCESS;
    }
 
}
3、页面结果页fileUploadResult2.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 'fileUploadResult2.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>
 
  username: <s:property value="username"/><br>
 
  <s:iterator value="fileFileName" id="f">
 
  file: <s:property value="#f.toUpperCase()"/><br>
 
  </s:iterator>
  </body>
</html>




posted @ 2013-12-25 14:19  Tim&Blog  阅读(201)  评论(0编辑  收藏  举报