有个form
<form action="upload" id="upForm" method="post">
<input type="file" id="att" name="p_w_upload" accept="img/jpeg">
<input type="submit" value="ok">
</form>
看action
package action;
public Class UploadAction extends ActionSupport(){
private File p_w_upload;
private String p_w_uploadContentType;
private String p_w_uploadFileName;
public File getAttachment() {
return p_w_upload;
}
public void setAttachment(File p_w_upload) {
this.p_w_upload = p_w_upload;
}
public String getAttachmentContentType() {
return p_w_uploadContentType;
}
public void setAttachmentContentType(String p_w_uploadContentType) {
this.p_w_uploadContentType = p_w_uploadContentType;
}
public String getAttachmentFileName() {
return p_w_uploadFileName;
}
public void setAttachmentFileName(String p_w_uploadFileName) {
this.p_w_uploadFileName = p_w_uploadFileName;
}
public String upload(){
String realpath=ServletActionContext.getServletContext().getRealPath("/");
FileInputStream fis=new FileInputStream(p_w_upload);
FileOutputStream fos=new FileOutputStream(realpath+"/upload/x.jpg");
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();
return null;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.multipart.maxSize" value="1048576"></constant> <package name="struts2" namespace="/" extends="struts-default"> <action name="upload" class="action.UploadAction" method="upload"> </package> </struts>
复习+备忘
转载于:https://blog.51cto.com/lreach/1420162