Strts2 picture upload
Three upload schemes
1. Disadvantage of uploading to tomcat server: the storage location of uploaded pictures is too coupled with tomcat server, which is not commonly used.
2. Establishing binary fields in database tables and storing pictures in database. Disadvantage: directly storing pictures in database takes up too much space.
3. Upload to the specified file directory, add the mapping relationship between server and real directory, and decouple the relationship between uploaded file and tomcat.
Let's write a third one today, based on the last code modification.
ClazzAction
package com.hc.crud.web; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.sql.SQLException; import java.util.List; import com.hc.crud.dao.ClazzDao; import com.hc.crud.entity.Clazz; import com.hc.crud.util.BaseAction; import com.hc.crud.util.PageBean; import com.opensymphony.xwork2.ModelDriven; public class ClazzAction extends BaseAction implements ModelDriven<Clazz> { private Clazz clz = new Clazz(); private ClazzDao clzDao = new ClazzDao(); // The attribute name here corresponds to the name in the form xxx private File file; // xxxFileName private String fileFileName; // xxxContentType private String fileContentType; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String list() { PageBean pageBean = new PageBean(); pageBean.setRequest(request); try { List<Clazz> list = this.clzDao.list(clz, pageBean); request.setAttribute("clzList", list); request.setAttribute("pageBean", pageBean); } catch (InstantiationException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "list"; } public String preSave() { if (clz.getCid() != 0) { try { this.result = this.clzDao.list(clz, null).get(0); } catch (InstantiationException | IllegalAccessException | SQLException e) { e.printStackTrace(); } } return "preSave"; } // preUpload /** * Skip Upload Image Interface * @return */ public String preUpload() { try { this.result = this.clzDao.list(clz, null).get(0); } catch (InstantiationException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "preUpload"; } public String upload() { String realDir = "E:/temp/T226"; String serverDir = "/upload"; try { // FileUtils.copyFile(file, new File(realDir + "/" +fileFileName)); copyFile(file, new File(realDir + "/" +fileFileName)); clz.setPic(serverDir + "/" +fileFileName); this.clzDao.edit(clz); } catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "toList"; } /** * Using Buffer Stream Technology to Copy * @param source * @param target * @throws IOException */ public void copyFile(File source,File target) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out= new BufferedOutputStream(new FileOutputStream(target)); byte[] bbuf = new byte[1024]; int len = 0; while((len = in.read(bbuf)) != -1) { out.write(bbuf, 0, len); } in.close(); out.close(); } public String add() { try { this.code = this.clzDao.add(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "toList"; } public String del() { try { this.clzDao.del(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "toList"; } public String edit() { try { this.clzDao.edit(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { e.printStackTrace(); } return "toList"; } @Override public Clazz getModel() { return clz; } }
struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="sy" extends="base" namespace="/sy"> <action name="/hello_*" class="com.hc.web.HelloAction" method="{1}"> <result name="success">/success.jsp</result> </action> <action name="/clz_*" class="com.hc.crud.web.ClazzAction" method="{1}"> <result name="list">/clzList.jsp</result> <result name="preSave">/clzEdit.jsp</result> <result name="preUpload">/clzUpload.jsp</result> <result name="toList" type="redirectAction">clz_list</result> </action> </package> </struts>
clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@taglib uri="/zking" prefix="z" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Class Home Page</title> </head> <body> <h2>Catalogue of Novels</h2> <br> <form action="${pageContext.request.contextPath}/sy/clz_list.action" method="post"> Title:<input type="text" name="bname"> <input type="submit" value="Sure?"> <input type="hidden" name="rows" value="3"> </form> <a href="${pageContext.request.contextPath}/sy/clz_preSave.action">increase</a> <table border="1" width="100%"> <tr> <td>number</td> <td>Class name</td> <td>Teacher</td> <td>Class Pictures</td> <td>operation</td> </tr> <c:forEach items="${clzList }" var="b"> <tr> <td>${b.cid }</td> <td>${b.cname }</td> <td>${b.cteacher }</td> <td> <img style="height: 50px;width: 60px" src="${pageContext.request.contextPath}${b.pic }"> </td> <td> <a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">modify</a> <a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">delete</a> <a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">Picture upload</a> </td> </tr> </c:forEach> </table> <z:page pageBean="${pageBean }"></z:page> </body> </html>
clzUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Picture upload</title> </head> <body> <form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data"> <input type="hidden" name="cid" value="${result.cid }"><br> <input type="hidden" name="cname" value="${result.cname }"><br> <input type="hidden" name="cteacher" value="${result.cteacher }"><br> <input type="file" name="file"> <input type="submit"> </form> </body> </html>
We need to configure a piece of code in server.xml of tomcat service
server.xml
Path represents your project name, docBase represents your path to file
Specific illustrations:
Match in the following code
<Context path="/struts2/upload" docBase="E:/temp/T226/"/>
Final effect