java-张侃― struts2-上传功能 (二)
java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
struts.xml文件
[html]
< xml version="1.0" encoding="UTF-8">
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
method="{1}">
WEB-INF/uploads
[html]
< xml version="1.0" encoding="UTF-8">
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
method="{1}">
WEB-INF/uploads
struts-constant.xml
[html]
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
[
html] view plaincopy
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
UploadAction文件
[java]
package www.csdn.struts_upload.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File[] upload; // 上传的文件
private String[] uploadContentType;// 文件类型
private String[] uploadFileName;// 文件的名称
private String path;
public String getPath() {
return ServletActionContext.getServletContext().getRealPath(path);
}
public void setPath(String path) {
this.path = path;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String upload() {
File file = new File(getPath());
if (!file.exists()) {
file.mkdirs();
}
try {
if (upload != null) {
for (int i = 0; i < upload.length; i++) {
File uploadFile = upload[i];
File