一、拦截器【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>