• 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>




  • 相关阅读:
    MSB4064 错误
    javascript 通过模块模式实现代码访问控制
    vs 2012 更新update4 后出现问题
    html 转 PDF wkhtmltopdf image 不能显示的问题
    Html方式导出word 页头和页脚设置
    使用JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength属性
    经验1-打印web
    DataGridView 多线程更新 数据 解决卡顿问题
    Copy List with Random Pointer [LeetCode]
    Validate Binary Search Tree [LeetCode]
  • 原文地址:https://www.cnblogs.com/wang3680/p/53bb058cc4860cb3d39c23b3d18079e8.html
Copyright © 2020-2023  润新知