拦截器与文件上传

Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor

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

文件下载

另存为
直接打开

package com.zking.five;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.zking.four.BaseAction;
/**
 * 文件上传的三种方式:
 * 1.上传的文件以二进制的形式保存到数据库(activiti,jbpmg工作流框架)oi系统
 * 2.将文件上传到指定服务器的硬盘(cpu快,硬盘大);
 * 3.将文件上传到tomcat所在服务器(对应的静态资源服务器)
 * @author Administrator
 *
 *		真实路径和虚拟路径
 *相对于自己的电脑而言的
 */
public class FileAction extends BaseAction {
	// 这是jsp传递过来的具体文件
	private File file;
	private String fileFileName;
	private String fileContentType;
//	虚拟路径
	private String serverDir ="upload";
/**
 *   上传图片到服务器
 * @return
 */
	public String upload() {
		System.out.println("fileFileName"+fileFileName);
		System.out.println("fileContentType"+fileContentType);
		String realPath=getRealPath(serverDir+"/"+fileFileName);
		System.out.println("realPath:"+realPath);
		try {
			FileUtils.copyFile(file, new File(realPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "seccess";
	} 
	/**
	 * 文件夹的真实路径
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return this.request.getServletContext().getRealPath(path);
	}
/**
 * 下载图片
 * @return
 */
	public String saveAs() {
		String fileName="QQ图片20181114120413.png";
		String fileType="image/jpeg";
		response.setContentType(fileType);
		response.setHeader("Content-Disposition","attachment;filename=" + fileName);
		String realPath=getRealPath(serverDir+"/"+fileName);
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
/**
 * 直接在页面打开图片
 * @return
 */
	public String poenAs() {
		String fileName="QQ图片20181114120413.png";
		String fileType="image/jpeg";
		response.setContentType(fileType);
		response.setHeader("Content-Disposition","filename=" + fileName);
		String realPath=getRealPath(serverDir+"/"+fileName);
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

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

}

  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文件上传大小设置

  5. struts2文件上传类型设置
    根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制

    image/png,image/gif,image/jpeg
  6. 其它
    enctype=“multipart/form-data” method=“post”
    private File file;
    private String fileContentType;
    private String fileFileName;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值