webxml <?xml version= encoding=UTF?>
<webapp version=
xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
<filter>
<filtername>struts</filtername>
<filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass>
</filter>
<filtermapping>
<filtername>struts</filtername>
<urlpattern>/*</urlpattern>
</filtermapping>
</webapp>
strutsxml
<?xml version= encoding=UTF ?>
<!DOCTYPE struts PUBLIC
//Apache Software Foundation//DTD Struts Configuration //EN
dtd>
<struts>
<package name=struts extends=strutsdefault>
<action name=upload class=comxiestrutsuploadUploadAction>
<result name=success>/upload/resultjsp</result>
</action>
</package>
</struts>
uploadjsp
<%@ page language=java contentType=text/html; charset=GB
pageEncoding=GB%>
<%@ taglib prefix=s uri=/strutstags %>
<html>
<head>
<title>upload</title>
</head>
<body>
<s:form action=upload enctype=multipart/formdata>
<s:textfield name=username id=username label=username/>
<s:file name=file id=file label=file/>
<s:submit/>
</s:form>
</body>
</html>
resultjsp
<%@ page language=java contentType=text/html; charset=GB
pageEncoding=GB%>
<%@ taglib prefix=s uri=/strutstags%>
<html>
<head>
<title>result</title>
</head>
<body>
<s:property value=username />
<br>
<s:property value=fileFileName />
</body>
</html>
UploadActionjava
package comxiestrutsupload;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioInputStream;
import javaioOutputStream;
import orgapachestrutsServletActionContext;
import comopensymphonyxworkActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private File file;
private String fileFileName; // 有属性file+Filename固定组成
private String fileContentType; // 有属性file+ContentType固定组成
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public File getFile() {
return file;
}
public void setFile(File file) {
thisfile = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
thisfileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
thisfileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String root = ServletActionContextgetRequest()getRealPath(/temp);
File destFile = new File(root thisgetFileFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[];
int length = ;
while ((length isread(buffer)) > ) {
oswrite(buffer length);
}
isclose();
osclose();
return SUCCESS;
}
}