上传图片
别的不说,先贴代码。
1.FileUtil.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; /** * 文件处理工具 * @author niuhailiang * */ public class FileUtil { /** * 复制 * @param src * @param dest * @return */ public static boolean copy(File src,File dest){ BufferedInputStream bis=null; BufferedOutputStream bos=null; try { bis=new BufferedInputStream(new FileInputStream(src)); bos=new BufferedOutputStream(new FileOutputStream(dest)); byte[]bts=new byte[1024]; int length=-1; while((length=bis.read(bts))!=-1){ bos.write(bts, 0, length); } return true; } catch (Exception e) { e.printStackTrace(); return false; }finally{ if(bis!=null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos!=null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2.部分上传代码
public String add() { //先上传 System.out.println(".........."+goods.getGoods_name()); src=new String[some.size()]; for(int i=0;i<some.size();i++){ //将文件放到项目部署的路径下的upload文件夹下 path="/upload/"+someFileName.get(i); //根据相对部署的路径计算完整地路径 path=ServletActionContext.getServletContext().getRealPath(path); System.out.println(path); //将临时的文件复制到上述路径下 FileUtil.copy(some.get(i),new File(path)); src[i]=someFileName.get(i); } System.out.println("进入添加方法"); goods.setCategory(categoryServcie.findById(category_id)); goods.setImages(src); goodsService.add(goods); ActionContext.getContext().put("url", "goods_list"); return "redirect"; }
3.页面操作(部分)
<form action="goods_add.action" method="post" name="form" id="form" enctype="multipart/form-data" > <div class="MainDiv"> <table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent"> <tr> <th class="tablestyle_title"> 商品基本信息录入 </th> </tr> <tr> <td class="CPanel"> <table border="0" cellpadding="0" cellspacing="0" style="width: 100%"> <tr> <td width="100%"> <fieldset style="height: 100%;"> <legend> 商品信息 </legend> <table border="0" cellpadding="2" cellspacing="1" style="width: 100%"> <tr> <td align="center"> 商品名: </td> <td > <input name="goods_name" /> </td> </tr> <tr> <td align="center"> 商品类别: </td> <td> <s:select name="category_id" list="#categoryList" listKey="id" listValue="name" id="vip"> </s:select> </td> </tr> <tr> <td align="center"> 价格: </td> <td> <input name="price" /> </td> </tr> <tr> <td align="center"> 会员价格: </td> <td> <input name="vip_price" /> </td> </tr> <tr> <td align="center"> 图片: </td> <td> <input name="some" type="file" /> </td> <td> <input name="some" type="file" /> </td> <td> <input name="some" type="file" /> </td> <td> <input name="some" type="file" /> </td> </tr> <tr> <td align="center"> 描述: </td> <td> <input name="description" /> </td> </tr> </table> <br /> </fieldset> </td> </tr> </table> </td> </tr> <tr> <td colspan="2" align="center" height="50px"> <input type="button" name="Submit" value="保存" onclick="link();" /> <input type="button" name="Submit2" value="返回" onclick="window.history.go(-1);" /> </td> </tr> </table> </td> </tr> </table> </div> </form>
4,显示图片(上传的图片到服务器,但是数据库中保存了上传的路径),轮播显示
<div id="wrapper"> <div id="carousel"> <ul> <s:iterator value="images" status="status"> <li><img src="${pageContext.request.contextPath}/upload/<s:property value="images[#status.index]"/>" /><span>Image</span></li> </s:iterator> </ul> <div class="clearfix"></div> <a id="prev" class="prev" href="#"><</a> <a id="next" class="next" href="#">></a> <div id="pager" class="pager"></div> </div> </div>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。