配置资源(jar包)

将前端页面整理好:

写核心的几个配置文件(applicationContext+wed.xml+jdbc.properties+log4j+springMVC.xml)

都是在src目录下:

applicationContext-mybatis.xml(配置和mybatis关联的文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--连接数据库-->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver1}"/>
<property name="url" value="${url1}"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> <!--获得sqlsession-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"/>
</bean> <!--扫描mapper文件-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"/>
<property name="basePackage" value="com.bjsxt.mapper"/>
</bean>
</beans>

applicationContext-service.xml(配置service层):

<!--扫描业务层注解-->
<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>

如果需要进行事务操作:

<!--配置声明事务-->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean> <!--扫描事务注解-->
<tx:annotation-driven></tx:annotation-driven>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<!--处理jsp都可以使用-->
<url-pattern>/</url-pattern>
</servlet-mapping> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

MVC分层:

控制层(controller):

由于是为了测试上传下载的功能,我并没有将功能对应的控制层分开写,就写了一个文件,用注解去访问

package com.bjsxt.controller;

import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.font.MultipleMaster;
import java.io.*;
import java.util.List;
import java.util.UUID; @Controller
public class MyCon { @Autowired
StudentService ss; //下载图片操作
@RequestMapping("download")
public void download(String filename, String filetype, HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取服务器的图片路径
String realPath = req.getServletContext().getRealPath("/upload"); //获取图片的名称和类型 String filename,String filetype File file=new File(realPath+"/"+filename); //将文件写入
InputStream inputStream=new FileInputStream(file); //设置属性下载到本地
//1.设置长度
resp.setContentLength((int)file.length()); //2.设置类型
resp.setContentType(filetype); //3.设置响应头
resp.setHeader("Content-Disposition","attachment;filename="+filename); //将读取的文件写入本地
OutputStream outputStream = resp.getOutputStream();
IOUtils.copy(inputStream,outputStream); //关闭流
outputStream.close();
inputStream.close(); } @RequestMapping("filee")
public String filee(String uname, String pwd, MultipartFile fil) throws IOException {
System.out.println(uname+":"+pwd);
System.out.println(fil.getName()+"---"+fil.getSize()+"---"+fil.getContentType()+"---"+fil.getOriginalFilename());
fil.transferTo(new File("F:/img/"+fil.getOriginalFilename()));
return "redirect:/index.jsp";
} @RequestMapping("insertStu")
public String insertStu(String name,int age,Double score, MultipartFile filename, HttpServletRequest req) throws IOException {
/*System.out.println(uname+":"+pwd);
System.out.println(fil.getName()+"---"+fil.getSize()+"---"+fil.getContentType()+"---"+fil.getOriginalFilename());*/ /* if (fil.getSize()>2*1024){
req.setAttribute("error","最大的上传文件是2kb");
return "forward:/zhuce.jsp";
}*/
String realPath = req.getServletContext().getRealPath("/upload");
/*为了防止文件名相同,覆盖原文件*/
String uuid = UUID.randomUUID().toString();
//截取图片的后缀名
String filname = filename.getOriginalFilename().substring(filename.getOriginalFilename().lastIndexOf("."));
String fname=uuid+filname;
File file=new File(realPath);
if (!file.exists()){
file.mkdirs();
}
//文件上传完毕
filename.transferTo(new File(file,fname)); Student student=new Student();
student.setAge(age);
student.setFilename(fname);
student.setName(name);
student.setScore(score);
student.setFiletype(filename.getContentType());
//调用业务层
int addstu = ss.addstu(student);
if (addstu>0){
//插入成功
return "redirect:/findall";
}else {
//插入失败
req.setAttribute("error","插入失败");
return "forward:/save.jsp"; } } @RequestMapping("findall")
public String findall(HttpServletRequest req){
List<Student> students = ss.finall();
req.setAttribute("students",students);
return "forward:/stuList.jsp";
} }

Mapper接口:

package com.bjsxt.mapper;

import com.bjsxt.pojo.Student;

import java.util.List;

public interface StudentMapper {
int insetstu(Student student); List<Student> selAll();
}

Mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<insert id="insetstu" parameterType="student" >
insert into student values(default,#{name},#{age},#{score},#{filename},#{filetype})
</insert> <select id="selAll" resultType="Student">
select * from student
</select>
</mapper>

Service接口:

package com.bjsxt.service;

import com.bjsxt.pojo.Student;

import java.util.List;

public interface StudentService {
public int addstu(Student student); public List<Student> finall();
}

Service实现类

package com.bjsxt.service.impl;

import com.bjsxt.mapper.StudentMapper;
import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service("ssi")
public class StudentServiceImpl implements StudentService { @Autowired
StudentMapper studentMapper; @Override
public int addstu(Student student) {
int insetstu = studentMapper.insetstu(student);
return insetstu;
} @Override
public List<Student> finall() {
List<Student> students = studentMapper.selAll();
return students;
}
}

pojo实体类:

package com.bjsxt.pojo;

import java.io.Serializable;

public class Student implements Serializable {
private int id;
private String name;
private int age;
private double score;
private String filename;
private String filetype; public Student(int id, String name, int age, double score, String filename, String filetype) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
this.filename = filename;
this.filetype = filetype;
} public Student() {
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getScore() {
return score;
} public void setScore(double score) {
this.score = score;
} public String getFilename() {
return filename;
} public void setFilename(String filename) {
this.filename = filename;
} public String getFiletype() {
return filetype;
} public void setFiletype(String filetype) {
this.filetype = filetype;
} @Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
", filename='" + filename + '\'' +
", filetype='" + filetype + '\'' +
'}';
}
}

实现效果:

SpringMVC实现上传下载功能的更多相关文章

  1. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  2. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  3. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  4. 【转】Android 服务器之SFTP服务器上传下载功能

    原文网址:http://blog.csdn.net/tanghua0809/article/details/47056327 本文主要是讲解Android服务器之SFTP服务器的上传下载功能,也是对之 ...

  5. 【转】Android 服务器之SFTP服务器上传下载功能 -- 不错

    原文网址:http://blog.csdn.net/tanghua0809/article/details/47056327 本文主要是讲解Android服务器之SFTP服务器的上传下载功能,也是对之 ...

  6. JavaWeb实现文件上传下载功能实例解析 (好用)

    转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...

  7. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

  8. SpringMVC 文件上传下载

    目录 文件上传 MultipartFile对象 文件下载 上传下载示例 pom.xml增加 创建uploadForm.jsp 创建uploadForm2.jsp 创建userInfo.jsp spri ...

  9. java中文上传下载功能实现(自己测试项目)

    1.新建maven项目打war包 2.搭建springMVC框架 web.xml文件配置 <?xml version="1.0" encoding="UTF-8&q ...

随机推荐

  1. vue 单页面应用 app自适应方案

    本文是使用淘宝的方案进行布局开发的,遇到的问题是会对app内使用的第三方插件,当页面进行缩放后,比如高德地图中的文字会显得过小,我使用的方法就是手动的动每一个尺寸进行手动的px 到 rem的替换,而不 ...

  2. 基于docker搭建Jenkins+Gitlab+Harbor+Rancher架构实现CI/CD操作

    一.各个组件的功能描述: Docker 是一个开源的应用容器引擎. Jenkis 是一个开源自动化服务器. (1).负责监控gitlab代码.gitlab中配置文件的变动: (2).负责执行镜像文件的 ...

  3. 学Linux到底学什么?

    前言 我们常常听到很多人说要学学Linux或者被人告知说应该学学Linux,那么学Linux到底要学什么? 为什么要学Linux 在回答学什么之前,我们先看看为什么要学.首先我们需要认识到的是,很多服 ...

  4. windows 2008 服务器优化:停powershell,卸载不相干软件,开启防火墙

    windows 2008 作为 全录 的服务器,经常cpu达到100%,查看是powershell.exe占cpu有98%.影响 全录 软件进行电话录音.所以想禁止powershell.exe程序启动 ...

  5. CSS(7)--- 通俗讲解清除浮动

    CSS(7)--- 通俗讲解清除浮动 上一篇讲了CSS浮动 博客地址:CSS(6)---通俗讲解浮动(float) 一.理解清除浮动 1.为什么要清除浮动 我们前面说过,浮动本质是用来做一些文字混排效 ...

  6. firefox浏览器中使用vux的x-input报错TypeError: _this3.$refs.input.scrollIntoViewIfNeeded is not a function

    最近做公众号项目,想着统一风格,所以决定使用vux. 在调试时发现,只要鼠标点击x-input输入框,就会报错 TypeError: _this3.$refs.input.scrollIntoView ...

  7. Viterbi(维特比)算法在CRF(条件随机场)中是如何起作用的?

    之前我们介绍过BERT+CRF来进行命名实体识别,并对其中的BERT和CRF的概念和作用做了相关的介绍,然对于CRF中的最优的标签序列的计算原理,我们只提到了维特比算法,并没有做进一步的解释,本文将对 ...

  8. Openlayers 实现轨迹播放/暂停/重新播放/从点击处播放/提速/减速

    说明: 我的需求是需要实现轨迹播放/暂停/重新播放/从点击处播放,因此封装了一个类 解决方案: 1.初始化:主要是处理一下图层以及数据,通过插值构造一个全局数组 /** * @description ...

  9. 基于 HTML5 WebGL 和 VR 技术的 3D 机房数据中心可视化

    前言 在 3D 机房数据中心可视化应用中,随着视频监控联网系统的不断普及和发展, 网络摄像机更多的应用于监控系统中,尤其是高清时代的来临,更加快了网络摄像机的发展和应用. 在监控摄像机数量的不断庞大的 ...

  10. python3 之 面向对象(类)、继承、派生和多态

    类提供了一种 组合数据和功能 的方法.创建一个新类意味着:创建一个新 类型  的对象,从而允许创建一个该类型的新 实例. 每个类的实例可以拥有: 保存自己状态的属性. 一个类的实例也可以有改变自己状态 ...