1.struts是自带的 文件上传拦截器
于是 配置 上有些变化
<action name="upload" class="com.actions.uploadAction" > <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg,image/bmp</param> </interceptor-ref> <interceptor-ref name="default"></interceptor-ref> <param name="savePath">/uploads</param> <result name="suc">/suc.jsp</result> </action> 然后就是 jsp页面的 form 需要加 特殊的 属性 <form action="upload.action" method="post" enctype="multipart/form-data"> <input name="title" type="text" /> <input name="upload" type="file" id="upload" οnchange="showUpload()" /> <input type="submit" value="体检" /><br/> <img style="display:none;" id="uploading"/> </form>最后就是 action处理页面package com.actions; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class uploadAction extends ActionSupport{ private String title; private File upload; private String uploadName; private String uploadContentType; // 这是自己 配置 private String savePath; public String execute(){ try { FileUtils.copyDirectory(upload,new File(this.getSavePath())); return "suc"; } catch (IOException e) { e.printStackTrace(); } return null; } public String getSavePath() { return ServletActionContext.getRequest().getRealPath(savePath)+"\\"+this.getUploadName(); } public void setSavePath(String savePath) { this.savePath = savePath; } }
