struts2上传图片后并显示图片
来源:互联网 发布:淘宝卖小样的正品店 编辑:程序博客网 时间:2024/04/28 11:14
利用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" onclick="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
0 0
- struts2上传图片后并显示图片
- 图片上传并显示
- uploadfiy插件文件上传后,并显示上传的图片
- struts2上传图片,并显示在 jsp 上
- struts2 图片上传到服务器并显示在页面
- struts2上传图片和显示图片
- 上传图片后立即显示
- ssh上传并显示图片
- jsp 上传图片并显示
- ssh上传并显示图片
- html5 上传图片并显示
- html上传图片并显示
- [前端] 上传图片并显示
- springmvc上传图片并显示图片--支持多图片上传
- springmvc上传图片并显示图片--支持多图片上传
- springmvc上传图片并显示图片--支持多图片上传
- springmvc上传图片并显示图片--支持多图片上传
- springmvc上传图片并显示图片--支持多图片上传
- POJ 2296 Map Labeler(二分边长+2-sat判解)(经典题)
- Truck History
- java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和联系
- MFC - EXCEL表格
- MemStore刷写线程—MemStoreFlusher源代码分析
- struts2上传图片后并显示图片
- 在本地部署Metro应用程序的方法
- 1.GitHub启程
- 【Lucas】 HDOJ 5226 Tom and matrix
- 简单的几个shell脚本三
- xcode6中添加pch文件
- 将datatable中的数据更新进入数据库
- OpenCV源码阅读(2)---matx.h---函数的内联实现
- 编写socket通信必备知识