利用struts自带的上传文件拦截器上传图片。
1.jsp页面
<body>
<s:form action="upload" method="post" theme="simple" enctype="multipart/form-data">
<c:if test="${empty imageName }">
<img alt="预览图片" src="<%=basePath%>${imageName}" id="images" name="images" style="display: none;">
</c:if>
<c:if test="${not empty imageName }">
<img alt="预览图片" src="<%=basePath%>${imageName}" id="images" name="images">
</c:if>
<s:file name="file" id="fileImages"></s:file>
<!-- <div id="showpic1" οnclick="document.getElementById('fileImages').click();" data="点击添加图片">选择文件</div> -->
<s:submit value="保存"></s:submit>
</s:form>
</body>
2.struts.xml配置文件
<action name="upload" class="com.itany.action.UploadAction" method="upload">
<result name="success">/upload.jsp</result>
<result name="input">/upload.jsp</result>
<!-- 定义图片上传拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 定义图片大小 -->
<param name="maximumSize">500000</param>
<param name="allowedTypes">
image/jpg,image/jpeg,image/bmp,image/gif,image/png
</param>
</interceptor-ref>
<!-- 自定义了拦截器后必手动定义默认的拦截器,否则默认的拦截器不会被执行 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private File file;
private String fileFileName;
private String fileContentType;
private String uploadUrl="uploadImage";
/**
*
* 上传图片
*
* @return
* @see [类、类#方法、类#成员]
*/
public String upload()
{
ServletContext sc = ServletActionContext.getServletContext();
String realPath = sc.getRealPath(uploadUrl);
String fileExt=getFileExt();
String fileName=getFileName("18251823253");
String fileAllName=fileName+"."+fileExt;
String path="";
if (fileContentType.indexOf("image") >= 0)
{
if (file != null)
{
File saveFile = new File(new File(realPath), fileAllName);
try
{
FileUtils.copyFile(file, saveFile);
File dirFile = new File(new File("/" + uploadUrl), fileAllName);
FileUtils.copyFile(file, dirFile);
path = realPath + "/" + fileName + "." + fileExt;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
HttpSession session = ServletActionContext.getRequest().getSession(false);
session.removeAttribute("imageName");
session.setAttribute("imageName", "/"+uploadUrl+"/"+fileAllName);
return "success";
}
/**
*
* 得到文件的后缀
* <功能详细描述>
* @return
* @see [类、类#方法、类#成员]
*/
private String getFileExt()
{
return FilenameUtils.getExtension(fileFileName);
}
private String getFileName(String username)
{
fileFileName=username;
return fileFileName;
}
public File getFile()
{
return file;
}
public void setFile(File file)
{
this.file = file;
}
public String getFileFileName()
{
return fileFileName;
}
public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}
public String getFileContentType()
{
return fileContentType;
}
public void setFileContentType(String fileContentType)
{
this.fileContentType = fileContentType;
}
public String getUploadUrl()
{
return uploadUrl;
}
public void setUploadUrl(String uploadUrl)
{
this.uploadUrl = uploadUrl;
}
}
在webroot根目录下建个文件夹uploadImage