struts 的拦截器和文件下载

拦截器:
Interceptor
    implements Interceptor    (com.opensymphony.xwork2.interceptor.Interceptor)
      extends AbstractInterceptor  (com.opensymphony.xwork2.interceptor.AbstractInterceptor)
    与filter的区别:先过filter再过interceptor

文件上传拦截器
org.apache.struts2.interceptor.FileUploadInterceptor    
文件上传:
    三种上传方案
    虚拟路径与真实路径    /upload
    copyFile与copydirectory

    
    0. 文件下载
   另存为
   直接打开

1. 内容类型
   response.setContentType(d.getMime());

2. 设置响应头
   response.setHeader("Content-Disposition","attachment;filename=" + fileName);//文件名

3. 处理文件名的中文乱码
   String fileName = d.getFileName();
   fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
            

4. struts2文件上传大小设置
   <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) 10M=10*1024*1024 -->
   <constant name="struts.multipart.maxSize" value="10485760"/>

5. struts2文件上传类型设置
   根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制
   <interceptor-ref name="fileUpload">
     <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
   </interceptor-ref>


6. 其它
   enctype="multipart/form-data" method="post"
   private File file;
private String fileContentType;
private String fileFileName;
   

package com.zking.five.interceptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.coyote.http11.filters.BufferedInputFilter;

import com.zking.four.web.BaseAction;

/**
 * 文件上传的三种方案:
 *     1.将上传的文件以二进制的形式存放到数据库        oa系统        activiti工作流程
 * @author Machenike
 * 
 *     2.将文件上传到文件服务器(硬盘足够大)中
 * 
 *     3.将文件发送到tomcat所在的普通web服务器
 * 
 * 
 * 真实路径与虚拟路径的概念
 *         1,所谓真实路径指的是在自己电脑上能够找到的路径
 *         2,所谓虚拟,在自己的电脑上看不到,在别人电脑(tomcat所在位置服务器)上能够看到
 *
 *
 */

public class UploadAction extends BaseAction{
    private File file;//变量名指的是jsp中文件选择器的name属性, 就是你要上传的文件         xxx
    private String fileContentType;//xxxContentType;    文件类型
    private String fileFileName;//xxxFileName        文件名称
    
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    private String serverDir = "/upload";
    

    /**
     * 下载文件的方法
     * @return
     */
    public String upload() {
        String realPath = serverDir + "/" +fileFileName;
        /*
         * 参数1:本地要下载的图片文件
         * 
         * 参数2: 在服务器生成的文件
         */
        System.out.println(fileFileName);
        System.out.println(fileContentType);
//        System.out.println(application.getRealPath(realPath));
//        System.out.println(getRealPath(realPath));
        try {
            FileUtils.copyFile(file, new File(getRealPath(realPath)));
//            File file2 = new File(getRealPath(realPath));
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SUCCESS;
    }
    
    
    /**
     * 设置缓冲流的查看图片  优化
     * @param bos
     * @param bis
     */
    public void CopyFile(BufferedOutputStream bos,BufferedInputStream bis) {
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while((len=bis.read(b))!=-1) {
                bos.write(b,0,len);
            }
            bis.close();
            bos.close();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 获取Linux下的上传文件的所在位置
     * @param path
     * @return
     */
    private String getRealPath(String path) {
        // TODO Auto-generated method stub
        System.out.println("path:"+path);
        return application.getRealPath(path);
    }

    /**
     * 在本地查看tomcat服务器上的图片
     * @return
     */
    public String openAs() {
        String name = "1.jpg";
        String type = "image/jpg";
        String realPath = serverDir + "/" +name;
        response.setContentType(type);
        response.setHeader("Content-Disposition","filename=" + name);//文件名
        
        try {
//            FileUtils.copyFile(new File(getRealPath(realPath)), response.getOutputStream());
            FileInputStream fileInputStream = new FileInputStream(new File(getRealPath(realPath)));
            CopyFile(new BufferedOutputStream(response.getOutputStream()),new BufferedInputStream(fileInputStream));
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 下载文件
     * @return
     */
    public String download() {
        String name = "1.jpg";
        String type = "image/jpg";
        String realPath = serverDir + "/" +name;
        response.setContentType(type);
        response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
        
        try {
            FileUtils.copyFile(new File(getRealPath(realPath)), response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    

}
 

   

猜你喜欢

转载自blog.csdn.net/qq_41915592/article/details/83099006
今日推荐