这里做的是图片的存储和显示
页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985
/** * 功能:这个是实现品牌类和web层的交互 * 时间:2015年5月23日10:31:07 * 文件:BrandAction.java * 作者:cutter_point */ packagecom.cutter_point.web.action.product; import java.util.ArrayList; importjava.util.LinkedHashMap; import java.util.List; import java.util.Map; importjavax.annotation.Resource; importorg.springframework.context.annotation.Scope; importorg.springframework.stereotype.Controller; importcom.cutter_point.bean.PageView; importcom.cutter_point.bean.QueryResult; importcom.cutter_point.bean.product.Brand; importcom.cutter_point.service.product.BrandService; importcom.opensymphony.xwork2.ActionContext; importcom.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") public class BrandActionextends ActionSupport { @Resource private BrandService brandService; //通过接口注入,aop默认的方式 private int page; @Override public String execute() throws Exception { Map request =(Map)ActionContext.getContext().get("request"); PageView<Brand> pageview = newPageView<Brand>(12, this.getPage()); int firstindex = (pageview.getCurrentpage() - 1) *pageview.getMaxresult(); //得到从哪个开始索引的值 LinkedHashMap<String, String> orderby = newLinkedHashMap<String, String>(); orderby.put("code", "asc"); StringBuilder hsql = newStringBuilder("o.visible = ?"); List<Object> params = newArrayList<Object>(); //这个用来存放需要的排序方式 params.add(true); QueryResult<Brand> qr =brandService.getScrollData(Brand.class, firstindex, pageview.getMaxresult(),hsql.toString(), params.toArray(), orderby); pageview.setQueryResult(qr); request.put("pageView", pageview); return "list"; } public void setPage(int page) { this.page = page; } public int getPage() { return page < 1 ? 1 : page; } }
/** * 功能:这个是品牌产品的管理动作 * 时间:2015年5月20日15:40:17 * 文件:ProductTypeManageAction.java * 作者:cutter_point */ packagecom.cutter_point.web.action.product; import java.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.UUID; importjavax.annotation.Resource; importorg.apache.commons.io.FileUtils; importorg.apache.struts2.ServletActionContext; importorg.springframework.context.annotation.Scope; importorg.springframework.stereotype.Controller; importcom.cutter_point.bean.product.Brand; importcom.cutter_point.service.product.BrandService; importcom.opensymphony.xwork2.ActionContext; importcom.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") public class BrandManageActionextends ActionSupport { @Resource private BrandService brandService; //通过接口注入,aop默认的方式 private File logofile; //上传的文件 private String logofileContentType; //这个是struts2自动传进来的属性,文件的内容类型 private String logofileFileName; //这个是struts2自动传进来的属性,上传文件名 private String name; //上传的品牌的名字 /** * 显示品牌添加界面 * @return Stringstruts2的返回跳转result * @throws Exception */ public String addUI() throws Exception { return "add"; } /** * 品牌添加操作,上传文件方式2 * @return * @throws Exception */ public String add2() throws Exception { Map request = (Map)ActionContext.getContext().get("request"); //我们保存文件的格式是images/brand/2015/5/23/ssss.gif //G:\Program Files\Apache SoftwareFoundation\Tomcat 8.0\webapps\babaSport_1100_brand\images\ 这个就是realpath String realpath =ServletActionContext.getServletContext().getRealPath("/images"); //这里我们规定一下格式 SimpleDateFormatdateformat = new SimpleDateFormat("yyy\\MM\\dd\\HH"); String logopath = realpath + "brand\\" +dateformat.format(new Date()); //构建图片保存的目录 Brand brand = new Brand(); brand.setName(this.getName()); //判断文件是否获取,文件获取到,且长度大于0 if(this.getLogofile() != null &&this.getLogofile().length() > 0) { File logosavedir = new File(logopath); //文件的保存路径 if(!logosavedir.exists()) { //如果文件保存路径不存在,我们就创建这个路径 logosavedir.mkdirs(); } //文件的名字 // String imagename = UUID.randomUUID().toString(); //构建文件名称 //文件输出到相应的目录,根据 parent 抽象路径名和 child 路径名字符串创建一个新File 实例。 File savefile = new File(logosavedir,this.getLogofileFileName()); FileUtils.copyFile(logofile, savefile); } brandService.save(brand); request.put("message", "添加品牌成功"); return "message"; } /** * 品牌添加操作,上传文件方式1 * @return * @throws Exception */ public String add() throws Exception { Map request = (Map)ActionContext.getContext().get("request"); //我们保存文件的格式是images/brand/2015/5/23/ssss.gif //G:\Program Files\Apache SoftwareFoundation\Tomcat 8.0\webapps\babaSport_1100_brand\images\ 这个就是realpath String realpath =ServletActionContext.getServletContext().getRealPath("/images"); //这里我们规定一下格式 SimpleDateFormat dateformat = new SimpleDateFormat("yyy\\MM\\dd\\HH"); String logopathdir = realpath +"brand\\" + dateformat.format(new Date()); //构建图片保存的目录 Brand brand = new Brand(); brand.setName(this.getName()); //判断文件是否获取,文件获取到,且长度大于0 if(this.getLogofile() != null &&this.getLogofile().length() > 0) { File logosavedir = new File(logopathdir); //文件的保存路径 if(!logosavedir.exists()) { //如果文件保存路径不存在,我们就创建这个路径 logosavedir.mkdirs(); } //得到图片后缀 String ext =this.getLogofileFileName().substring(this.getLogofileFileName().lastIndexOf('.')); //文件的名字 String showpath = "..\\" +logosavedir.toString().substring(logosavedir.toString().lastIndexOf("\\images")); String imagename = UUID.randomUUID().toString()+ ext; //构建文件名称 System.out.println(imagename +"?????????????????????"); //这里用文件流来传进来 FileOutputStream fos = null; FileInputStream fis = null; try { String logopath = logosavedir +"\\" + imagename; showpath += "\\" +imagename; //建立文件输出流 fos = newFileOutputStream(logopath); //建立文件上传流 fis = newFileInputStream(this.getLogofile()); //设定一个字节缓存 byte[] buffer = new byte[2048]; int len = 0; //每次上传的长度 //不断地从文件上传流输出到输出流 while((len = fis.read(buffer))!= -1) { //输出 fos.write(buffer, 0,len); } brand.setLogopath(showpath); } catch (Exception e) { System.out.println("文件上传失败"); e.printStackTrace(); } finally { this.close(fos, fis); } } brandService.save(brand); request.put("message", "添加品牌成功"); return "message"; } //管理文件流 protected void close(FileOutputStream fos, FileInputStreamfis) { if(fis != null) { try { fis.close(); } catch (Exception e) { System.out.println("关闭文件输入流失败"); e.printStackTrace(); } } if(fos != null) { try { fos.close(); } catch (Exception e) { System.out.println("关闭文件输出流失败"); e.printStackTrace(); } } } public File getLogofile() { return logofile; } public void setLogofile(File logofile) { this.logofile = logofile; } public BrandService getBrandService() { return brandService; } public void setBrandService(BrandService brandService) { this.brandService = brandService; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLogofileContentType() { return logofileContentType; } public void setLogofileContentType(StringlogofileContentType) { this.logofileContentType = logofileContentType; } public String getLogofileFileName() { return logofileFileName; } public void setLogofileFileName(String logofileFileName) { this.logofileFileName = logofileFileName; } }
<%@ page isELIgnored="false"contentType="text/html;charset=UTF-8" %> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>产品类别管理</title> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <link rel="stylesheet"href="../css/vip.css" type="text/css"> <script type="text/javascript"> //到指定的分页页面 function topage(page) { document.getElementById("page").value = page; //alert(document.getElementById("page").value); //document.form1.method= 'post'; //document.form1.submit(); var obj = document.getElementsByName("form1").item(0).submit(); } </script> <SCRIPT language=JavaScript src="../js/FoshanRen.js"></SCRIPT> </head> <body bgcolor="#FFFFFF"text="#000000" marginwidth="0" marginheight="0"> <form id="form11"name="form1" action="brandlist"method="post"> <s:hidden id="page" name="page"/> <table width="98%"border="0" cellspacing="1"cellpadding="2" align="center"> <tr ><td colspan="4" bgcolor="6f8ac4" align="right" > <%@ include file="../share/fenye.jsp"%> </td></tr> <tr> <td width="20%" bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">代号</font></div></td> <td width="5%" nowrapbgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">修改</font></div></td> <td width="45%" bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">名称</font></div></td> <td width="30%" nowrapbgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">Logo</font></div></td> </tr> <!---------------------------LOOPSTART------------------------------> <s:iterator value="#request.pageView.records" var="entry"> <tr> <td bgcolor="f5f5f5"> <div align="center"><s:property value="#entry.code"/></div> </td> <td bgcolor="f5f5f5"> <div align="center"> <a href="<s:url action="edit-producttypemanage"/>?typeid=<s:property value="#entry.typeid"/>"> <img src="../images/edit.gif"width="15" height="16"border="0"> </a> </div> </td> <td bgcolor="f5f5f5" align="center"> <s:property value="#entry.name"/> </td> <td bgcolor="f5f5f5"> <s:if test="#entry.logopath== null">没有Logo</s:if> <s:if test="#entry.logopath!= null"><img width="100"src="${entry.logopath}"></s:if> </td> </tr> </s:iterator> <!----------------------LOOPEND-------------------------------> <tr> <td bgcolor="f5f5f5" colspan="4"align="center"><table width="100%"border="0" cellspacing="1"cellpadding="3"> <tr> <td width="5%"></td> <td width="85%"> <input name="AddDic" type="button"class="frm_btn" id="AddDic"onClick="javascript:window.location.href='<s:url action="add-brandmanage" />'"value="添加品牌"> <input name="query" type="button"class="frm_btn" id="query"onClick="javascript:window.location.href='<s:url action="query-brandmanage" />'"value=" 查询 "> </td> </tr> </table></td> </tr> </table> </form> </body> </html>
<%@ page isELIgnored="false"contentType="text/html;charset=UTF-8" %> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>添加类别</title> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <link rel="stylesheet"href="/css/vip.css" type="text/css"> <SCRIPT language=JavaScript src="/js/FoshanRen.js"></SCRIPT> <script language="JavaScript"> function checkfm(form) { if (trim(form.name.value)=="") { alert("品牌名称不能为空!"); form.name.focus(); return false; } /* if(byteLength(form.note.value)>200) { alert("备注不能大于100字!"); form.note.focus(); returnfalse; } */ return true; } </script> </head> <body bgcolor="#FFFFFF"text="#000000" leftmargin="0" topmargin="0"marginwidth="0" marginheight="0"> <s:form action="brandmanage-add" method="post" enctype="multipart/form-data" onsubmit="returncheckfm(this)"> <br> <table width="90%"border="0" cellspacing="2"cellpadding="3" align="center"> <tr bgcolor="6f8ac4"> <td colspan="2" > <fontcolor="#FFFFFF">添加品牌:</font> </td> </tr> <tr bgcolor="f5f5f5"> <td width="22%" > <div align="right">品牌名称:</div> </td> <td width="78%"> <input name="name" size="50"maxlength="40" /> <fontcolor="#FF0000">*</font> </td> </tr> <tr bgcolor="f5f5f5"> <td width="22%"><div align="right">Logo图片:</div></td> <td width="78%"> <input name="logofile"type="file" size="50"maxlength="100" /> </td> </tr> <tr bgcolor="f5f5f5"> <td colspan="2"><div align="center"> <input type="submit" value=" 确定 " class="frm_btn"> </div></td> </tr> </table> </s:form> <br> </body> </html>
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <include file="struts-default.xml" /> <constant name="struts.ObjectFactory" value="spring" /><!-- 表示这里面的action由spring进行创建 --> <constant name="struts.devMode" value="true" /> <!--解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="control" namespace="/control"extends="struts-default"> <global-results> <result name="message">/page/share/message.jsp</result> </global-results> <action name="center-*"><!-- 直接跳转,不需要经过class的验证,默认返回success --> <result name="success">/page/controlcenter/{1}.jsp</result> </action> <!-- 产品类别展示 --> <action name="producttypelist" class="productTypeAction" method="execute"> <result name="list">/page/product/producttypelist.jsp</result> </action> <!-- 产品类别管理 --> <action name="*-producttypemanage" class="productTypeManageAction" method="{1}UI"> <result name="{1}">/page/product/{1}_productType.jsp</result> </action> <action name="producttypemanage-*" class="productTypeManageAction" method="{1}"> <result name="{1}">/page/product/{1}_productType.jsp</result> </action> <!-- 品牌类别展示 --> <action name="brandlist" class="brandAction"method="execute"> <result name="list">/page/product/brandlist.jsp</result> </action> <!-- 品牌类别管理 --> <action name="*-brandmanage" class="brandManageAction" method="{1}UI"> <result name="{1}">/page/product/{1}_brand.jsp</result> </action> <action name="brandmanage-*" class="brandManageAction" method="{1}"> <result name="{1}">/page/product/{1}_brand.jsp</result> <!-- <interceptor-refname="fileUpload"> 文件大小, 以字节为单位 <paramname="maximumSize">1025956</param> </interceptor-ref> 默认拦截器必须放在fileUpload之后,否则无效 <interceptor-refname="defaultStack" /> --> </action> </package> </struts>
我们访问这个网站
http://localhost:8080/babaSport_2100_cart/control/center-main
关于网站为什么要这样写,上几篇blog有介绍
这里我们做的就是如何把图片上传到服务器,怎么保存,保存到那里去,用什么样的格式保存
原文地址:http://blog.csdn.net/cutter_point/article/details/46544603