1、titleFile Uploadlink href=%=request.getContextPath()%/css/fileupload.css rel=stylesheet type=text/css/headbody bgcolor=#ccccccdiv style=padding-top: 30px; padding-left: 30pxfieldset style=width: 600px; border-color: #000000; 10pxlegendfont size=-1 color=#000000b上传图片/font/legenddiv class=ErrorDivs:fi
2、elderror/actionerror/divform action=fileUpload.action method=post theme=simple enctype=multipart/form-datatable border=1 width=500pxtrtd class=tableTDCenter 图片路径:/tdfile name=file cssClass=fileInput/ /tr colspan=2submit value=提交button/s:submit/tableform/fieldset/body/html很简单,就是个上传框和按钮,别的没有了。至于一些css就
3、是稍微的装饰装饰,一会也在下面写上!上传成功的画面:Image Listiterator value=#list id=picturedivfloatimg src=outPicture.action?id=下面我们看看struts2的配置文件:?xml version=1.0 encoding=DOCTYPE struts PUBLIC -/Apache Software Foundation/DTD Struts Configuration 2.0/ENhttp:/struts.apache.org/dtds/struts-2.0.dtdstrutsconstant name=struts
4、.custom.i18n.resources value=messageResource /struts.i18n.encodingpackage name=default extends=struts-default-上传-action name=fileUpload class=org.csdn.action.FileUploadActioninterceptor-ref name =defaultStackparam name =allowedTypes image/bmp,image/png,image/gif,image/jpg/paramparam name=maximumSize
5、404800result name=success/uploadSuccess.jspinput/fileUpload.jsp-输出显示-outPictureorg.csdn.action.OutPictureAction/package/struts至于里面的东西是什么,大家肯定根据名字一看就知道了。上面我引用了messageResource资源文件,为了显示错误消息。比如上传类型不正确,超过大小,以及上传失败。下面就是资源文件中的内容。struts.messages.error.content.type.not.allowed=The file you uploaded is not a
6、imagestruts.messages.error.file.too.large=this picutre is too largefileupload.fail=file upload is fail如果没有重新定义上面两个key的话,显示的会是struts2默认的错误消息,很是难看,也乱起八糟的。所以我重新定义了。接着我们就开始写上传和输出图片的action,以及相关的类信息上传的action:import java.io.File;import java.util.List;import org.csdn.service.FileUploadService;import org.csd
7、n.vo.Picture;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/* * author closewubq */public class FileUploadAction extends ActionSupport private static final long serialVersionUID = 1L; private File file; public File getFile() return file; public void setFil
8、e(File file) this.file = file; /* * 上传文件 Override public String execute() FileUploadService fuservice=new FileUploadService(); if(fuservice.fileUpload(file) List list=fuservice.findAll(); ActionContext cxt=ActionContext.getContext(); cxt.put(list,list); return SUCCESS; else super.addActionError(this
9、.getText(fileupload.fail); return INPUT;非常简单,因为我只单纯的用了struts2,没有用spring。所以FileUploadService就硬编码写在里面了。、图片上传的主要业务类import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import org.csdn.
10、connection.GetConnection;public class FileUploadService * 上传图片到数据库 * param flie * return boolean * 上传是否成功 public boolean fileUpload(File flie) FileInputStream in = null; Connection conn = null; PreparedStatement ps = null; try in = new FileInputStream(flie); String sql = insert into savepicture(pict
11、ure) value(?); conn = GetConnection.getConn(); if (conn = null) System.out.println(连接为null); return false; ps = conn.prepareStatement(sql); ps.setBinaryStream(1, in, in.available(); if (ps.executeUpdate() 0) GetConnection.close(conn, ps, null); return true; else catch (Exception e) System.out.printl
12、n(e.getMessage(); * 检索所有图片 * return list * 返回所有图片记录 public List findAll() list = new ArrayList(); Picture pic = null; ResultSet rs = null;select id from savepicture return null; rs = ps.executeQuery(); while (rs.next() pic = new Picture(); pic.setId(rs.getInt(id list.add(pic); GetConnection.close(co
13、nn, ps, rs); return list; e.printStackTrace(); * 根据图片ID获取流对象 * param id * return InputStream public InputStream getPicById(int id) InputStream is = null;select picture from savepicture where id=? ps.setInt(1, id); if(rs.next() is = rs.getBinaryStream( return is; catch (Exception ex) ex.printStackTra
14、ce();简单的写了个jdbc链接的获取类。import java.sql.DriverManager;import java.sql.SQLException; * 获取数据库链接public class GetConnection * 获取数据库连接 * return Connection * 返回数据库连接 public static Connection getConn() Class.forName(com.mysql.jdbc.Driver).newInstance(); String url = jdbc:mysql:/localhost/csdn?user=root&passw
15、ord= Connection connection = DriverManager.getConnection(url); return connection; * 关闭连接释放资源 * param conn * param rs * param st public static void close(Connection conn,PreparedStatement ps,ResultSet rs) if(rs!=null) rs.close(); catch (SQLException e) if(ps! ps.close(); if(conn! conn.close();还有保存图片信
16、息的VOpublic class Picture private int id; public int getId() return id; public void setId(int id) this.id = id;输出图片信息的Actionimport javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContex
17、t; * 图片输出 *public class OutPictureAction extends ActionSupport public String execute() throws Exception HttpServletRequest request = ServletActionContext.getRequest(); int id=Integer.parseInt(request.getParameter( FileUploadService service=new FileUploadService(); InputStream in=service.getPicById(id); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType(image/gif int size=in.available(); byte image=new bytesize; in.read(image); ServletOutputStream out=response.getOutputStream(); out.write(image);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1