基于SSM的图片上传功能

最近想用SSM开发一个图片上传的功能,找了一遍网上的资料总觉得残缺不全,今天把个人的实现方式分享一下,同时做一个记录。

一、环境配置

1.准备JAR包
链接:https://pan.baidu.com/s/1yG1mXBADS3oaLi3eZL7HRA
提取码:rxg2
第一部分JAR包
第二部分JAR包
2.环境配置
总共三个配置文件
spring_config.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"
	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">

	<context:component-scan
		base-package="com.spring.serviceimpl,com.spring.controller" />
		
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 数据库驱动JDBC -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 数据库URL -->
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false" />
		<!-- 用户名 -->
		<property name="user" value="root" />
		<!-- 密码 -->
		<property name="password" value="123456" />
		<!-- 连接池初始大小 -->
		<property name="initialPoolSize" value="10" />
		<!-- 连接池最大值 -->
		<property name="maxPoolSize" value="20" />
		<!-- 连接最大静默时间 单位为秒 -->
		<property name="maxIdleTime" value="30" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 依赖注入配置好的数据源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<tx:annotation-driven />

	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 依赖注入数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 别名的Package ,URL: org.apache.ibatis.type -->
		<property name="typeAliasesPackage" value="com.spring.entity" />
		<!-- mybatis Annotation的包 (映射类) -->
		<property name="typeHandlersPackage" value="com.spring.dao" />
		<!-- 配置mapper.xml文件扫描 -->
		<property name="mapperLocations" value="mapper/*.xml"/>
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<value>
							params=value1
						</value>
					</property>
				</bean>
			</array>
		</property>
	</bean>
	
	<!--这里拿一个模块来实例-->
	<bean id="articleDao"
		  class="org.mybatis.spring.mapper.MapperFactoryBean">
		<!-- 依赖注入配置好的Mybatis -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<!-- UserInfoDao接口 -->
		<property name="mapperInterface"
			value="com.spring.dao.ArticleDao" />
	</bean>
</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">

	
	<context:component-scan 
	base-package="com.spring.controller" />
	
	<mvc:default-servlet-handler />
	
	<mvc:annotation-driven />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- view处理类 -->
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<!-- 前缀 -->
		<property name="prefix" value="/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 配置multipartResolver文件解析器,文件上传的关键配置 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

		<!-- 设置上传文件的大小 -->
		<property name="maxUploadSize" value="52428800" />
		<!-- 设置默认编码 -->
		<property name="defaultEncoding" value="utf-8" />
	</bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>SSM</display-name>
<welcome-file-list>
	<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring_config.xml</param-value>
	</context-param>
	

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- SpringMVC配置文件路径 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring_MVC.xml</param-value>
		</init-param>
	</servlet>
		
	<servlet-mapping>
    	<servlet-name>springMVC</servlet-name>
    	<url-pattern>/</url-pattern>
  	</servlet-mapping>

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

二、代码实现

基于SSM的图片上传功能无需在dao层和service层写任何方法,只需在controller层写入

Controller层

@Controller
@RequestMapping(value = "/FileUpload")
public class FileUploadController {
	//这里的service只是一个增加信息的模块,和图片上传没有关系
	@Autowired
    private ArticleService articleService;
    
	@RequestMapping(value = "/insertArticle")
    public void addInfo(MultipartFile file, Article article, HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        //该路径是你要把图片上传到哪个文件夹的路径,我这里是上传到本项目的images文件夹中
        String path = "D:\\IdeaWorkbench\\MyProject\\SSM\\WebContent\\images";
        File f = new File(path);

        if (!f.exists()){
            f.mkdirs();
        }
        if (!file.isEmpty()){
            try {
                FileOutputStream fos = new FileOutputStream(path + "/" + fileName);
                InputStream in = file.getInputStream();
                int len = -1;
                byte[] data = new byte[1024];
                while ((len = in.read(data)) != -1){
                    fos.write(data, 0, len);
                }
                in.close();
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".gif")){
            article.setImages("/images/" + fileName);
            articleService.insertArticle(article);
        }
    }
 }   

FileUpload.jsp

<form id="infoForm" enctype="multipart/form-data" method="post"
 class="am-form tpl-form-line-form" action="${pageContext.request.contextPath}/FileUpload/insertArticle">
              <div class="am-form-group">
                   <label for="user-weibo" class="am-u-sm-3 am-form-label">封面图</label>
                   <div>
                        <input id="images" name="file" type="file">
                   </div>
              </div>

               <div class="am-form-group">
                     <div class="am-u-sm-9 am-u-sm-push-3">
                          <button onclick="formSubmit()" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success ">提交</button>
                     </div>
               </div>
</form>
<script type="text/javascript">
    function formSubmit() {
        $("#infoForm").submit();
    }
</script>    

三、功能实例

选择恶灵骑士这一张图片
在这里插入图片描述
在这里插入图片描述
可以看到我文件夹下没有恶灵骑士的图片在这里插入图片描述
点击上传之后
在这里插入图片描述
同时数据库也有记录了
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于SSM图片识别颜色系统是一种利用SSM框架(Spring+SpringMVC+MyBatis)开发的系统,用于对图片进行颜色识别和分析的应用。下面是该系统的主要功能和实现方式: 1. 图片上传:用户可以通过系统界面将图片上传到系统中。 2. 图片处理:系统会对上传图片进行处理,提取出图片中的颜色信息。 3. 颜色识别:系统会对提取出的颜色信息进行识别,判断图片中包含的主要颜色。 4. 颜色分析:系统会对识别出的颜色进行分析,统计每种颜色在图片中的占比。 5. 结果展示:系统将识别和分析的结果展示给用户,以图表或文字形式呈现。 实现该系统可以按照以下步骤进行: 1. 搭建SSM框架:使用Spring框架管理系统的各个模块,使用SpringMVC框架处理用户请求,使用MyBatis框架与数据库进行交互。 2. 图片上传:在系统界面中添加上传按钮,使用SpringMVC接收用户上传图片,并保存到服务器指定的目录。 3. 图片处理:使用Java图像处理库(如OpenCV)对上传图片进行处理,提取出颜色信息。 4. 颜色识别:根据提取出的颜色信息,使用颜色识别算法(如K-means聚类算法)对图片中的颜色进行识别。 5. 颜色分析:统计识别出的颜色在图片中的占比,可以使用统计算法(如直方图统计)进行分析。 6. 结果展示:将识别和分析的结果以图表或文字形式展示给用户,可以使用前端框架(如Bootstrap)进行页面设计和展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值