复习struts2+jsp上传文件


有个form

<form action="upload" id="upForm" method="post">
    <input type="file" id="att" name="attachment" accept="img/jpeg">
    <input type="submit" value="ok">
</form>


看action

package action;
public Class UploadAction extends ActionSupport(){
    private File attachment;
    private String attachmentContentType;
    private String attachmentFileName;
    public File getAttachment() {
    	return attachment;
    }
    public void setAttachment(File attachment) {
    	this.attachment = attachment;
    }
    public String getAttachmentContentType() {
    	return attachmentContentType;
    }
    public void setAttachmentContentType(String attachmentContentType) {
    	this.attachmentContentType = attachmentContentType;
    }
    public String getAttachmentFileName() {
    	return attachmentFileName;
    }
    public void setAttachmentFileName(String attachmentFileName) {
    	this.attachmentFileName = attachmentFileName;
    }
    public String upload(){
        String realpath=ServletActionContext.getServletContext().getRealPath("/");
        FileInputStream fis=new FileInputStream(attachment);
		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>


复习+备忘

复习struts2+jsp上传文件,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。