Struts2(二)文件上传和下载
1.创建一个index.jsp界面 和success.jsp上传成功界面
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
<s:form action="files/add" method="post" enctype="multipart/form-data">
<s:file label="图片上传" name="upload"/>
<s:submit value="提交"/>
</s:form>
success.jsp
<body>
文件操作成功!!
</body>
2.创建对应的struts.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>
<!--设置文件上传的大小 -->
<constant name="struts.multipart.maxSize" value="5000000000"></constant>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/files" extends="struts-default">
<action name="add" class="cn.bdqn.action.UploadAction" method="upload">
<!-- 限制文件只能是图片
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref> -->
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
3.创建对应的UploadAction
属性名的规范 底层代码 FileUploadInterceptor中
File[] files = multiWrapper.getFiles(inputName);
if (files != null && files.length > 0) {
List<File> acceptedFiles = new ArrayList<File>(files.length);
List<String> acceptedContentTypes = new ArrayList<String>(files.length);
List<String> acceptedFileNames = new ArrayList<String>(files.length);
String contentTypeName = inputName + "ContentType";
String fileNameName = inputName + "FileName";
public class UploadAction extends ActionSupport {
private File upload; //上传的文件 和前台file标签中的name属性 一致
private String uploadFileName; //上传的文件名称 底层有规定,必须按照规范来
private String uploadContentType; //上传的文件的类型
//文件上传的方法
public String upload(){
System.out.println("进入了文件的上传......");
//指定文件上传的路径
String path=ServletActionContext.getServletContext().getRealPath("/upload");
//File.separatorChar 系统分隔符 区别linux和windows系统
String pathName=path+File.separatorChar+uploadFileName;
System.out.println("上传文件的全名称:"+pathName);
try {
FileUtils.copyFile(upload, new File(pathName));
} catch (IOException e) {
e.printStackTrace();
return INPUT;
}
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
4.多文件上传 创建index2.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <body> <s:form action="files/adds" method="post" enctype="multipart/form-data"> <s:file label="图片上传" name="upload"/> <s:file label="图片上传" name="upload"/> <s:file label="图片上传" name="upload"/> <s:submit value="提交"/> </s:form> </body>
5.在struts.xml文件中 新增
<!-- 多个文件上传 -->
<action name="adds" class="cn.bdqn.action.UploadAction2" method="upload">
<result>/success.jsp</result>
<result name="input">/index2.jsp</result>
</action>
6.创建对应的UploadAction2
public class UploadAction2 extends ActionSupport {
private File [] upload; //上传的文件
private String [] uploadFileName; //上传的文件名称
private String [] uploadContentType; //上传的文件的类型
//文件上传的方法
public String upload(){
System.out.println("进入了多文件的上传......");
String path= ServletActionContext.getServletContext().getRealPath("/upload");
String pathName="";
//遍历 用户选择的文件
for (int i = 0; i < upload.length; i++) {
pathName= path+File.separatorChar+uploadFileName[i];
try {
//把用户选择的文件复制到 指定的文件下
FileUtils.copyFile(upload[i], new File(pathName));
} catch (IOException e) {
e.printStackTrace();
return INPUT;
}
}
return SUCCESS;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
7.创建一个downLoad.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
<s:form action="files/downLoad" method="post">
<s:textfield label="下载的地址" name="downLoad"/>
<s:submit value="下载"/>
</s:form>
</body>
8.在struts.xml文件中新增
<!-- 文件下载 -->
<action name="downLoad" class="cn.bdqn.action.DownLoadAction" method="downLoad">
<!-- result节点中 不需要返回界面 可以设置属性 -->
<result type="stream">
<!-- 文件下载到本地下载文件夹中 不写 默认 在界面中显示 源码得知 -->
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
public class StreamResult extends StrutsResultSupport {
private static final long serialVersionUID = -1468409635999059850L;
protected static final Logger LOG = LoggerFactory.getLogger(StreamResult.class);
public static final String DEFAULT_PARAM = "inputName";
protected String contentType = "text/plain";
protected String contentLength;
protected String contentDisposition = "inline";
protected String contentCharSet ;
protected String inputName = "inputStream";
9.创建对应的DownLoadAction
public class DownLoadAction extends ActionSupport {
private InputStream inputStream; //创建输入流对象
private String fileName; //下载文件的名称
private String downLoad; //和前台用户输入的name属性值 一致
//文件下载
public String downLoad(){
try {
inputStream=new FileInputStream(downLoad);
// e:\java\cat.jpg 只需要文件名
int index=downLoad.lastIndexOf("\\");
fileName=downLoad.substring(index+1);
//防止下载的中文乱码
try {
fileName=URLEncoder.encode(fileName, "utf-8");
} catch (UnsupportedEncodingException e) {
System.out.println("乱码问题");
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return INPUT;
}
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getDownLoad() {
return downLoad;
}
public void setDownLoad(String downLoad) {
this.downLoad = downLoad;
}
}