1、整合MVC实现文件上传
整合MVC实现文件上传
在实际的开发中在实现文件上传的同时肯定还有其他信息需要保存到数据库,文件上传完毕之后需要将提交的基本信息插入数据库,那么我们来实现这个操作。
整个MVC实现文件上传
1、拷贝之前的dao层和service
2、开发控制层(Controller)
3、调整dao层的父类(BaseController)
<%@ 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="testupload/regist" method="post" enctype="multipart/form-data">
<fieldset>
<legend>请注册</legend>
name:<input type="text" name="ename" value="king"><br><br>
sal:<input type="text" name="sal" value="9999.00"><br><br>
job:<input type="text" name="job" value="manager"><br><br>
comm:<input type="text" name="comm" value="3000.00"><br><br>
deptno:<input type="text" name="deptno" value="10"><br><br>
照&nbsp;&nbsp;&nbsp;片:<input type="file" name="pic"><br><br>
<input type="submit" value="提交"><br><br>
<input type="reset" value="重置">
</fieldset>
</form> </body>
</html>
package cn.sxt.mvcpro.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import cn.sxt.mvcpro.service.IEmpService;
import cn.sxt.mvcpro.service.impl.EmpServiceImpl;
import cn.sxt.mvcpro.servicefactory.ServiceFactory;
import cn.sxt.mvcpro.vo.Emp; @SuppressWarnings("serial")
@WebServlet("/testupload/*")
public class TestUpload extends BaseServlet{
@SuppressWarnings("static-access")
IEmpService proxy = (IEmpService) new ServiceFactory().getInstance(EmpServiceImpl.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathInfo = req.getPathInfo();
if ("/regist".equals(pathInfo)) {
this.regist(req,resp);
}
}
private void regist(HttpServletRequest req, HttpServletResponse resp) {
// System.out.println(req.getParameter("username"));
saveFile(req, resp);
Emp emp = initObj(req, Emp.class);
System.out.println(emp);
System.out.println(proxy.addEmp(emp));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public String getDir() {
return "empImg/";
}
}
package cn.sxt.mvcpro.controller;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException; @SuppressWarnings("serial")
public abstract class BaseServlet extends HttpServlet {
SmartUpload smart = null;
String newFileName = null; public void saveFile(HttpServletRequest req, HttpServletResponse resp) {
try {
// 1.实例化上传的工具对象(jspSmartUpload.jar)
smart = new SmartUpload();
// 2.取得config内置对象
ServletConfig config = this.getServletConfig();
// 3.初始化smart
smart.initialize(config, req, resp);
// 4.设置相关参数
smart.setAllowedFilesList("jpg,png,gif");
// smart.setDeniedFilesList("mp4");
smart.setMaxFileSize(1024 * 1024 * 1024 * 3);
smart.setTotalMaxFileSize(1024 * 1024 * 1024 * 3 * 10);
// 5.实现上传(将文件放到内存,还没有到 磁盘)
smart.upload();
// 6.取得项目部署路径
String savePath = req.getServletContext().getRealPath("/"+this.getDir());
System.out.println(savePath);
File file = new File(savePath);
if (!file.exists()) {
file.mkdirs();
}
// 按照文件的原名保存--》 smart.save(savePath);
// 7.为文件重命名保存(避免覆盖)
String ext = smart.getFiles().getFile(0).getFileExt();// 取得文件扩展名
System.out.println(smart.getFiles());
System.out.println(smart.getFiles().getFile(0));
System.out.println(smart.getFiles().getFile(0).getFileExt());
newFileName = UUID.randomUUID() + "." + ext;
smart.getFiles().getFile(0).saveAs(savePath + newFileName.replaceAll("-", ""));
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmartUploadException e) {
e.printStackTrace();
} } public <T> T initObj(HttpServletRequest req, Class<T> clz) {
T t = null;
try {
t = clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
String fname = f.getName();
// System.out.println(fname + "================");
String fvalue = null;
if (this.smart == null) {
fvalue = req.getParameter(fname);
// System.out.println(fvalue + ">>>>>>>>>>>>");
} else {
fvalue = smart.getRequest().getParameter(fname);
// System.out.println(fvalue + "<<<<<<<<<<<<<<<");
}
if (fvalue == null) {
continue;
}
if ("Double".equals(f.getType().getSimpleName())) {
f.set(t, Double.valueOf(fvalue));
} else if ("Integer".equals(f.getType().getSimpleName())) {
f.set(t, Integer.valueOf(fvalue));
} else if ("Date".equals(f.getType().getSimpleName())) {
try {
f.set(t, new SimpleDateFormat("yyyy-MM-dd").parse(fvalue));
} catch (IllegalArgumentException | ParseException e) {
e.printStackTrace();
}
} else {
f.set(t, fvalue);
}
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return t;
} // 定义一个抽象方法 子类必须实现(文件夹的名称交给子类决定)
public abstract String getDir();
}

整合MVC实现文件上传的更多相关文章

  1. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  2. MVC之文件上传1

    MVC之文件上传 前言 这一节我们来讲讲在MVC中如何进行文件的上传,我们逐步深入,一起来看看. Upload File(一) 我们在默认创建的项目中的Home控制器下添加如下: public Act ...

  3. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  4. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  5. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  6. 【Spring学习笔记-MVC-13】Spring MVC之文件上传

    作者:ssslinppp       1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...

  7. MVC图片上传、浏览、删除 ASP.NET MVC之文件上传【一】(八) ASP.NET MVC 图片上传到服务器

    MVC图片上传.浏览.删除   1.存储配置信息 在web.config中,添加配置信息节点 <appSettings> <add key="UploadPath" ...

  8. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  9. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

随机推荐

  1. mvc框架模式

    首先分为3个板块 路由的api相当于一个域名. 根据当前地址在执行路由里的代码; 逻辑层: 书写业务逻辑的都代码都放在controller层 数据处理层: model 写数据的增删改查方法,导出一般供 ...

  2. 一键部署office的工具——OTool

    OTool可用于office的下载.安装和激活,其激活方式是调用kmspico服务器进行的,官方网站是https://otp.landian.vip/zh-cn/,最新版本5.9.3.6在2019/4 ...

  3. 使用命令查看 Laravel 的版本

    进入项目根目录文件夹后,进入命令行,输入命令: php artisan --version 或者输入: php artisan 会出现 artisan 的帮助文档,最上面就是 laravel 的版本号

  4. python selenium-webdriver 登录验证码的处理(十二)

    很多系统为了防止坏人,会增加各样形式的验证码,做测试最头痛的莫过于验证码的处理,验证码的处理一般分为三种方法 1.开发给我们设置一个万能的验证码: 2.开发将验证码给屏蔽掉: 3.自己识别图片的上的千 ...

  5. Git与Bitbucket配合使用

    1 , 简介 Git : Git是目前世界上最先进的分布式版本控制系统 Bitbucket : BitBucket 是一家代码托管网站 , 类似与GitHub , 不同的是GitHub更专注于开源 , ...

  6. pycharm2018.11最新激活码

    第一步:先按下键盘的win + r ,然后复制c:\windows\system32\drivers\etc粘贴到对话框回车打开文件管理器: 第二步:打开hosts文件,将0.0.0.0 accoun ...

  7. 查看局域网内所有IP的方法

    1,windows下查看局域网内所有IP的方法: 在MS-DOS命令行输入arp -a 2,Linux下,查看局域网内所有IP的方法: 在命令行输入nmap -sP 172.10.3.0/24

  8. WPF 开源项目

    Modern UI for WPF :http://mui.codeplex.com/ 利用Wpf实现Win8 Modern样式的开源项目wpf toolkit :http://wpftoolkit. ...

  9. 《重构-改善既有代码的设计》学习笔记---Replace Temp with Query(以查询取代临时变量)

    临时变量的问题在于: 它们是暂时的,而且只能在所属函数内使用.由于临时变量只在所属函数内可见,所以,如果很多地方都在用这个临时变量,就会驱使你写出更长的函数.如果把临时变量替换为一个查询,那么其他函数 ...

  10. Ubuntn16.04.3安装Hadoop3.0+scale2.12+spark2.2

    Ubuntn16.04.3安装Hadoop3.0+scale2.12+spark2.2 对比参照此博文.bovenson 前言:因为安装的Hadoop.Scale是基于JAVA的应用程序,所以必须先安 ...