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>




  • 相关阅读:
    kubeadm安装kubernetes集群
    推荐几个大厂的前端代码规范,你也能写出诗一样的代码!
    恕我直言,你可能连 GitHub 搜索都不会用
    全球最火的WEB开发学习路线!没有之一!3 天就在GitHub收获了接近 1w 点赞
    VS2013扩展和更新JS智能提示
    linux下升级git版本的操作记录(摘录)
    python 多版本 安装模块 ModuleNotFoundError: No module named 'xxx'
    记录一次app报病毒的问题
    python 根据大图片生成各种规格图片 生成ios需要的各个规格的icon
    React Native iOS 项目初始化
  • 原文地址:https://www.cnblogs.com/wang3680/p/53bb058cc4860cb3d39c23b3d18079e8.html
Copyright © 2011-2022 走看看