1 <form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="submit">
上传所需要的jar包:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
2 <action name="upload" class="obt.net.action.UploadAction">
<result name="success">/success4.jsp</result>
</action>
3 import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private File file;
private String fileFileName;
private String fileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String execute() throws Exception{
InputStream is = new FileInputStream(file);
String root = "d://car/";
File descfile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(descfile);
byte buffer[] = new byte[400];
int length = 0;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
return "success";
}
}