1. uploads
Upload files to a specified directory, add a mapping between the server and the real directory, thereby decoupling upload files relationship with the tomcat
file server
2.web code optimization
1 package com.yuan.crud.web; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.sql.SQLException; 11 import java.util.List; 12 13 import org.apache.commons.io.FileUtils; 14 15 Import com.opensymphony.xwork2.ModelDriven; 16 Import com.yuan.crud.dao.ClazzDao; . 17 Import com.yuan.crud.entity.Clazz; 18 is Import com.yuan.crud.util.PageBean; . 19 20 is public class ClazzAction the extends BaseAction the implements the ModelDriven <Clazz> { 21 is 22 is Private Clazz CLZ = new new Clazz (); 23 is Private ClazzDao clzdao = new new ClazzDao (); 24 // here must be the name and property value corresponding to the page name 25 Private File file; 26 private String fileFileName; 27 private String fileContentType; 28 29 /** 30 * 跳转上传图片的界面 31 * @return 32 */ 33 public String preUpload() { 34 try { 35 this.result = this.clzdao.list(clz, null).get(0); 36 } catch (InstantiationException | IllegalAccessException | SQLException e) { 37 e.printStackTrace(); 38 is } 39 return "preUpload" ; 40 } 41 is 42 is 43 is public String Upload () { 44 is String realdir = "D: / Image"; // save the tape path so as not to Chinese, 45 String severDir = "/ Upload"; // read path 46 is the try { 47 // FileUtils.copyFile (File, new new File (realdir + "/" + fileFileName)); 48 copyFile (File, new new File (realdir + "/" + fileFileName)); 49 clz.setPic (severDir + "/" + fileFileName); 50 the try { 51 is the this .clzdao.edit (CLZ); 52 is } the catch (an IllegalArgumentException | IllegalAccessException | a NoSuchFieldException | a SecurityException 53 is | SQLException E) { 54 is e.printStackTrace (); 55 } 56 is } the catch (IOException E) { 57 is e.printStackTrace (); 58 } 59 return "toList" ; 60 } 61 is / ** 62 is * copy technique using the buffer flow 63 *@param source 64 * @param target 65 * @throws IOException 66 */ 67 public void copyFile(File source, File target) throws IOException{ 68 try { 69 BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); 70 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target)); 71 byte[] bbuf = new byte[1024]; 72 int len =0; 73 while((len = in.read(bbuf))!=-1) { 74 out.write(bbuf, 0, len); 75 } 76 in.close(); 77 out.close(); 78 } catch (FileNotFoundException e) { 79 e.printStackTrace(); 80 } 81 82 } 83 84 85 86 /** 87 * 查询 88 * @return 89 */ 90 public String list() { 91 PageBean pageBean = new PageBean(); 92 pageBean.setRequest(request); 93 try { 94 List<Clazz> list = this.clzdao.list(clz, pageBean); 95 System.out.println(list); 96 request.setAttribute("clzList", list); 97 request.setAttribute("pageBean", pageBean); 98 } catch (InstantiationException | IllegalAccessException |E SQLException) { 99 // the TODO Auto-Generated Block the catch 100 e.printStackTrace (); 101 } 102 return "List" ; 103 } 104 105 / ** 106 * Jump edit page (new edit page) 107 * @ return 108 * / 109 public String preSave () { 110 IF (clz.getCid () = 0! {) 111 the try { 112 the this .Result = the this .clzdao.list (CLZ, null ) .get (0 ); 113 } catch (InstantiationException | IllegalAccessException | SQLException e) { 114 // TODO Auto-generated catch block 115 e.printStackTrace(); 116 } 117 } 118 return "preSave"; 119 } 120 121 public String add() { 122 try { 123 this.code = this.clzdao.add(clz); 124 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException 125 | SQLException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } 129 return "toList"; 130 } 131 132 public String edit() { 133 try { 134 this.clzdao.edit(clz); 135 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException 136 | SQLException e) { 137 // TODO Auto-generated catch block 138 e.printStackTrace(); 139 } 140 return "toList"; 141 } 142 143 public String del() { 144 try { 145 this.clzdao.del(clz); 146 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException 147 | SQLException e) { 148 // TODO Auto-generated catch block 149 e.printStackTrace(); 150 } 151 return "toList"; 152 } 153 154 155 @Override 156 public Clazz getModel() { 157 158 return clz; 159 } 160 161 public File getFile() { 162 return file; 163 } 164 165 public void setFile(File file) { 166 this.file = file; 167 } 168 169 public String getFileFileName() { 170 return fileFileName; 171 } 172 173 public void setFileFileName(String fileFileName) { 174 this.fileFileName = fileFileName; 175 } 176 177 public String getFileContentType() { 178 return fileContentType; 179 } 180 181 public void setFileContentType(String fileContentType) { 182 this.fileContentType = fileContentType; 183 } 184 185 186 187 188 }
3.jsp Code
3.1 clzList.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <%@taglib prefix="z" uri="/****"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <form action="${pageContext.request.contextPath }/sy/clz_list.action" method="post"> 13 14 班级名: <input type="text" name="cname" /><input type="submit" value="确定"/> 15 16 </form> 17 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action">新增</a> 18 <table border="1" width="100%"> 19 <tr> 20 <td>编号</td> 21 <td>班级名</td> 22 <td>教员</td> 23 <td>图片</td> 24 <td>操作</td> 25 </tr> 26 27 <c:forEach items="${clzList }" var="c"> 28 <tr> 29 <td>${c.cid }</td> 30 <td>${c.cname }</td> 31 <td>${c.cteacher }</td> 32 <td> 33 <img style="height: 60px;width: 60px;" src="${pageContext.request.contextPath }${c.pic}"> 34 </td> 35 <td> 36 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid}">修改</a> 37 <a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid}">删除</a> 38 <a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${c.cid}">图片上传</a> 39 </td> 40 </tr> 41 </c:forEach> 42 </table> 43 <z:Page pageBean="${pageBean }"></z:Page> 44 </body> 45 </html>
3.2 clzUpload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>图片上传</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath }/sy/clz_upload.action" method="post" enctype="multipart/form-data"> 11 <input type="hidden" name="cid" value="${result.cid }"/> 12 <input type="hidden" name="cname" value="${result.cname }"/> 13 <input type="hidden" name="cteacher" value="${result.cteacher }"/> 14 <input type="file" name="file"> 15 <input type="submit" value="ok"/> 16 </form> 17 </body> 18 </html>
4. Configuration xml
4.1 struts sy.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <package name="sy" extends="base" namespace="/sy"> 7 <action name="/hello_*" class="com.yuan.web.HelloAction" method="{1}"> 8 <result name="success">/success.jsp</result> 9 </action> 10 11 12 <action name="/clz_*" class="com.yuan.crud.web.ClazzAction" method="{1}"> 13 <result name="list">/clzList.jsp</result> 14 <result name="preSave">/clzEdit.jsp</result> 15 <result name="preUpload">/clzUpload.jsp</result> 16 <result name="toList" type="redirectAction">clz_list</result> 17 </action> 18 </package> 19 </struts>
4.2 server.xml configuration (found in the Servers project)
Value inserted after the last tag:
<Context path = "/ T226_struts01 / upload" docBase = "D: / Image" />
Note: path to upload the file name of the current project folder, docBase path to save the picture
5. Page Display
5.1 Home
5.2 upload page
5.3 results
Thank you for watching ^ - ^! ! !