Picture upload for Struts

Keywords: Java JSP Struts xml

File upload based on previous blog

Website: https://blog.csdn.net/weixin_45174537/article/details/97618636

File upload:
Three upload schemes
1. Upload to tomcat server The location where the uploaded pictures are stored is too coupled with the tomcat server
2. Upload to a specified file directory, add a mapping relationship between the server and the real directory, thereby decoupling the relationship between the uploaded file and tomcat
File Server
3. Create binary fields in database tables to store pictures in database

There are three upload schemes, but we use the second one

ClazzAction

package com.caoguangli.crud.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.caoguangli.crud.dao.ClazzDao;
import com.caoguangli.crud.entity.Clazz;
import com.caoguangli.crud.util.BaseAction;
import com.caoguangli.crud.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>  {

	private Clazz clazz = new Clazz();
	private ClazzDao clazzDao = new ClazzDao();
//	The attribute name here corresponds to the name xxx
	private File file;
//	xxxFileName
	private String fileFileName;
//	xxxContentType
	private String fileContentType;
	
	/**
	 * Skip Upload Picture Interface
	 * @return
	 */
	public String preUpload() {
			try {
				this.result = this.clazzDao.list(clazz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				e.printStackTrace();
			}
		return "preUpload";
	}

	public String upload() {
		String realDir = "F:/tupian";
		String severDir = "/upload";
		try {
//			FileUtils.copyFile(file, new File(realDir + "/" + "fileFileName"));
			copyFile(file,new File(realDir + "/" +"fileFileName"));
			clazz.setPic(severDir + "/" +fileFileName);
		    this.clazzDao.edit(clazz);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
		
	}
	
	/**
	 * Copying with Buffer Stream Technology
	 * @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();
	}
	
	/**
	 * Query Method
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public String list() throws InstantiationException, IllegalAccessException, SQLException {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		List<Clazz> list = this.clazzDao.list(clazz, pageBean);
		request.setAttribute("clazzList", list);
		request.setAttribute("pageBean", pageBean);
		return "list";
		
	}
	/**
	 * Jump to Edit Page (New Modification Page)
	 * @return
	 */
	
	public String preSave() {
		if(clazz.getCid() != 0) {
			try {
				this.result = this.clazzDao.list(clazz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "preSave";
		
	}
	
	
	public String add() {
		try {
			this.clazzDao.add(clazz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
		
	}
	
	public String edit() {
		try {
			this.clazzDao.edit(clazz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
		
	}
	public String del() {
		try {
			this.clazzDao.del(clazz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
		
	}
	
	@Override
	public Clazz getModel() {
		// TODO Auto-generated method stub
		return clazz;
	}
	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;
	}
	
	
}

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.caoguangli.one.web.HelloAction" method="{1}">
	       <result name="success">/success.jsp</result>
	   </action>
	   <action name="/demo_*" class="com.caoguangli.test.Demo7" method="{1}">
	       <result name="success">/success.jsp</result>
	   </action>
	   <action name="/clazz_*" class="com.caoguangli.crud.web.ClazzAction" method="{1}">
	       <result name="list">/clazzList.jsp</result>
	       <result name="preSave">/clazzEdit.jsp</result>
	      <result name="preUpload">/clazzUpload.jsp</result>
	       <result name="toList" type="redirectAction">/clazz_list</result>
	   </action>
	</package>
</struts>

clazzList.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="/caoguangli" prefix="d" %>
<!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 Main Interface</title>
</head>
<body>
	<br>

	<form action="${pageContext.request.contextPath}/sy/clazz_list.action"
		method="post">
		Class name:<input type="text" name="cname"> <input type="submit"
			value="Determine">
			<input type="hidden" name="rows" value="15">
	</form>
	<a href="${pageContext.request.contextPath}/sy/clazz_preSave.action">increase</a>
	<table border="1" width="100%">
		<tr>
			<td>number</td>
			<td>Class name</td>
			<td>Teacher</td>
			<td>Class picture</td>
			<td>operation</td>
		</tr>
		<c:forEach items="${clazzList }" var="c">
			<tr>
				<td>${c.cid }</td>
				<td>${c.cname }</td>
				<td>${c.cteacher }</td>
				<td>
				    <img style="height: 60px;width: 60px" src="${pageContext.request.contextPath}${c.pic }">
				</td>
				<td>
					<a href="${pageContext.request.contextPath}/sy/clazz_preSave.action?cid=${c.cid}">modify</a>&nbsp;
					<a href="${pageContext.request.contextPath}/sy/clazz_del.action?cid=${c.cid}">delete</a>
					<a href="${pageContext.request.contextPath}/sy/clazz_preUpload.action?cid=${c.cid}">Picture Upload</a>
				</td>
			</tr>
		</c:forEach>
	</table>
	
	<d:page pageBean="${pageBean }"></d:page>

</body>
</html>

clazzUpload.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/clazz_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" ><br>
	<input type="submit">
</form>
</body>
</html>

One thing to note here is that you need to modify the configuration file server.xml inside servers to add it at the end

server.xml

<Context path="/web_struts/upload" docBase="F:/tupian/"/>


Page effect:

Posted by mduran on Tue, 30 Jul 2019 11:58:05 -0700