文件上传--多文件上传

Posted 残殇--

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传--多文件上传相关的知识,希望对你有一定的参考价值。

首先先创建jsp页面(用于多文件上传)

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>登录页面</title>

</head>

<body>

    <s:form action="upload.action" enctype="multipart/form-data" method="post">
        
        <s:file name="upload" label="选择文件" />
        <br />
        <s:file name="upload" label="选择文件" />
        <br />
        <s:file name="upload" label="选择文件" />
        <br />
        <s:submit name="submit" value="上传文件"></s:submit>
    </s:form>

</body>
</html>
复制代码

success.jsp页面(用来显示上传成功后的文件和文件类型)

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>成功页面</title>

</head>

<body>
    您所上传的文件是:<s:property value="uploadFileName"/><br/>
    文件类型:<s:property value="uploadCOntentType"/>


</body>
</html>
复制代码

接下来创建UploadAction类

复制代码
package cn.happy.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {


    // 上传文件的属性
    private File[] upload;
    // 上传文件的类型
    private String[] uploadContentType;
    // 上传文件的名称
    private String[] uploadFileName;
    // 上传文件的地址
    private String savePath;
    
    @Override
    public String execute() throws Exception {
        byte[] buffer=new byte[1024];
        for (int i = 0; i < upload.length; i++) {
            
            //建立上传文件的输入流
            FileInputStream fis=new FileInputStream(getUpload()[i]);
            //建立上传文件的输出流, getImageFileName()[i]
            FileOutputStream fos=new FileOutputStream(getSavePath()+"\\\\"+getUploadFileName()[i]);
            int length=fis.read(buffer);
            while(length>0){
                fos.write(buffer,0,length);
                length = fis.read(buffer);
                
            }
            fis.close();
            fos.flush();
            fos.close();
        }
        return SUCCESS;
    }
    
    
    
    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    
    public File[] getUpload() {
        return upload;
    }
    public void setUpload(File[] upload) {
        this.upload = upload;
    }
    public String[] getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String[] uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String[] getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String[] uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    
    
    
    
    

}
复制代码

最后编写配置文件struts.xml

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- 动态方法调用 -->
 
    <constant name="struts.devMode" value="false" />
    
    
    <package name="default" namespace="/" extends="struts-default">
        
        <action name="upload" class="cn.happy.action.UploadAction" method="execute">
            <param name="savePath">/image</param>
           <result name="success">/upload/success.jsp</result>
        </action>
    </package>
</struts>
复制代码

实现效果展示

选择文件后

以上是关于文件上传--多文件上传的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

文件上传(多文件上传)/下载

SpringBoot单文件与多文件上传

将存储在内存中的文件上传到s3

多文件上传:从前台到后台代码实现

JS创建文件并上传服务器