1.上传方式
上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器
2.web代码优化
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 import com.yuan.crud.util.PageBean; 19 20 public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ 21 22 private Clazz clz = new Clazz(); 23 private ClazzDao clzdao = new ClazzDao(); 24 //这里的属性名一定要和页面上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 } 39 return "preUpload"; 40 } 41 42 43 public String upload() { 44 String realDir="D:/image";//保存路径尽量不要带中文, 45 String severDir = "/upload";//读取路径 46 try { 47 // FileUtils.copyFile(file, new File(realDir+"/"+fileFileName)); 48 copyFile(file, new File(realDir+"/"+fileFileName)); 49 clz.setPic(severDir+"/"+fileFileName); 50 try { 51 this.clzdao.edit(clz); 52 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException 53 | SQLException e) { 54 e.printStackTrace(); 55 } 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 return "toList"; 60 } 61 /** 62 * 利用缓冲流技术进行拷贝 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 | SQLException e) { 99 // TODO Auto-generated catch block 100 e.printStackTrace(); 101 } 102 return "list"; 103 } 104 105 /** 106 * 跳转编辑页面(新增修改页面) 107 * @return 108 */ 109 public String preSave() { 110 if(clz.getCid()!=0) { 111 try { 112 this.result = 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代码
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; 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.配置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 配置(在Servers项目中找到)
在最后一个Value标签后面插入:
<Context path="/T226_struts01/upload" docBase="D:/image"/>
注:path为当前项目名下的upload文件夹,docBase为图片保存的路径
5. 页面显示
5.1 首页
5.2 上传页面
5.3 结果
谢谢观看 ^-^!!!