发布网友 发布时间:2022-04-10 18:11
共4个回答
懂视网 时间:2022-04-10 22:33
com.sxt.mvcpro.servlet; import java.io.File; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.UUID; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; @SuppressWarnings("serial") public abstract class BaseServlet extends HttpServlet { SmartUpload smart = null; String newFileName = null; public void saveFile(HttpServletRequest req, HttpServletResponse resp) { smart = new SmartUpload(); ServletConfig config = super.getServletConfig(); try { // 初始化 smart.initialize(config, req, resp); smart.setAllowedFilesList("jpg,png,gif"); smart.setMaxFileSize(1024 * 1024 * 1024 * 5); smart.setTotalMaxFileSize(1024 * 1024 * 1024 * 5 * 10); smart.upload(); // 取得上传文件的路径信息 String filepath = req.getServletContext().getRealPath("/" + this.getDir()); File file = new File(filepath); if (!file.exists()) { file.mkdirs(); } if (smart.getFiles().getFile(0).getSize() > 0) {// 有文件才进行上传 String ext = smart.getFiles().getFile(0).getFileExt(); // 生成新的文件名 String fileName = UUID.randomUUID().toString().replaceAll("-", ""); // 保存文件(按照新文件名保存) newFileName = this.getDir() + fileName + "." + ext; smart.getFiles().getFile(0).saveAs(filepath + fileName + "." + ext); } else { throw new Exception("文件为空"); } } catch (Exception e) { e.printStackTrace(); } } public <T> T initObj(HttpServletRequest req, Class<T> clz) throws IllegalArgumentException, ParseException, UnsupportedEncodingException { // req.setCharacterEncoding("utf-8"); T t = null; try { t = clz.newInstance(); Field[] fs = clz.getDeclaredFields(); for (int i = 0; i < fs.length; i++) { fs[i].setAccessible(true); // 取得属性名 String fname = fs[i].getName(); // 从request内置对象中取得相应的值 String fvalue = null; if (this.smart == null) { fvalue = req.getParameter(fname); } else { fvalue = new String(this.smart.getRequest().getParameter(fname).getBytes("GBK"), "utf-8"); } if (fvalue == null) { continue; } System.out.println(fname+"==" + fvalue); if ("Double".equals(fs[i].getType().getSimpleName())) { fs[i].set(t, Double.valueOf(fvalue)); } else if ("Integer".equals(fs[i].getType().getSimpleName())) { fs[i].set(t, Integer.parseInt(fvalue)); } else if ("Date".equals(fs[i].getType().getSimpleName())) {// 2019-5-5 fs[i].set(t, new SimpleDateFormat("yyyy-MM-dd").parse(fvalue)); } else { fs[i].set(t, fvalue); } } } catch (Exception e) { e.printStackTrace(); } return t; } protected void removeFile(HttpServletRequest req, String fileName) { String filePath = req.getServletContext().getRealPath("/") + fileName; File file = new File(filePath); if (file.exists()) { file.delete(); } } //增加一个抽象方法子类必须实现(文件夹的名称交给子类决定) public abstract String getDir(); }定义emp类:
package com.sxt.mvcpro.vo; import java.io.Serializable; import java.util.Date; public class Emp implements Serializable { private Integer empno; private String ename; private String job; private Double sal; private Date hiredate; private Integer mgr; private Double comm; private Integer deptno; private String img; public Emp() { super(); } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", sal=" + sal + ", hiredate=" + hiredate + ", mgr=" + mgr + ", comm=" + comm + ", deptno=" + deptno + ", img=" + img + "]"; } public void setJob(String job) { this.job = job; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public Emp(Integer empno, String ename, String job, Double sal, Date hiredate, Integer mgr, Double comm, Integer deptno, String img) { this.empno = empno; this.ename = ename; this.job = job; this.sal = sal; this.hiredate = hiredate; this.mgr = mgr; this.comm = comm; this.deptno = deptno; this.img = img; } public Emp(Integer empno, String ename, String job, Double sal, Date hiredate, Integer mgr, Double comm, Integer deptno) { this.empno = empno; this.ename = ename; this.job = job; this.sal = sal; this.hiredate = hiredate; this.mgr = mgr; this.comm = comm; this.deptno = deptno; } }
调整业务层父类(EmpServlet)
package com.sxt.mvcpro.servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sxt.mvcpro.dao.service.IEmpService; import com.sxt.mvcpro.dao.service.impl.EmpServiceImpl; import com.sxt.mvcpro.factory.ServiceFactory; import com.sxt.mvcpro.vo.Emp; @SuppressWarnings("serial") @WebServlet(urlPatterns = { "/emp/*", "/dept/*" }) public class EmpServlet extends BaseServlet { private IEmpService empService = (IEmpService) ServiceFactory.getInstance(EmpServiceImpl.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathInfo = req.getPathInfo(); try { if ("/regist".equals(pathInfo)) { this.regist(req, resp); } } catch (Exception e) { e.printStackTrace(); } } public void regist(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException { super.saveFile(req, resp); // 将基本信息保存到数据库中 Emp emp = null; try { emp = super.initObj(req, Emp.class); emp.setImg(super.newFileName); System.out.println(emp); if (empService.addEmp(emp)) { // 保存添加雇员的信息 req.setAttribute("emp", emp); req.getRequestDispatcher("/pages/emp.jsp").forward(req, resp); } } catch (Exception e) { e.printStackTrace(); } System.out.println(emp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } @Override public String getDir() { return "empImgs/"; }
前端客户端输入界面代码:(newLogin.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <base href="/Mvcpro/"/> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="emp/regist" method="POST" enctype="multipart/form-data"> <fieldset> <legend>请注册</legend> <br>编号:<input type="text" name="empno" value="7550"><br> <br>姓名:<input type="text" name="ename" value="水电费"><br> <br>职位:<input type="text" name="job" value="上单"><br> <br>薪资:<input type="text" name="sal"value="20000"><br> <br>领导编号:<input type="text" name="mgr"value="2343"><br> <br>入职日期:<input type="text" name="hiredate"value="2019-07-04"><br> <br>佣金:<input type="text" name="comm"value="1000"><br> <br>部门编号:<input type="text" name="deptno"value="40"><br> <br>照 片:<input type="file" name="img"><br> <br><input type="submit" value="提交"><input type="reset" value="重置"> </fieldset> </form> </body> </html>
前端客户端输出界面代码:(emp.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>Insert title here</title> <base href="/MvcPro/"/> </head> <body> <h1> 显示雇员的信息:${emp} </h1> <table border="1"> <tr> <td>编号</td><td>姓名</td><td>职位</td><td>薪资</td><td>领导编号</td><td>入职日期</td><td>佣金</td><td>部门编号</td><td>照片</td> </tr> <tr> <td>${emp.empno}</td><td>${emp.ename}</td><td>${emp.job}</td><td>${emp.sal}</td><td>${emp.mgr}</td><td>${emp.hiredate}</td><td>${emp.comm}</td><td>${emp.deptno}</td><td><img src="${emp.img}"></td> </tr> </table> </body> </html>
调试效果图:
混合表单文件上传到数据库(基于TOMCAT)
标签:factory config 代码 ack parse date content list char
热心网友 时间:2022-04-10 19:41
首先,把你的本地文件全部上传到服务器,然后你的域名解析到你的服务器,就可以直接通过域名访问了,当然,前提你服务器php 环境得搭建好。然后把本地的mysql 数据库导入到服务器的mysql 数据库,网站的配置文件把数据库名,数据库密码等信息改对了,网站就可以正常访问了热心网友 时间:2022-04-10 20:59
你好,
首先你有了服务器,说明有一台虚拟的不关机的电脑,并且你在该服务器上安装Tomcat,该电脑有一个ip地址,你可以直接先输入服务器的IP地址检查是否能够访问。如果不能,检查下你的jdk之类的安装好了没;如果能,在Tomcat里有一个conf/server.xml的配置文件
你想访问时不需要添加端口号,就将原默认8080端口改为80
你想更改默认访问路径,修改appBase
你想用域名访问,首先你要将你的项目文件放在webapps里面。然后看需要更改默认访问文件不,一般默认访问index.html
你要实现数据库功能,你的虚拟机上就要有php环境和mysql环境
推荐你用wampserver集成环境,比配置Tomcat方便。
热心网友 时间:2022-04-10 22:33
服务器环境与本地环境配置相同,开放8080端口,以 域名:8080访问。