1、文件上传
必要前提:
a.表单method必须是post;
b.enctype取值必须multipart/form-data;
c.提供文件选择域。
上传的jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:actionerror/>
<s:form action="file/upload" method="post"
enctype="multipart/form-data" namespace="/file">
<input type="file" multiple="multiple" name="image">
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
作用类
package struts.web.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileAction extends ActionSupport{
//上传字段
private File[] image;
//上传的文件名,该属性是固定写法:上传字段名称+FileName(区分大小写)
private String[] imageFileName;
//上传文件的MIME类型,写法固定::上传字段名称+ContentType(区分大小写)
private String[] imageContentType;
//下载时候用的,先不管
private String fileName;
private FileInputStream fileInputStream;
public String upload() {
//指定存放的目录
String root=ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(root);
File rootPath=new File(root);
if (!rootPath.exists()) {
rootPath.mkdirs();
}
//同时存放多个文件,需要遍历
for (int i = 0; i < image.length; i++) {
System.out.println("imageFileName :"+imageFileName+"imageContentType :"+imageContentType);
//把临时文件重命名后,存放到指定目录,临时文件没了
image[i].renameTo(new File(root,imageFileName[i]));
}
return SUCCESS;
}
public String download() throws FileNotFoundException {
String root=ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(root);
File rootPath=new File(root);
File downloadFile =new File(root,fileName);
fileInputStream =new FileInputStream(downloadFile);
return "downSuccess";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContenType() {
return imageContentType;
}
public void setImageContenType(String[] imageContenType) {
this.imageContentType = imageContenType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public FileInputStream getFileInputStream() {
return fileInputStream;
}
public void setFileInputStream(FileInputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
}
struts-file.xml配置
<?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>
<!-- extends="global-package"没有我之前配置的话要把global-package改成struts-default -->
<package name="struts-file" extends="global-package" >
<action name="*" class="struts.web.action.FileAction" method="{1}">
<!-- 防止之前做的全局拦截器拦截 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input">/file.jsp</result>
<!-- 看着应该是用来防止被拦截
<interceptor-ref name="global-interceptor-stack">
<param name="checkLogin.excludeMethods">upload,download</param>
</interceptor-ref> -->
</action>
</package>
</struts>
2、文件上传的配置
2.1、文件上传大小限制(默认是2MB)
如果上传文件超过了默认大小,upload拦截器会转向一个input的逻辑视图。
可以在struts.xml中改写default.properties文件中的常量。
加写一行下面代码
struts.ognl.allowStaticMethodAccess=true
2.2、限制文件上传的类型
a、通过限制上传文件的扩展名
思路:给fileUpload拦截器注入参数
当上传非限定扩展名时:有如下错误提示
b、通过限制上传文件的MIME类型
当上传非限定MIME类型时:有如下错误提示
3、文件下载:其实就是一种结果类型(Stream)
动作类:在上面的FileAction里头的public string download()方法
配置文件(struts-file.xml):
在 action标签 里头加上
<result name="downSuccess" type="stream">
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<param name="contentType">application/octet-stream</param>
</result>
注意:文件名不能在配置文件中写死,需要根据实际情况获取。
over~~~