Servlet实现文件上传(深度)(二)

2022-12-01,,

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文件的配置了,你自己添加把

来自为知笔记(Wiz)

Servlet实现文件上传(深度)(二)的相关教程结束。

《Servlet实现文件上传(深度)(二).doc》

下载本文的Word格式文档,以方便收藏与打印。