package cn.xxx.action;
import java.io.File;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class FileUploadAction extends ActionSupport{
private File file;
private String fileFileName;
private String fileFileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileFileContentType() {
return fileFileContentType;
}
public void setFileFileContentType(String fileFileContentType) {
this.fileFileContentType = fileFileContentType;
}
public String upload(){
String path = ServletActionContext.getServletContext().getRealPath("");//项目发布路径
path = path +"\\upload\\"+"images";
String ext = FilenameUtils.getExtension(fileFileName); //得到后缀
file.renameTo(new File(path, "test"+"."+ext)); //把上传的文件存到path路径下
System.out.println("path:"+path);
return null;
}
}
<s:form action="fileUpload_upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交" />
</s:form>
这样就可以上传了,当然还要jar包。