zoukankan      html  css  js  c++  java
  • 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>




  • 相关阅读:
    Matlab2014+VS2013配置vlfeat0.9.20 2016-10-27 15:03 223人阅读 评论(0) 收藏
    scikit-learn SVM使用和学习 2016-08-21 16:34 214人阅读 评论(0) 收藏
    python部落习题笔记 标签: python 2016-07-14 16:08 248人阅读 评论(0) 收藏
    NBNN及SIS Measure 标签: 稀疏相似度度量NBNNSIS分类 2016-04-16 10:49 754人阅读 评论(0)
    压缩感知的MP算法 标签: 压缩感知稀疏表示MP算法 2016-03-19 20:41 594人阅读 评论(0)
    K-means 处理 RGB 图像 标签: clusteringkmeansRGBmatlab 2015-08-16 16:49 255人阅读
    C++ Primer Plus 第6版 中文版随书笔记 标签: c++ 2015-07-22 18:58 205人阅读 评论(0)
    3D 数据的获取和读入 标签: matlab3D 图像 2015-04-19 11:52 387人阅读 评论(0)
    Python学习笔记九:文件I/O
    Python学习笔记八:模块
  • 原文地址:https://www.cnblogs.com/wang3680/p/53bb058cc4860cb3d39c23b3d18079e8.html
Copyright © 2011-2022 走看看