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>

Servlet实现文件上传(多文件)(三)的更多相关文章

  1. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  2. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  3. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

  4. webAPI文件上传时文件过大404错误的问题

    背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...

  5. Java中实现文件上传下载的三种解决方案

    第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...

  6. 与文件上传到的三个类:FileItem类、ServletFileUpload 类、DiskFileItemFactory类

    文件上传: ServletFileUpload负责处理上传的文件数据,并将表单中每个输入项封装成一个FileItem对象中, 在使用ServletFileUpload对象解析请求时需要根据DiskFi ...

  7. Http服务器实现文件上传与下载(三)

    一.引言 在前2章的内容基本上已经讲解了整个的大致流程.在设计Http服务器时,我设计为四层的结构,最底层是网络传输层,就是socket编程.接着一层是请求和响应层,叫做Request和Respons ...

  8. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  9. spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)

    一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...

  10. struts2实现文件上传(多文件上传)及下载

    一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...

随机推荐

  1. Spring MVC中前后台数据传输小结

    前台向后台传递参数: @ResponseBody @RequestMapping(value = "/findById/{id}", method = { RequestMetho ...

  2. FZU 2113 BCD Code 数位dp

    数位dp,但是很奇怪的是我在虚拟oj上用GUC C++提交会wa,用Visual c++提交正确,但是加上注释后提交又莫名CE--好任性啊 0 ,0 题目思路:看代码吧 注释很详细 #include& ...

  3. js 关键字 in

    对于数组 ,迭代出来的是数组元 素,对于对象 ,迭代出来的是对象的属性: var x var mycars = new Array() mycars[0] = "Saab" myc ...

  4. Android Camera(一)

    最近老大交给了一个任务,说是要在本地视频端很够调节摄像头焦距. 碰到了一些问题: 1.手机支不支持摄像头变焦 2.系统自带摄像软件可以变焦,但是自己编写的程序不支持变焦, 这个问题网上也有很多童鞋碰到 ...

  5. android touchEvent事件学习

    学习网址:http://www.apkbus.com/forum.php?mod=viewthread&tid=44296 1:Android Touch事件传递机制解析 android系统中 ...

  6. Vim编辑器的使用和基本配置

    三种模式 1 命令模式 插入 a i o A I O 定位 gg G :n nG ngg $ 0 删除 x nx dd ndd dG 复制和剪切 yy-p dd-p 替换 r R 撤销和恢复 u Ct ...

  7. 使用print2flash开发在线文档

    www.print2flash.com 命令行调用: A:\Program Files (x86)\Print2Flash3>p2fServer.exe a.pdf a.swf

  8. listview的简单封装

    package com.itheima.googleplay.ui.view; import android.content.Context; import android.graphics.Colo ...

  9. android adb shell 命令大全

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...

  10. inline-block的升级float:浮动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...