Jar包不变
Action类有改变
都改成 集合 例如:List<String>
1.1 Action类
package cn.happy.struts10files;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
/**
* Created by linlin on 2017/10/30.
*/
public class FileAction implements Action{
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
private String savePath;
public String execute() throws Exception {
byte[] buffer=new byte[1024];
int index=0;
for(File item:upload) {
FileInputStream fis = new FileInputStream(item);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName().get(index++));
int length = fis.read(buffer);
while (length > 0) {
fos.write(buffer, 0, length);
length = fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
}
return SUCCESS;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
1.2 Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="55000000"></constant>
<package name="day-10" namespace="" extends="struts-default">
<action name="moreuploadAction" class="cn.happy.struts10files.FileAction">
<param name="savePath">/upload</param>
<result name="success">/file/strutsupload.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1000000</param>
</interceptor-ref>
</action>
</package>
</struts>
1.3页面
<%--
Created by IntelliJ IDEA.
User: linlin
Date: 2017/11/5
Time: 16:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:form action="moreuploadAction" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件"/><br/>
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: linlin
Date: 2017/11/5
Time: 16:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:iterator value="uploadFileName">
文件:<s:property/> <br/>
</s:iterator>
<s:iterator value="uploadContentType">
文件类型:<s:property/> <br/>
</s:iterator>
</body>
</html>
文件下载:
1.1Action类
package cn.happy.struts11down;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/**
* Created by linlin on 2017/11/5.
*/
public class FileDownAction extends ActionSupport {
private String inputPath;
private String fileName;
private InputStream inputStream;
private String contentType;
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() throws FileNotFoundException {
String path= ServletActionContext.getServletContext().getRealPath(inputPath);
return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}
1.2Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="day-11" namespace="" extends="struts-default">
<!--文件下载-->
<action name="filedownAction" class="cn.happy.struts11down.FileDownAction">
<!-- 设置文件路径的参数,传到action类文件中去 -->
<param name="inputPath">/upload</param>
<!-- 下载文件类型定义,即定义为“stream” -->
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<!-- 下载文件输出流定义 -->
<!-- 这里的inputName元素所对应的value值downloadFile,在action中一定要有对应的getDownloadFile()方法 -->
<param name="inputName">inputStream</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param><!--设置响应的HTTP头信息中的Content-Disposition参数的值-->
<!-- 下载缓冲区的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
1.3页面
<%--
Created by IntelliJ IDEA.
User: linlin
Date: 2017/11/5
Time: 17:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="filedownAction.action?fileName=8.jpg">文件下载</a></body>
</body>
</html>