文件上传及下载

一、要实现文件上传,需在项目中添加两个jar文件

Jsp页面

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

Action类

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];
        
        //读取文件
        FileInputStream fis=new FileInputStream(getUpload());
        //保存文件
        FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName());
        int length=fis.read(buffer);
        while(length>0){
            //每次写入length长度的内容
            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);
     }

 

在Action中使用了三个属性封装文件信息:

File类型的XXX属性,与表单的File控件的name属性一样,用于封装File控件对应的文件内容

String类型的xxxFileName属性,该属性名称由前面的File类型属性和FileName组合,是固定的语法,是封装File控件对应文件的文件名

String类型的XXXContentType属性,同样由xxx属性和ContentType组合而成,是固定语法,封装File控件对应文件的文件类型

配置Action:

<?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="true"></constant>
    <package name="default" namespace="/" extends="struts-default">
    <!-- 文件上传 -->
        <action name="upload" class="cn.happy.action.UploadAction">
        <!-- 通过param参数设置保存目录的路径 -->
        <param name="savePath">/upload</param>
           <result name="success">ok.jsp</result> 
        </action>
        
    </package>
</struts>

上传成功后的结果页面:植入的value是Action类中所对应的实体类属性

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

页面运行效果:

多个文件上传

只需在上传Action中将原本处理单个文件的操作改成对集合操作即可。

其他的都跟单个上传的一样

页面

<!-- 多个文件上传 -->
        <action name="someupload" class="cn.action.SomeUploadAction">
            <!-- 通过param参数设置保存目录的路径 -->
            <param name="savePath">/upload</param>
            <result name="success">success.jsp</result>
        </action>

开发实现文件上传的Action

package cn.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class SomeUploadAction extends ActionSupport {
    //封装上传文件属性
        private File[] upload;
        
        //封装上传文件的类型
        private String[] uploadContentType;
        
        //封装上传文件名称
        private String[] uploadFileName;
        
        //封装文件上传的路径
        private String savePath;
        

        
        public String execute() throws Exception{
            byte[] buffer=new byte[1024];
            for (int i = 0; i < upload.length; i++) {
                FileInputStream fis=new FileInputStream(getUpload()[i]);
                FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName()[i]);
                int length=fis.read(buffer);
                while(length>0){
                    fos.write(buffer, 0, length);
                    length=fis.read(buffer);
                }
                fos.flush();
                fos.close();
                fis.close();
            }
            return SUCCESS;
        }



        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;
        }



        
}

配置Action

<!-- 多个文件上传 -->
        <action name="someupload" class="cn.action.SomeUploadAction">
            <!-- 通过param参数设置保存目录的路径 -->
            <param name="savePath">/upload</param>
            <result name="success">success.jsp</result>
        </action>

 

 

文件下载

文件下载需要InputStream,首先在文件下载Action中提供一个获得InputStream的方法,通过输入流可以获取希望下载的文件内容

package cn.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownAction extends ActionSupport {
    //读取下载文件的目录
        private String inputPath;
        
        //下载文件的文件名
        private String fileName;
        
        //读取下载文件的输入流
        private InputStream inputStream;
        
        //下载文件的类型
        private String conetntType;
        
        
        
        
        
        public String execute(){
            return SUCCESS;
            
        }

        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;
        }
        //创建InputStream输入流
        public InputStream getInputStream() throws Exception {
            String path=ServletActionContext.getServletContext().getRealPath(inputPath);
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
            return stream;
        }

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

        public String getConetntType() {
            return conetntType;
        }

        public void setConetntType(String conetntType) {
            this.conetntType = conetntType;
        }
        
        
}

通过Context得到下载文件的实际路径,构建一个InputStream输入流实现文件的下载读取。

在配置文件中,同样对Action进行配置,并对stream结果类型的参数进行设置。

 

<!-- download指定的Action -->
        <action name="download" class="cn.action.FileDownAction">
            <param name="inputPath">/upload</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>

 

 

 

 
posted @ 2016-10-10 17:51  Onlyだ  阅读(278)  评论(0编辑  收藏  举报