图片上传
<form id="form" name="form" action="${basePath }nsfw/user_add.action" method="post" enctype="multipart/form-data">
<tr>
<td class="tdBg" width="200px">头像:</td>
<td>
<input type="file" name="headImg"/>
</td>
</tr>
</form>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="50000000"/>
<package name="user-action" namespace="/nsfw" extends="struts-default">
<action name="user_*" class="main.java.cn.itcastTax.nsfw.user.controller.UserController" method="{1}">
<result name="{1}">/WEB-INF/jsp/nsfw/user/{1}.jsp</result>
<result name="list" type="redirectAction">
<param name="actionName">user_listUI</param>
</result>
</action>
<action name="add"
class="com.silon.web.struts.action.PicAction" method="add">
<interceptor-ref name="fileUpload">
<param name="maximumSize">50000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="input">/pages/pic/picUpload/addUI.jsp</result>
</action>
</package>
</struts>
private File headImg;
private String headImgContentType;
private String headImgFileName;
public String getHeadImgFileName() {
return headImgFileName;
}
public void setHeadImgFileName(String headImgFileName) {
this.headImgFileName = 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 add(){
try {
if(user != null){
if (headImg!=null){
String finePath = ServletActionContext.getServletContext().getRealPath("upload/user");
String fineName= UUID.randomUUID().toString()+headImgFileName.substring(headImgFileName.lastIndexOf("."));
FileUtils.copyFile(headImg,new File(finePath,fineName));
user.setHeadImg("user/"+fineName);
fineName=null;
}
userService.save(user);
}
}catch (Exception e){
e.printStackTrace();
}
return "list";
}