其他分享
首页 > 其他分享> > Struts2 文件上传和下载 -- 青鸟

Struts2 文件上传和下载 -- 青鸟

作者:互联网

前言

学习一下Struts2框架中文件上传和下载。

文件上传

Struts2 提供了对commons-fileupload 组件的支持,上传文件需要导入两个Jar包或者(Maven的依赖)

单文件上传

1. 准备一个上传页面 (Jsp)
<s:form action="upload" enctype="multipart/form-data" method="POST">
    选择文件:
    <s:file name="upload" label="选择文件"/> <br>
    <s:submit name="sub" value="文件上传"/> <br>
</s:form>
2. 单文件上传的Action
package com.bdqn.Control;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadAction extends ActionSupport {
    //封装上传服务器的文件对象
    private File upload;
    //上传文件的类型
    private String uploadContentType;
    //上传文件的名称
    private String uploadFileName;
    //获取上传文件的保存路径
    private String 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;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    @Override
    public String execute() throws Exception {
        byte[] bytes = new byte[1024];
        //读取文件
        FileInputStream fileInputStream = new FileInputStream(getUpload());
        //保存文件,并且设置保存的路径
        System.out.println(getSavePath()+"-------------->"+getUploadFileName());
        FileOutputStream fileOutputStream = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
        int length=fileInputStream.read(bytes);
        while (length>0){
            //每次写入length的内容
            fileOutputStream.write(bytes,0,length);
            length=fileInputStream.read(bytes);
        }
        fileInputStream.close();
        fileOutputStream.flush();
        fileOutputStream.close();
        return SUCCESS;
    }
}

3. 配置struts.xml文件
	<!--文件上传-->
	<action name="upload" class="com.bdqn.Control.UploadAction">
		<!--设置保存目录的路径-->
		<param name="savePath">/uploads</param>
		<result name="success">ZhanUpload.jsp</result>
	</action>

注意 :Action中 ServletActionContext.getServletContext().getRealPath(savePath); 报红,就要导入Servlet的Api依赖(jar包)

 	<dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
	</dependency>

多文件上传

实现多文件上传的操作非常简单,在表单中添加多个相同name属性的File控件,这样当表单提交时,将会提交一个数组。

<s:form action="upload" enctype="multipart/form-data" method="POST">
    选择文件:
    <s:file name="upload" label="选择文件"/> <br>
    <s:file name="upload" label="选择文件"/> <br>
    <s:submit name="sub" value="文件上传"/> <br>
</s:form>
package com.bdqn.Control;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

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

public class DuoUploadAction extends ActionSupport {
    //封装上传服务器的文件对象
    private File[] upload;
    //上传文件的类型
    private String[] uploadContentType;
    //上传文件的名称
    private String[] uploadFileName;
    //获取上传文件的保存路径
    private String 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;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    @Override
    public String execute() throws Exception {
        byte[] bytes = new byte[1024];
        for (int i = 0; i <upload.length ; i++) {
            //读取文件
            FileInputStream fileInputStream = new FileInputStream(getUpload()[i]);
            //保存文件,并且设置保存的路径
            FileOutputStream fileOutputStream = new FileOutputStream(getSavePath()+"\\"+getUploadFileName()[i]);
            int length=fileInputStream.read(bytes);
            while (length>0){
                //每次写入length的内容
                fileOutputStream.write(bytes,0,length);
                length=fileInputStream.read(bytes);
            }
            fileInputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        }
            return SUCCESS;
    }
}

多文件上传,还可以采用多个File控件,不同的name属性,不过这样的方式每增加一个File控件,都必须相应的增加属性的设置,不建议使用。

文件下载

为支持文件的下载,Struts2 框架提供stream结果类型,该类型的作用就是实现文件下载功能。

stream 结果类型

stream 配置参数

名称 作用
contentType 设置发送到浏览器的MIME类型
contentLength 设置文件的大小
contentDisposition 设置响应的HTTP头信息中的Content-Disposition参数的值
inputName 指定Action中提供的inputStream类型的属性名称
bufferSize 设置读取和下载文件时的缓冲区大小
	<a href="download?fileName=jietu.png">下载</a>
	<!--下载文件-->
	<action name="download" class="com.bdqn.Control.DownFileAction">
		<param name="inputPath">/uploads</param>
		<result name="success" type="stream">
			<param name="contentType">application/octet-stream</param>
			<param name="inputName">inputStream</param>
			<param name="contentDisposition">attachment;fileName="${fileName}"</param>
			<param name="bufferSize">4096</param>
		</result>
	</action>
package com.bdqn.Control;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class DownFileAction extends ActionSupport {
    //读取下载文件的目录
    private String inputPath;
    //下载文件的文件名
    private String fileName;
    //下载文件的输入流
    private InputStream inputStream;
    //下载文件中的类型
    private String contenttype;

    public String getInputPath() {
        return inputPath;
    }

    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getInputStream() throws FileNotFoundException {
        String path= ServletActionContext.getServletContext().getRealPath(inputPath);
        return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getContenttype() {
        return contenttype;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }

    @Override
    public String execute() throws Exception {

        return SUCCESS;
    }
}

标签:文件,return,String,青鸟,Struts2,import,上传,public
来源: https://blog.csdn.net/chenHaiJaheike/article/details/99747825