首页 > Web开发 > 详细

复习struts2+jsp上传文件

时间:2014-06-01 16:28:53      阅读:362      评论:0      收藏:0      [点我收藏+]


有个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上传文件,布布扣,bubuko.com

复习struts2+jsp上传文件

原文:http://lreach.blog.51cto.com/5291289/1420162

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!