struts2(三):拦截器与文件上传

本文详细介绍了Struts2框架中拦截器的实现,从继承Interceptor接口开始,探讨了文件上传的三种策略,包括存入数据库、文件服务器和Web服务器。接着,文章讲解了如何配置Struts2文件上传的最大大小和允许类型,以及解决文件名中文乱码问题。最后,通过具体的实例展示了文件上传、展示和下载的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、拦截器【interceptor】

1、继承Interceptor【implements Interceptor】

2、 文件上传的三种方案:
    1、将上传的文件以二进制的形式存放到数据库     oa系统    actibeti工作流框架
    2、将文件上传到文件服务器(硬盘足够大)中
    3、将文件上传到tomcat所在的普通Web服务器

 

说明:

真实路径与虚拟路径的概念:
 *	1、所谓真实路径指的是在自己电脑上能够找得到的路径
 *	2、所谓虚拟,在自己电脑上是看不到的,路径在别人电脑上(tomcat所在位置)能看到

二、文件上传

1、struts2文件上传大小设置
   <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) 10M=10*1024*1024 -->
 

 <constant name="struts.multipart.maxSize" value="10485760"/>
private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
		byte[] bbuf = new byte[1024];
		int len = 0;
		while((len = in.read(bbuf))!=1) {
			out.write(bbuf,0,len);
		}
		in.close();
		out.close();
	}

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

 <interceptor-ref name="fileUpload">
     <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
   </interceptor-ref>

3、处理文件名的中文乱码

 String fileName = d.getFileName();
   fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");

三、实例【实现文件上传,展示以及下载】

struts-sy.xml

<action name="uploadAction_*" class="com.zking.five.web.UploadAction" method="{1}">
				<result name = "success">/success.jsp</result>
		</action>

Action

package com.zking.five.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;

import com.zking.four.web.BaseAction;
/**
 * 文件上传的三种方案:
 * 	1、将上传的文件以二进制的形式存放到数据库     oa系统    actibeti工作流框架
 * 	2、将文件上传到文件服务器(硬盘足够大)中
 * 	3、将文件上传到tomcat所在的普通Web服务器
 * 
 * 
 * 
 *真实路径与虚拟路径的概念:
 *	1、所谓真实路径指的是在自己电脑上能够找得到的路径
 *	2、所谓虚拟,在自己电脑上是看不到的,路径在别人电脑上(tomcat所在位置)能看到
 * @author Administrator
 *
 */
public class UploadAction extends BaseAction{
	private File file;//变量名指的是jsp的name属性,就是你要上传的文件 xxx
	private String fileContentType;//xxxContentType
	private String fileFileName;//xxxFileName
	
	//创建一个虚拟的路径
	private String serverDir = "/upload";
	
	public String upload() throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
/*		System.out.println(fileContentType);
		System.out.println(fileFileName);
*/		/**
		 * 参数1:本地图片文件
		 * 参数2:在服务器生的文件
		 */
		System.out.println(fileContentType);
		System.out.println(fileFileName);
		String realPath = getRealPath(serverDir + "/" + fileFileName);
		System.out.println(realPath);
		try {
			FileUtils.copyFile(file, new File(realPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 获取Linux下的上传文件的具体所在位置
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return application.getRealPath(path);
	}
	public String openAs() {
		String type = "image/jpeg";
		String name = "9.jpg";
		response.setContentType(type);
		response.setHeader("Content-Disposition","filename=" + name);//文件名
		/** 
		 * 将远程的图片输出到本地
		 * 数据源inputstream :远程  new File(realPath)
		 * 目的:输出到本地的jsp:response.getOutputStream()
		 * 
		 */
		  String realPath = getRealPath(serverDir + "/" + name);
		try {
			//别人提供的上传图片时间流
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
			/**
			 * 自定义上传图片时间流【速度较快】
			 */
//			BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(realPath)));
//			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
//			copyStream(in, out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
		byte[] bbuf = new byte[1024];
		int len = 0;
		while((len = in.read(bbuf))!=1) {
			out.write(bbuf,0,len);
		}
		in.close();
		out.close();
	}
	
	
	
	public String download() {
		String type = "image/jpeg";
		String name = "1.jpg";
		response.setContentType(type);
	    response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
		/** 
		 * 将远程的图片输出到本地
		 * 数据源inputstream :远程  new File(realPath)
		 * 目的:输出到本地的jsp:response.getOutputStream()
		 * 
		 */
		  String realPath = getRealPath(serverDir + "/" + name);
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
			/*BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(realPath)));
			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
			copyStream(in, out);*/
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public File getFile() {
		return file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
}

jsp

<h1>struts2文件的上传下载</h1>
<form action = "${pageContext.request.contextPath  }/sy/uploadAction_upload.action" enctype="multipart/form-data"  method="post">
	<input type = "file" name = "file">
	<input type = "submit" value = "上传">
 </form>
 <%@page import="com.opensymphony.xwork2.util.ValueStack"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags"  prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>跳转成功! </h1>
<br>

<h2>打开图片</h2>
<s:url var="openAsurl" namespace="/sy" action = "uploadAction_openAs.action"></s:url>
<%-- <s:property value = "openAsurl"/> --%>
<img alt="" src='<s:property value = "openAsurl"/>'>


<h2>下载图片</h2>
<s:url var="downloadUrl" namespace="/sy" action = "uploadAction_download.action"></s:url>
<s:a href = "%{#downloadUrl}">下载</s:a>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值