上传图片代码如下,首先是页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 这是一个图片上传程序,在上传的时候form必须加上 enctype="multipart/form-data" 否则则会有报错的情况发生,action类里新建一个demo对象,对应数据库里一条新的记录 大概方法就是这样,如果有需要的话还可以再扩展,图片上传代码完毕 <form id="form1" name="form1" action="text.do" method="post" enctype="multipart/form-data"> <input type="file" id="userImage" name="userImage"> <input type="text" id="mytext" name="mytext"> <input type="submit" value="确定"> </form> </body> </html>
传递至textAction这个类中
我用的是springside的框架,这个框架中的action默认执行的方法是list,其它不变
下边是action类
package com.mytext.action;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.CRUDActionSupport;
import com.mytext.entity.Demo;
import com.mytext.service.DemoServices;
import com.sun.xml.internal.ws.util.ByteArrayBuffer;
@SuppressWarnings("serial")
public class TextAction extends CRUDActionSupport<Demo>{
@Autowired
private DemoServices demoServices;
private Demo demo;
private String mytext;
private File userImage;
@Override
public String delete() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String list() throws Exception {
if(userImage==null)
{
return "try";
}
prepareModel();
try {
BufferedImage tt = ImageIO.read(userImage);
demo.setImage(toByte(tt));
demo.setName(mytext);
demoServices.save(demo);
} catch (Exception e) {
System.out.println("保存失败");
return "try";
}
return "try";
//demoServices.getAllDemo();
}
//转化为流
private byte[] toByte(BufferedImage image) throws IOException
{
ByteArrayOutputStream cc = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", cc);
return cc.toByteArray();
}
@Override
protected void prepareModel() throws Exception {
// TODO Auto-generated method stub
demo = new Demo();
}
@Override
public String save() throws Exception {
// TODO Auto-generated method stub
return null;
}
public Demo getModel() {
// TODO Auto-generated method stub
return demo;
}
public File getUserImg() {
return userImage;
}
public void setUserImg(File userImage) {
this.userImage = userImage;
}
public String getMytext() {
return mytext;
}
public void setMytext(String mytext) {
this.mytext = mytext;
}
public File getUserImage() {
return userImage;
}
public void setUserImage(File userImage) {
this.userImage = userImage;
}
}
文中加红的为保存方法,是springside独有的一个东西,不必深究,可跟据自己的框架来保存对象至数据库中
下边是我数据库的实体类
package com.mytext.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Demo entity.
*
* @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "demo")
public class Demo extends IdEntity implements java.io.Serializable
{
private String name;
private byte[] image;
private String shouming;
public Demo() {
}
public Demo(String name, byte[] image) {
this.name = name;
this.image = image;
}
@Column(name = "name", length = 15)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "image")
public byte[] getImage() {
return this.image;
}
public void setImage(byte[] image) {
this.image = image;
}
@Column(name="shouming", length = 225)
public String getShouming() {
return shouming;
}
public void setShouming(String shouming) {
this.shouming = shouming;
}
}
继承了一个共用的类,用于id主键生成
package com.mytext.entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; //所有实体类的基类,实现id的自增长 @MappedSuperclass public class IdEntity { private Long id; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
我用的是mysql数据库,测试己成功,action方法中继承的是springside框架中的action类,不用这个框架的不必深究,
以下是在jsp页面中读出图片的方法
先是jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="ctx" value="${pageContext.request.contextPath}"/> <%@ taglib prefix="s" uri="/struts-tags" %> <!-- 注意标签库的异入> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>jquery编辑器实例</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" href="${ctx }/edit/common.css" type="text/css" media="screen" /> <script type="text/javascript" src="${ctx }/edit/jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="${ctx }/edit/xheditor.js"></script> </head> <body> <!-- jquery的编辑器试验己成功,能够成功写入数据库,能够成功在页面上显示出来 --> 一般模式的edit <form method="post" action="edit.do"> <h3>xhEditor demo1 : 默认模式</h3> xheditor(默认完全):<br /> <textarea id="elm1" name="elm1" class="xheditor" rows="12" cols="80" style="width: 80%"> <p>请输入信息</p> </textarea> <br> <input type="submit" value="试验中的jquery编辑器"> </form> <!-- 从数据库里边读出一个图片 至此,图片上传和显示制做完毕--> 开始从数据库里边读出来一个图片<br> <img src="<s:url action="edit!getUserImgFromByte.do"> <s:param name="id" value="5"></s:param> </s:url>" height="300px" width="300px"> </body> </html>
红色部分为显示图片,可人为控制图看大小,上边的是一个类似于fck的jquery编辑器
action方法
package com.mytext.action;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;
import com.mytext.entity.Demo;
import com.mytext.service.DemoServices;
public class EditAction extends CRUDActionSupport<Demo>{
/**
*
*/
@Autowired
private DemoServices demoSdrvices;
private Long id;
private String elm1;
private Demo demo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String delete() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String list() throws Exception {
id=1L;
getModel();
demo.setShouming(elm1);
demoSdrvices.save(demo);
return "showedit";
}
@Override
protected void prepareModel() throws Exception {
// TODO Auto-generated method stub
}
@Override
public String save() throws Exception {
// TODO Auto-generated method stub
return null;
}
public Demo getModel() {
if(id==null)
{
demo=new Demo();
}
else
{
demo = demoSdrvices.get(id);
}
return demo;
}
public String getElm1() {
return elm1;
}
public void setElm1(String elm1) {
this.elm1 = elm1;
}
public Demo getDemo() {
return demo;
}
public void setDemo(Demo demo) {
this.demo = demo;
}
public String getUserImgFromByte() throws Exception {
getModel();
HttpServletResponse response = Struts2Utils.getResponse();
ServletOutputStream out = null;
try {
response.setContentType("image/jpeg");
out = response.getOutputStream();
out.write(demo.getImage());
out.flush();
} catch (Exception e) {
} finally {
try {
if (null != out)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
红色部分为从数据库读图片代码
转载使用请注明出处!写的时候不知道,后来才发现代码内部不能加红!!!
真屎!
读本人博客代码如有任何疑问或代码不能执行,请速联系本人