添加表單中選擇文件上傳
<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>新聞熱點
疑難解答