添加表单中选择文件上传
<tr> <td class="tdBg" width="200px">头像:</td> <td> <input type="file" name="headImg"/> </td> </tr>在action中接受文件
PRivate File headImg ; private String headImgContentType; private String headImgFileName; public File getHeadImg() { return headImg; } public void setHeadImg(File headImg) { this.headImg = headImg; } public String getHeadImgContentType() { return headImgContentType; } public void setHeadImgContentType(String headImgContentType) { this.headImgContentType = headImgContentType; } public String getHeadImgFileName() { return headImgFileName; } public void setHeadImgFileName(String headImgFileName) { this.headImgFileName = headImgFileName; }在添加方法中保存处理文件
//处理头像if(headImg!=null){ String filePath = ServletActionContext.getServletContext().getRealPath("upload/user");String fileName =UUID.randomUUID().toString().replaceAll("-","")+headImgFileName.substring(headImgFileName.lastIndexOf("."));//复制文件FileUtils.copyFile(headImg, new File(filePath,fileName));//设置头像路径user.setHeadImg("user/"+fileName);}编辑页面上修改
<tr> <td class="tdBg" width="200px">头像:</td> <td> <s:if test="%{user.headImg != null && user.headImg != ''}"> <img src="${basePath }upload/<s:property value='user.headImg'/>" width="100" height="100"/> <s:hidden name="user.headImg"/> </s:if> <input type="file" name="headImg"/> </td> </tr>新闻热点
疑难解答