Action->
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ParentPackage("service-default")
@Results({ @Result(name = "list", location = "/pages/file/upload.jsp") })
public class UploadAction extends BaseAction {
private static final long serialVersionUID = 4885233632866196355L;
private final static Logger log = LoggerFactory.getLogger(UploadAction.class);
private String downpath;
private String fileName;
private File file;
private String[] files;
InputStream fileInputStream = null;
@Action("file_index")
public String list() throws Exception {
String uploadPath = context.getRealPath(File.separator + "upload");
File destDir = new File(uploadPath);
if (destDir.exists() && destDir.isDirectory()) {
files = destDir.list();
}
return "list";
}
@Action("file_upload")
public String file_upload() throws Exception {
File srcFile = getFile();
String temp[] = getFileName().replaceAll("\\\\", "/").split("/");
if (temp.length > 1) {
setFileName(temp[temp.length - 1]);
}
String destFile = context.getRealPath(File.separator + "upload") + File.separator + getFileName();
FileUtils.copyFile(srcFile, new File(destFile));
log.debug("save to >" + destFile);
return list();
}
@Action("file_remove")
public String file_remove() throws Exception {
String filePath = context.getRealPath(File.separator + "upload") + File.separator + getFileName();
File file = new File(filePath);
if (file.exists() && file.isFile()) {
file.delete();
}
return list();
}
@Action("file_download")
public String file_download() throws Exception {
String filePath = context.getRealPath(File.separator + "upload") + File.separator + getFileName();
File file = new File(filePath);
if (file.exists() && file.isFile()) {
setFileName(file.getName());
fileInputStream = new BufferedInputStream(new FileInputStream(file));
}
return "download";
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String[] getFiles() {
return files;
}
public void setFiles(String[] files) {
this.files = files;
}
public InputStream getFileInputStream() {
return fileInputStream;
}
public void setFileInputStream(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
public String getDownpath() {
downpath = context.getContextPath() + "/file_download?fileName=";
return downpath;
}
public void setDownpath(String downpath) {
this.downpath = downpath;
}
}
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.devMode" value="true" /> <constant name="struts.action.extension" value="" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.multipart.maxSize" value="102400000" /> <constant name="struts.i18n.encoding" value="utf-8" /> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <package name="service-default" extends="struts-default"> <global-results> <result name="error">/pages/error.jsp</result> <result name="download" type="stream"> <param name="contentType">application/octet-stream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="inputName">fileInputStream</param> </result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> </package> </struts>
Pages
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>文件上传示例</title>
<script type="text/javascript">
function remove(fileName) {
var url="<%=request.getContextPath()%>/file_remove?fileName="+fileName;
window.document.location = url;
}
function download(fileName) {
var url="<%=request.getContextPath()%>/file_download?fileName="+ fileName;
window.document.location = url;
}
function change(o) {
document.getElementsByName("fileName")[0].value=o.value;
}
function index() {
var url="<%=request.getContextPath()%>/file_index";
window.document.location = url;
}
</script>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="5">
<tr>
<td align="center">ID</td>
<td align="center"><a href="#" οnclick="index()">INDEX</a></td>
<td align="center">Func</td>
</tr>
<s:iterator value="files" status="st">
<tr>
<td><s:property value="#st.index+1" /></td>
<td><div><s:property value="downpath" /><s:property /></div></td>
<td>
<a href="#" οnclick="remove('<s:property />')">remove</a>
<a href="#" οnclick="download('<s:property />')">download</a>
</td>
</tr>
</s:iterator>
</table>
<br>
<s:form name="myform" action="/file_upload" method="POST" enctype="multipart/form-data">
<s:hidden name="fileName" />
<td>
<input type="file" name="file" value="" id="file_upload_file" οnchange="change(this)"/>
<input type="submit" value="上传"/>
</td>
</s:form>
</body>
</html>