使用SSM框架上传图片

article/2025/9/20 8:09:06

使用SSM框架上传图片

为了大家方便对照,我上传源码到网盘,有兴趣的自取.
ps:其中有一个存储数据的网页,我没删除,可以忽略
链接:https://pan.baidu.com/s/1u24E8mUs4K-raoQgx-ae2A
提取码:java

建数据表

CREATE DATABASE my_resource;USE my_resource;CREATE TABLE `image`(`id` INT AUTO_INCREMENT ,`title` VARCHAR(255) ,`url` VARCHAR(255) ,`type` VARCHAR(20),PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

通过idea开始一个maven工程

导入相应坐标到pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssm2</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>ssm2 Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><!--spring相关--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency><!--servlet和jsp--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version></dependency><!--mybatis相关--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.18</version><scope>provided</scope></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.3</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources><finalName>ssm2</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

在main生成如下包路径

001

实体类

package com.hoymit.domain;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Images {private Integer id;private String title;private String url;private String type;
}

编写mapper接口(在resource目录下创建一个和在main相同的包路径下面)

package com.hoymit.mapper;import com.hoymit.domain.Images;public interface ImagesMapper {void saveAll(Images images);
}

编写相应的xml实现

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hoymit.mapper.ImagesMapper"><insert id="saveAll" parameterType="images">//在xml中已经配置别名insert into image values(#{id},#{title},#{url},#{type})</insert></mapper>

编写service接口

package com.hoymit.service;import com.hoymit.domain.Images;public interface ImagesService {void saveAll(Images images);
}

编写相应实现

package com.hoymit.service.impl;import com.hoymit.domain.Images;
import com.hoymit.mapper.ImagesMapper;
import com.hoymit.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service("imagesService")
public class ImagesServiceImpl implements ImagesService {@Autowiredprivate ImagesMapper imageMapper;@Overridepublic void saveAll(Images images) {imageMapper.saveAll(images);}
}

配置文件

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--组件扫描 扫描service和mapper--><context:component-scan base-package="com.hoymit"><!--排除controller的扫描--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter></context:component-scan><!--加载propeties文件--><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><!--配置数据源信息--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--配置sessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!--加载mybatis核心文件--><property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property></bean><!--扫描mapper所在的包 为mapper创建实现类--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.hoymit.mapper"></property></bean><!--声明式事务控制--><!--平台事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务增强--><tx:advice id="txAdvice"><tx:attributes><tx:method name="*"/></tx:attributes></tx:advice><!--事务的aop织入--><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hoymit.service.impl.*.*(..))"></aop:advisor></aop:config></beans>

spring-mvc.xml

<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--组件扫描  主要扫描controller--><context:component-scan base-package="com.hoymit.controller"></context:component-scan><!--配置mvc注解驱动--><mvc:annotation-driven></mvc:annotation-driven>
<!--    返回json快捷写法--><!--内部资源视图解析器--><bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><!--开发静态资源访问权限--><mvc:default-servlet-handler></mvc:default-servlet-handler><!--配置文件上传解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="UTF-8"/><property name="maxUploadSize" value="500000"/></bean>
</beans>

sqlMapConfig-spring.xml(一般这里只剩下别名了,其他bean都通过spring容器进行注入)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--定义别名--><typeAliases><!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>--><package name="com.hoymit.domain"></package></typeAliases></configuration>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/my_resource?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

在webapp下创建相应的jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="${pageContext.request.contextPath}/load/imageTest" method="post" enctype="multipart/form-data">图片文件<input type="file" name="uploadFile" multiple><br/>文章标题:<input type="text" name="title"><br>图片位置:<input type="text" name="type"><br><input type="submit" value="提交"></form>
</body>
</html>

最后编写controller层

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;@Controller
@RequestMapping("/load")
public class ImagesController {@Autowiredprivate ImagesService imagesService;@RequestMapping(value = "/imageTest" ,produces = "text/html;charset=UTF-8")@ResponseBodypublic String  loadImageTest(MultipartFile[] uploadFile, Images image){for (MultipartFile multipartFile : uploadFile) {String originalFilename = multipartFile.getOriginalFilename();//原文件名字String newFilename= UUID.randomUUID()+"_"+originalFilename;	//使用UUID避免重名File filePath=new File("D:\\load_load\\"+newFilename);try {multipartFile.transferTo(filePath);} catch (IOException e) {e.printStackTrace();return "上传失败";}String url = "http://localhost:8080/load_load/"+newFilename;image.setUrl(url);System.out.println(image.getTitle()+"----------------------------");imagesService.saveAll(image);}return "binggo";}
}

此时我把D盘下的load_load作为服务器存放图片资源的文件夹,通过访问http://localhost:8080/load_load/生成的文件名即可访问图片

添加外部加载资源

002

效果图

003


http://chatgpt.dhexx.cn/article/BneMsuNr.shtml

相关文章

SSM框架简单介绍

一. SSM框架简介及特征 1.SpringMVC Spring MVC属于SpringFrameWork的后续产品&#xff0c;已经融合在Spring Web Flow 里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构&#xff0c;从而在使用Spring进行WEB开发时&#xff0c;可…

SSM框架整合详细教程

目录 1. 什么是SSM&#xff1f; 2. SSM整合的时候容器之间如何访问 3. SSM下面的开发步骤 4. SSM整合时候时容易混乱的知识点 1. 什么是SSM&#xff1f; SSM是对三个框架名字的简写&#xff0c;其中第一个S指的是SpringMVC,第二个S指的是Spring&#xff0c;第三个M指的是M…

SSM框架理解

一、作用&#xff1a; 1、 SSM是spingspringMVCmybatis集成的框架。是标准的MVC模式&#xff0c;将整个系统划分为表现层&#xff0c;controller层&#xff0c;service层&#xff0c;DAO层四层。 2、使用spring MVC负责请求的转发和视图管理&#xff0c;spring实现业务对象管理…

SSM框架初探

SSM 框架初探 SSM框架简介对框架的理解为什么使用 SSM 框架 SpringSpringMVCMybatis SSM框架简介 SSM框架一种以java语言作为后端搭建基本语言的应用系统搭建框架。是继SSH(strutsspringhibernate)之后&#xff0c;目前较为主流的Java EE企业级框架&#xff0c;适用于搭建各种大…

SSM框架整合

今天来整合一下SSM三大框架~~ 1、创建一个maven项目 比较简单就不赘述了&#xff0c;创建的时候选择webapp骨架。 用骨架创建的项目&#xff0c;在创建完之后要更新一下web.xml 模板目录&#xff1a;“你的Tomcat安装目录\webapps\ROOT\WEB-INF\web.xml” 2、项目整体结构 按…

SSM框架原理

SSM框架是spring MVC &#xff0c;spring和mybatis框架的整合&#xff0c;是标准的MVC模式&#xff0c;将整个系统划分为表现层&#xff0c;controller层&#xff0c;service层&#xff0c;DAO层四层 使用spring MVC负责请求的转发和视图管理 spring实现业务对象管理&#xff…

SSM框架总结

一&#xff1a;什么是SSM框架&#xff1f; SSM框架是Spring、SpringMVC和MyBatis框架的总结&#xff0c;是比较标准的MVC模式。 标注的SSM框架有四层&#xff1a;dao&#xff08;mapper&#xff09;层、service层、controller层、domain(entity)层。 使用Spring实现业务对象…

SSM框架详解

SSM框架详解 写在前面&#xff1a;当初整理SSM原理时&#xff0c;参考了网上一些前辈的文章&#xff0c;时间久远已经忘记来源&#xff0c;所以文中原理部分如有侵权请联系我删除。 基于SSM框架的仿天猫商城网站电商后台管理系统 本文视频讲解 文章目录 SSM框架详解 一、项…

java ssm框架论文,ssm框架理解

文章简介: SSM框架集简介 spring框架IOC的理解 mybatis框架sqlSessionFactory理解 Tomcat的理解 图解SSM SSM框架常用注解 1.SSM框架集简介 SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的…

SSM框架详细讲解

SSM框架 文章目录 SSM框架&#xff08;白痴都看完都会&#xff09;介绍SSM框架<原理>一、什么是SSM框架&#xff1f; 1.Spring2.Spring MVC3.Mybatis &#xff08;核心是SqlSession&#xff09;二、代码实战 1.创建配置工程2.代码实战&#xff08;查询记录数&#xff0…

SSM三大框架超详细总结(适合你重新回顾)

目录 1.1 概念 1.2 Mybatis优点 1.3 Mybatis架构 1.4 底层原理 1.5 Mybatis缓存 1.6 常见面试题 2.1 概念 2.2 Spring优点 2.3 Spring架构 2.4 控制反转&#xff08;IOC&#xff09; 2.5 DI依赖注入 2.6 底层原理(常见面试题) 8、如何用基于 Java 配置的方式配置 Spring&#…

SSM框架整合思想及步骤

前言 SSM框架即是将SpringMVC框架、Spring框架、MyBatis框架整合使用。以简化在web开发中繁琐、重复的操作&#xff0c;让开发人员的精力专注于业务处理的开发上。 一、SSM框架的思想 ssm框架根据SpringMVC、Spring、MyBatis三者各自的特性及应用场景对其操作的的业务进行了分…

SSM框架简介

一、Java SSM框架的概念 Java SSM框架即指SpringSpringMVCMyBatis的简称&#xff0c;框架集由Spring、MyBatis两个开源框架整合而成&#xff08;SpringMVC是Spring中的部分内容&#xff09;,常作为数据源较简单的web项目的框架。 相比于之前的SSH&#xff08;SpringStrutsHibe…

SSM框架讲解(史上最详细的文章)

SSM框架&#xff08;白痴都看完都会&#xff09; 文章目录 SSM框架&#xff08;白痴都看完都会&#xff09;介绍SSM框架<原理>一、什么是SSM框架&#xff1f;1.Spring2.Spring MVC3.Mybatis &#xff08;核心是SqlSession&#xff09; 二、代码实战1.创建配置工程2.代码实…

MySQL安装教程(超详细教程)

MySQL详细安装步骤 - windows(MySQL Installer for Windows)附链接 1.下载所需要的版本(可自选&#xff0c;5.7.29为稳定版本)下载链接&#xff0c;运行程序 2.个人学习使用server only&#xff0c;如果不确定需求&#xff0c;则选择full&#xff0c;全部安装&#xff1b;Next …

Mysql详细安装教程

&#x1f973;&#x1f973;&#x1f973; 茫茫人海千千万万&#xff0c;感谢这一刻你看到了我的文章&#xff0c;感谢观赏 &#x1f60f;。 &#x1f449; 作者简介&#xff1a;最爱吃鱼罐头。(抱歉&#xff0c;我真的吃鱼罐头&#x1f92b;) &#x1f97a; 本人不才&#xff…

【MySQL】免安装版MySQL安装教程

前言 近日&#xff0c;重新安装了一下本地的数据库&#xff0c;参考了很多博客才将MySQL给安装好&#xff0c;为了方便以后安装&#xff0c;便结合了网上博客的安装方法以及自己的一些经验写下这篇博客&#xff0c;也希望能给你们带来帮助。 一、MySQL是什么&#xff1f; My…

MySQL 8.0.32安装教程

目前&#xff0c;主流关系型数据库管理系统&#xff1a;Oracle&#xff08;收费的数据库&#xff0c;价钱较昂贵&#xff0c;但是除了提供软件还提供相应服务&#xff09;、MySQL&#xff08;中小型数据库&#xff0c;开源的社区版和收费版&#xff09;、Microsoft SQL server&…

mysql安装教程5.1_mysql 5.1安装教程详解

1. 下载的mysql安装文件&#xff0c;运行 mysql-5.1.62&#xff0c;出现如下界面&#xff1b; 2. 向导启动&#xff0c;按Next继续&#xff0c;有三个选项&#xff0c;我们选择用户自定义“Custom”&#xff0c;有更多的选项&#xff0c;也方便熟悉整个安装过程&#xff1b;…

MySQL安装教程——安装篇

目录 1.双击下载的mysql文件(.msi结尾的) 2. 选中“ I accept the license terms”单击Next 3.选择安装类型&#xff0c;我这里选择“Server only”&#xff0c;仅安装MySQL数据库服务器&#xff0c;然后点击Next 4. 执行MySQL的下载和安装&#xff0c;单击 Execute 5. MySQL安…