jsp上传图片,并加以显示

て烟熏妆下的殇ゞ 提交于 2019-12-08 20:56:18

上传图片代码如下,首先是页面:

Jsp代码  

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5.   
  6. %>  
  7. <%@ taglib prefix="s" uri="/struts-tags" %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10.   
  11. <html>  
  12.   
  13.   <head>  
  14.   
  15.     <base href="<%=basePath%>">  
  16.       
  17.     <title>My JSP 'index1.jsp' starting page</title>  
  18.    
  19.     <meta http-equiv="pragma" content="no-cache">  
  20.   
  21.     <meta http-equiv="cache-control" content="no-cache">  
  22.   
  23.     <meta http-equiv="expires" content="0">      
  24.   
  25.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  26.   
  27.     <meta http-equiv="description" content="This is my page">  
  28.   
  29.     <!--  
  30.     <link rel="stylesheet" type="text/css" href="styles.css">  
  31.   
  32.     -->  
  33.   
  34.   </head>  
  35.   
  36.   <body>  
  37.   
  38.   这是一个图片上传程序,在上传的时候form必须加上 enctype="multipart/form-data"  
  39.   
  40.   否则则会有报错的情况发生,action类里新建一个demo对象,对应数据库里一条新的记录  
  41.   
  42.   大概方法就是这样,如果有需要的话还可以再扩展,图片上传代码完毕  
  43.   
  44.   <form id="form1" name="form1" action="text.do" method="post" enctype="multipart/form-data">  
  45.   
  46.     <input type="file" id="userImage" name="userImage">  
  47.   
  48.     <input type="text" id="mytext" name="mytext">  
  49.   
  50.   <input type="submit" value="确定">  
  51.   
  52.   </form>  
  53.   
  54.     </body>  
  55.   
  56. </html>  

 传递至textAction这个类中

我用的是springside的框架,这个框架中的action默认执行的方法是list,其它不变

下边是action类

Java代码  

  1. package com.mytext.action;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4.   
  5. import java.io.ByteArrayInputStream;  
  6. import java.io.File;  
  7.   
  8. import java.io.IOException;  
  9.   
  10. import javax.imageio.ImageIO;  
  11.   
  12. import org.apache.commons.io.output.ByteArrayOutputStream;  
  13.   
  14. import org.springframework.beans.factory.annotation.Autowired;  
  15.   
  16. import org.springside.modules.web.struts2.CRUDActionSupport;  
  17.   
  18. import com.mytext.entity.Demo;  
  19.   
  20. import com.mytext.service.DemoServices;  
  21.   
  22. import com.sun.xml.internal.ws.util.ByteArrayBuffer;  
  23.   
  24. @SuppressWarnings("serial")  
  25.   
  26. public class TextAction extends CRUDActionSupport<Demo>{  
  27.   
  28.     @Autowired  
  29.   
  30.     private DemoServices demoServices;  
  31.       
  32.     private Demo demo;  
  33.   
  34.     private String mytext;  
  35.   
  36.     private File userImage;  
  37.   
  38.     @Override  
  39.   
  40.     public String delete() throws Exception {  
  41.   
  42.         // TODO Auto-generated method stub  
  43.   
  44.         return null;  
  45.   
  46.     }  
  47.   
  48.     @Override  
  49.   
  50.     public String list() throws Exception {       
  51.   
  52.     if(userImage==null)  
  53.   
  54.         {  
  55.                 return "try";  
  56.             }  
  57.   
  58.             prepareModel();  
  59.   
  60.         try {  
  61.   
  62.                 BufferedImage tt = ImageIO.read(userImage);  
  63.   
  64.                 demo.setImage(toByte(tt));  
  65.   
  66.                 demo.setName(mytext);  
  67.   
  68.                 <span style="background-color: #ffffff; color: #000000;">demoServices.save(demo);</span>  
  69.   
  70.   
  71.   
  72.   
  73.             } catch (Exception e) {  
  74.   
  75.                 System.out.println("保存失败");  
  76.   
  77.                 return "try";  
  78.   
  79.             }  
  80.   
  81.             return "try";  
  82.       
  83.         //demoServices.getAllDemo();      
  84.     }  
  85.   
  86.   
  87. //转化为流  
  88.   
  89.     private byte[] toByte(BufferedImage image) throws IOException  
  90.   
  91.   
  92.     {     
  93.   
  94.   
  95.         ByteArrayOutputStream cc = new   ByteArrayOutputStream();  
  96.   
  97.   
  98.             ImageIO.write(image, "jpg", cc);  
  99.   
  100.             return cc.toByteArray();      
  101.   
  102.     }  
  103.   
  104.     @Override  
  105.   
  106.     protected void prepareModel() throws Exception {  
  107.   
  108.         // TODO Auto-generated method stub  
  109.   
  110.         demo = new Demo();  
  111.     }  
  112.   
  113.     @Override  
  114.   
  115.     public String save() throws Exception {  
  116.   
  117.         // TODO Auto-generated method stub  
  118.   
  119.         return null;  
  120.   
  121.     }  
  122.   
  123.     public Demo getModel() {  
  124.   
  125.         // TODO Auto-generated method stub  
  126.   
  127.         return demo;  
  128.   
  129.     }  
  130.   
  131.     public File getUserImg() {  
  132.   
  133.         return userImage;  
  134.   
  135.     }  
  136.   
  137.     public void setUserImg(File userImage) {  
  138.   
  139.         this.userImage = userImage;  
  140.   
  141.     }  
  142.   
  143.     public String getMytext() {  
  144.   
  145.         return mytext;  
  146.   
  147.     }  
  148.   
  149.     public void setMytext(String mytext) {  
  150.   
  151.         this.mytext = mytext;  
  152.   
  153.     }  
  154.   
  155.     public File getUserImage() {  
  156.   
  157.         return userImage;  
  158.     }  
  159.   
  160.     public void setUserImage(File userImage) {  
  161.   
  162.         this.userImage = userImage;  
  163.   
  164.     }  
  165. }  

保存方法,是springside独有的一个东西,不必深究,可跟据自己的框架来保存对象至数据库中

下边是我数据库的实体类

Java代码  

  1. package com.mytext.entity;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import static javax.persistence.GenerationType.IDENTITY;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.Table;  
  9.   
  10. /** 
  11.  * Demo entity. 
  12.  *  
  13.  * @author MyEclipse Persistence Tools 
  14.  */  
  15. @Entity  
  16. @Table(name = "demo")  
  17. <span style="color: #000000;">public class Demo extends IdEntity implements java.io.Serializable</span>  
  18.   
  19.   
  20.   
  21.   
  22. {  
  23.   
  24.     private String name;  
  25.     private byte[] image;  
  26.     private String shouming;  
  27.     public Demo() {  
  28.     }  
  29.     public Demo(String name, byte[] image) {  
  30.         this.name = name;  
  31.         this.image = image;  
  32.     }  
  33.   
  34.     @Column(name = "name", length = 15)  
  35.     public String getName() {  
  36.         return this.name;  
  37.     }  
  38.   
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     @Column(name = "image")  
  44.     public byte[] getImage() {  
  45.         return this.image;  
  46.     }  
  47.   
  48.     public void setImage(byte[] image) {  
  49.         this.image = image;  
  50.     }  
  51.     @Column(name="shouming", length = 225)  
  52.     public String getShouming() {  
  53.         return shouming;  
  54.     }  
  55.     public void setShouming(String shouming) {  
  56.         this.shouming = shouming;  
  57.     }  
  58.   
  59. }  

 继承了一个共用的类,用于id主键生成

Java代码  

  1. package com.mytext.entity;  
  2.   
  3. import javax.persistence.GeneratedValue;  
  4. import javax.persistence.GenerationType;  
  5. import javax.persistence.Id;  
  6. import javax.persistence.MappedSuperclass;  
  7. //所有实体类的基类,实现id的自增长  
  8. @MappedSuperclass  
  9. public class IdEntity {  
  10.     private Long id;  
  11.   
  12.     @Id  
  13.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  14.     public Long getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(Long id) {  
  19.         this.id = id;  
  20.     }  
  21. }  

 我用的是mysql数据库,测试己成功,action方法中继承的是springside框架中的action类,不用这个框架的不必深究,

 

 

以下是在jsp页面中读出图片的方法

 

先是jsp页面

Java代码  

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <span style="color: #000000;"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  7. <c:set var="ctx" value="${pageContext.request.contextPath}"/>  
  8. <%@ taglib prefix="s" uri="/struts-tags"  %></span>  
  9.   
  10.   
  11.   
  12.   
  13.      <!--  注意标签库的异入>  
  14. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  15. <html>  
  16.   <head>  
  17.     <base href="<%=basePath%>">  
  18.       
  19.     <title>jquery编辑器实例</title>  
  20.       
  21.     <meta http-equiv="pragma" content="no-cache">  
  22.     <meta http-equiv="cache-control" content="no-cache">  
  23.     <meta http-equiv="expires" content="0">      
  24.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  25.     <meta http-equiv="description" content="This is my page">  
  26.     <!--  
  27.     <link rel="stylesheet" type="text/css" href="styles.css">  
  28.     -->  
  29.     <link rel="stylesheet" href="${ctx }/edit/common.css" type="text/css" media="screen" />  
  30.     <script type="text/javascript" src="${ctx }/edit/jquery/jquery-1.3.2.min.js"></script>  
  31.     <script type="text/javascript" src="${ctx }/edit/xheditor.js"></script>  
  32.   </head>  
  33.   <body>  
  34.   <!--  jquery的编辑器试验己成功,能够成功写入数据库,能够成功在页面上显示出来 -->  
  35.   一般模式的edit  
  36.   <form method="post" action="edit.do">  
  37.     <h3>xhEditor demo1 : 默认模式</h3>  
  38.     xheditor(默认完全):<br />  
  39.     <textarea id="elm1" name="elm1" class="xheditor" rows="12" cols="80" style="width: 80%">  
  40.     <p>请输入信息</p>  
  41.     </textarea>  
  42.     <br>  
  43.      <input type="submit" value="试验中的jquery编辑器">  
  44.    </form>  
  45.    <!-- 从数据库里边读出一个图片 至此,图片上传和显示制做完毕-->  
  46.    开始从数据库里边读出来一个图片<br>  
  47.   <span style="color: #ff0000;"> <span style="color: #000000;"><img src="<s:url action="edit!getUserImgFromByte.do">  
  48.    <s:param name="id" value="5"></s:param>  
  49.    </s:url>" height="300px" width="300px"> </span>  
  50.   
  51.   
  52. </span>  
  53.   
  54.   
  55.   
  56.   
  57.   
  58.   </body>  
  59. </html>  
 

可人为控制图看大小,上边的是一个类似于fck的jquery编辑器

 

action方法

Java代码  

  1. package com.mytext.action;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletOutputStream;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springside.modules.web.struts2.CRUDActionSupport;  
  10. import org.springside.modules.web.struts2.Struts2Utils;  
  11.   
  12. import com.mytext.entity.Demo;  
  13. import com.mytext.service.DemoServices;  
  14.   
  15. public class EditAction extends CRUDActionSupport<Demo>{  
  16.   
  17.     /** 
  18.      *  
  19.      */  
  20.     @Autowired  
  21.     private DemoServices demoSdrvices;  
  22.       
  23.     private Long id;  
  24.     private String elm1;  
  25.     private Demo demo;  
  26.   
  27.     public Long getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(Long id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     @Override  
  36.     public String delete() throws Exception {  
  37.         // TODO Auto-generated method stub  
  38.         return null;  
  39.     }  
  40.   
  41.     @Override  
  42.     public String list() throws Exception {  
  43.         id=1L;  
  44.         getModel();  
  45.         demo.setShouming(elm1);  
  46.         demoSdrvices.save(demo);  
  47.         return "showedit";  
  48.     }  
  49.   
  50.     @Override  
  51.     protected void prepareModel() throws Exception {  
  52.         // TODO Auto-generated method stub  
  53.           
  54.     }  
  55.   
  56.     @Override  
  57.     public String save() throws Exception {  
  58.         // TODO Auto-generated method stub  
  59.         return null;  
  60.     }  
  61.   
  62.     public Demo getModel() {  
  63.         if(id==null)  
  64.         {  
  65.             demo=new Demo();  
  66.         }  
  67.         else  
  68.         {  
  69.             demo = demoSdrvices.get(id);  
  70.         }  
  71.         return demo;  
  72.     }  
  73.   
  74.     public String getElm1() {  
  75.         return elm1;  
  76.     }  
  77.   
  78.     public void setElm1(String elm1) {  
  79.         this.elm1 = elm1;  
  80.     }  
  81.   
  82.     public Demo getDemo() {  
  83.         return demo;  
  84.     }  
  85.   
  86.     public void setDemo(Demo demo) {  
  87.         this.demo = demo;  
  88.     }  
  89.     <span style="color: #000000;">public String getUserImgFromByte() throws Exception {  
  90.         getModel();  
  91.         HttpServletResponse response = Struts2Utils.getResponse();  
  92.         ServletOutputStream out = null;  
  93.         try {  
  94.             response.setContentType("image/jpeg");  
  95.             out = response.getOutputStream();  
  96.             out.write(demo.getImage());  
  97.             out.flush();  
  98.         } catch (Exception e) {  
  99.         } finally {  
  100.             try {  
  101.                 if (null != out)  
  102.                     out.close();  
  103.             } catch (IOException e) {  
  104.                 e.printStackTrace();  
  105.             }  
  106.         }  
  107.         return null;  
  108.     }</span>  
  109.   
  110.   
  111.   
  112.   
  113.   


 转载自:

 http://wineer200.iteye.com/blog/404303

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!