1、首先我们定义struts.properties的文件上传中的规则如下
struts.action.extension=action  <!--以.action为我们提交的后缀名-->
struts.multipart.saveDir=c:/shengsiyuan  <!--保存我们上传文件的临时目录,此文件夹下面会生成临时文件,以.tmp为后缀名-->
struts.multipart.maxSize=1048576000  <!--规定文件的大小-->
当然以上文件的大小界定我们还可以这样在struts.xml文件中定义如下
<struts>
    <constant name="struts.multipart.maxSize" value="1048576000"></constant>
</struts>

2、页面fileUpload.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="fileUpload.action" method="post" enctype="multipart/form-data">
 
    username: <input type="text" name="username"><br>
    file: <input type="file" name="file"><br>
 
    <input type="submit" value="submit">
 
    </form>
 
  </body>
</html>
3、UploadAction.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 org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class UploadAction extends ActionSupport
{
    private String username;  //传入的username
 
    private File file;    //以File为类型的file
 
    private String fileFileName;  //小心命名,以file开头FileName固定
 
    private String fileContentType;
 
    public String getUsername()
    {
        return username;
    }
 
    public void setUsername(String username)
    {
        this.username = username;
    }
 
    public File getFile()
    {
        return file;
    }
 
    public void setFile(File file)
    {
        this.file = file;
    }
 
    public String getFileFileName()
    {
        return fileFileName;
    }
 
    public void setFileFileName(String fileFileName)
    {
        this.fileFileName = fileFileName;
    }
 
    public String getFileContentType()
    {
        return fileContentType;
    }
 
    public void setFileContentType(String fileContentType)
    {
        this.fileContentType = fileContentType;
    }
 
    @Override
    public String execute() throws Exception
    {
        String root = ServletActionContext.getRequest().getRealPath("/upload");//定义上传文件的路径
 
        InputStream is = new FileInputStream(file);
 
        //这里查看上传文件临时路径、以及临时文件的名字,在struts.properties已规定
        System.out.println("path: " + file.getAbsolutePath()); 
        System.out.println("file: " + file.getName());
 
        //这里查看文件的真实名称
        System.out.println("fileFileName: " + fileFileName);
 
        File destFile = new File(root, fileFileName);
 
        OutputStream os = new FileOutputStream(destFile);
 
        byte[] buffer = new byte[400];//定义以400字节的速度上传文件
 
        int length = 0;
 
        while(-1 != (length = is.read(buffer)))
        {
            os.write(buffer, 0, length);
 
           // Thread.sleep(1000);//为了能够查看临时文件存在,定义线程延时1秒
        }
 
        is.close();
        os.close();
 
        return SUCCESS;
 
    }
}
4、结果展示页fileUploadResult.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 'fileUploadResult.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>
 
    name:<s:property value="fileFileName"/>
    <br>
    type:<s:property value="fileContentType"/>
  </body>
</html>

5、忘记struts.xml文件的配置了,你自己添加把

Servlet实现文件上传(深度)(二)的更多相关文章

  1. Servlet实现文件上传

    一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip      :   点击打开链接 2) commons- ...

  2. Servlet实现文件上传,可多文件上传

    一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...

  3. 配置servlet支持文件上传

    Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...

  4. jsp+servlet实现文件上传下载

    相关素材下载 01.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  5. java commons-fileupload servlet 多文件上传

    commons-fileupload servlet 多文件上传 需要引入的 jar 包. commons-fileupload-1.3.2.jar commons-io-2.2.jar 工程路劲:查 ...

  6. servlet web文件上传

    web文件上传也是一种POST方式,特别之处在于,需设置FORM的enctype属性为multipart/form-data. 并且需要使用文件域. servlet的代码比较关键是这几句: // 使用 ...

  7. servlet实现文件上传,预览,下载和删除

      一.准备工作 1.1 文件上传插件:uploadify: 1.2 文件上传所需jar包:commons-fileupload-1.3.1.jar和commons-io-2.2.jar 1.3 将数 ...

  8. Servlet实现文件上传和下载

    对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具commo ...

  9. jsp+servlet实现文件上传

    上传(上传不能使用BaseServlet) 1. 上传对表单限制 * method="post" * enctype="multipart/form-data" ...

随机推荐

  1. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Food on the Plane

    Food on the Plane time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. spring jdbc 源码

    类:org.springframework.jdbc.core.JdbcTemplate public <T> T execute(PreparedStatementCreator psc ...

  4. UIImageView 的contentMode属性 浅析

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...

  5. dhcpv6开源软件配置

    ISC-dhcp server for IPv6 1.  下载源码:http://www.isc.org/software/dhcp 2.安装:最好以root身份安装,否则会permission de ...

  6. js框架——angular.js(6)

    1. ng-class 这个指令是用来绑定一个或者多个css代码.它的值一般是一个表达式,也可以是函数什么的,只要返回的确实是一个类的名字就可以—— ng-class="nextPageDi ...

  7. pat L1-006. 连续因子

    L1-006. 连续因子 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个正整数N的因子中可能存在若干连续的数字.例如630 ...

  8. HDU ACM 1495 非常可乐(广搜BFS)

    非常可乐 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  9. UVALive 7291 Kinfolk(最近公共祖先)

    题目中的描述就很最近公共祖先,再说其实这个题并不难,就是麻烦点(代码其实可以化简的),我写的判定比较多. 方法:求出两者的最近公共祖先lca,在求出两者到lca的距离 分析:给出a和b,如果LCA(a ...

  10. 9个Console控制台命令(转载)

    一.显示信息的命令 <!DOCTYPE html> <html> <head> <title>常用console命令</title> < ...