首页 > 代码库 > 复习struts2+jsp上传文件
复习struts2+jsp上传文件
有个form
<form action="upload" id="upForm" method="post"> <input type="file" id="att" name="attachment" accept="img/jpeg"> <input type="submit" value="http://www.mamicode.com/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="http://www.mamicode.com/1048576"></constant> <package name="struts2" namespace="/" extends="struts-default"> <action name="upload" class="action.UploadAction" method="upload"> </package> </struts>
复习+备忘
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。