javaee SpringMVC文件上传 项目结构

news2024/10/6 10:36:37

引入依赖

<?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>TestSpringMVC5</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>TestSpringMVC5 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>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>


    <!-- 导入SpringMvc 需要的jar包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- 配置servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>


    <!-- jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- 1.2以下的版本 需要加standard架包 -->
    <!--
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    -->

    <!--配置jsp的依赖 -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>

    <!-- 添加 json 转换包 springmvc支持的可以自己调用 -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.2.4</version>
    </dependency>

    <!-- json依赖 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

    <!-- 文件上传需要的jar包-->
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>TestSpringMVC5</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>

修改配置文件

<?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:p="http://www.springframework.org/schema/p"
       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">
      <!-- 1. 配置  需要扫描的控制层在哪个包  -->
    <context:component-scan base-package="com.test"></context:component-scan>

    <!-- 2 配置 视图解析器 中的 前缀和后缀  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置前缀  -->
        <property name="prefix" value="/WEB-INF/"/>
        <!-- 设置后缀 -->

        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 开启注解驱动
      注册了两个数据转换的注解@NumberFormatannotation支持,@DateTimeFormat
      json相关的。。
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 放行静态资源
        location:静态资源的真实路径
        mapping:访问路径(可以自定义)
        放行static文件夹下的所有的文件
     -->
    <mvc:resources mapping="/static/**" location="/static/" />
<!--    <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />-->



    <!-- multipartResolver配置 id必须为multipartResolver
MultipartResolver 用于处理文件上传,当收到请求时 DispatcherServlet 的 checkMultipart() 方法会调用 MultipartResolver 的 isMultipart() 方法判断请求中是否包含文件。如果请求数据中包含文件,则调用 MultipartResolver 的 resolveMultipart() 方法对请求的数据进行解析,然后将文件数据解析成 MultipartFile 并封装在 MultipartHttpServletRequest (继承了 HttpServletRequest) 对象中,最后传递给 Controller
 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
          p:defaultEncoding="UTF-8"
          p:maxUploadSize="5242880"
          p:uploadTempDir="file:/e:/file/temp"/>


</beans>

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>

<form action="users/uploadFile" method="post" enctype="multipart/form-data">

    上传图片:<input type="file" name="myfile" />

    <input type="submit" name="sub" value="提交" />

</form>

</body>
</html>

java

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/users")
public class UsersController {

    @RequestMapping("/uploadFile")
    public String uploadFile(@RequestParam("myfile") MultipartFile myfile, HttpServletRequest request)
    {
           String filename=  myfile.getOriginalFilename();

           System.out.println(filename);

           //将文件从临时存储的位置转移到指定的位置(永久存储)
        try {
            //myfile.transferTo(new File("e:/file/upload/"+filename));

            //myfile.transferTo(new File("D:\\idea_workspace\\SpringMVC\\TestSpringMVC5\\target\\TestSpringMVC5\\static\\images"));

            //获取项目上传到的服务器端地址

            String path=request.getSession().getServletContext().getRealPath("/static/images");

            System.out.println(path);

            myfile.transferTo(new File(path+"/"+filename));

        } catch (IOException e) {
            e.printStackTrace();
        }


        return "success";
    }


}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <!-- 注册过滤器  设置编码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</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>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 加入前端控制器 DispatcherServlet -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 设置 DispatcherServlet 的参数 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>

    <!-- 1 启动服务器的时候就加载  -->
    <!-- 0 使用的时候再加载 -->
    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String baseurl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
    pageContext.setAttribute("baseurl",baseurl);
%>
<html>
<base href="${baseurl}" />
<head>
    <title>Title</title>
</head>
<body>
success
<img src="${baseurl}/static/images/3.jpg" />
</body>
</html>

项目结构

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1066321.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

c++模板库容器list vector map set操作和性能对比

文章目录 listvectormapset性能比较总结 list 列表&#xff08;list&#xff09;是C STL中的一种容器类型&#xff0c;它是一个双向链表&#xff0c;可以在任意位置高效地添加、删除、移动元素。 以下是一些常用的列表操作&#xff1a; 创建列表 #include <list> std…

21.6 CSS 弹性布局

1. 弹性盒子 CSS弹性盒子(Flexbox)是一种布局模型, 用于创建灵活的, 自适应的网页布局. 它的目的是在不同屏幕尺寸和设备上实现一致的布局效果.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列, 对齐和分配空白空间.弹性容器通过设置display…

c++视觉图像线性混合

图像线性混合 使用 cv::addWeighted() 函数对两幅图像进行线性混合。alpha 和 beta 是两幅图像的权重&#xff0c;它们之和应该等于1。gamma 是一个可选的增益&#xff0c;这里设置为0。 你可以通过调整 alpha 的值来改变混合比例。如果 alpha0.5&#xff0c;则两幅图像等权重…

扬尘在线监测是什么?如何实现?

扬尘在线监测技术是一种针对扬尘污染问题的环境监测技术&#xff0c;它通过实时监测和数据分析&#xff0c;为管理者提供准确及时的信息&#xff0c;以便他们可以采取有效的控制措施来减少扬尘污染对空气质量和人们健康的影响。 扬尘在线监测系统通常由传感器、数据采集仪器、…

SpringBoot项目:Cannot find declaration to go to

SpringBoot项目get,set方法总报Cannot find declaration to go to 搜了很多答案&#xff0c;没解决 后来仔细一想&#xff0c;原来是我的idea软件重装了&#xff0c;lombok插件没重新安装导致。 安装步骤&#xff1a; 1、下载地址&#xff1a;https://plugins.jetbrains.com…

12P2532X152 KJ3222X1-BA1 CE4003S2B1 EMERSON DELTAV

12P2532X152 KJ3222X1-BA1 CE4003S2B1 EMERSON DELTAV 除了标准的实时计算、通信和控制&#xff0c;边缘设备和关键网络应用的fog通常执行人工智能(AI)、虚拟现实(VR)和增强现实(AR)解决方案。 目前&#xff0c;制药商和医疗保健机构对它们的需求快速增长&#xff0c;因为它们…

el-table进阶(每条数据分行或合并)

最麻烦的还是css样式&#xff0c;表格样式自己调吧 <!-- ——————————————————————————————————根据数据拓展表格—————————————————————————————————— --> <div style"display: flex"&…

SAP从入门到放弃系列之QM检验方法(Inspection Method)

概述 检验方法描述了如何对检验特征执行检验。 QS21-创建主检验特征时&#xff0c;可以对主检验特征可以分配多种检验方法。关于创建主检验特征详见&#xff1a;SAP从入门到放弃系列之QM主检验特征 当任务清单中包含主检验特征时&#xff0c;将为任务清单中的特征选择特定的…

vue3 -- 封装 Turf.js地图常用方法

Turf.js中文网 地理空间分析库,处理各种地图算法 文档地址 安装 Turf 库 npm install @turf/turf创建src/hooks/useTurf.ts 文件1:获取线中心点 效果: 代码: useTurf.ts import * as turf from @turf/turf// 获取线中心点 export class CenterPointOfLine {

Linux系列讲解 —— 【fsck】检查并修复Linux文件系统

当文件系统出现损坏时&#xff0c;例如文件无法查看&#xff0c;删除等&#xff0c;可以使用 fsck&#xff08;File System Consistency Check&#xff09;进行修复。但是需要注意fsck在修复时&#xff0c;如果检查出某个文件有问题&#xff0c;可能会向用户请求删除。所以&…

分层强化学习 综述论文阅读 Hierarchical Reinforcement Learning: A Comprehensive Survey

分层强化学习 综述论文阅读 Hierarchical Reinforcement Learning: A Comprehensive Survey 摘要一、介绍二、基础知识回顾2.1 强化学习2.2 分层强化学习2.2.1 子任务符号2.2.2 基于半马尔可夫决策过程的HRL符号 2.3 通用项定义 三、分层强化学习方法3.1 学习分层策略 (LHP)3.1…

赴日IT 35岁以上程序员能申请日本技术人文签证吗?

我们都知道&#xff0c;要想去日本工作&#xff0c;必须要办理签证&#xff0c;日本人文技术国际业务签证就是一个非常好的签证种类。那么办理此类签证需要满足哪些要求呢&#xff1f; 年龄上其实比较推荐的是25-35岁这个年龄阶段&#xff0c;因为这个年龄段通常在日语能力和工…

抖音账号矩阵系统开发源码----技术研发

一、技术自研框架开发背景&#xff1a; 抖音账号矩阵系统是一种基于数据分析和管理的全新平台&#xff0c;能够帮助用户更好地管理、扩展和营销抖音账号。 抖音账号矩阵系统开发源码 部分源码分享&#xff1a; ic function indexAction() { //面包屑 $breadc…

elasticsearch内存占用详细分析

内存占用 ES的JVM heap按使用场景分为可GC部分和常驻部分。 可GC部分内存会随着GC操作而被回收&#xff1b; 常驻部分不会被GC&#xff0c;通常使用LRU策略来进行淘汰&#xff1b; 内存占用情况如下图&#xff1a; common space 包括了indexing buffer和其他ES运行需要的clas…

添加/查看/清空购物车 -----苍穹外卖day7

1.添加购物车 产品原型 接口设计 新增类使用post 需求分析 数据库查询过程中设计了冗余字段&#xff0c;意义在于提高查询速度&#xff0c;不用和菜品表中去连接查询&#xff0c;直接查询购物车表。但是冗余字段必须相对稳定不能经常变化 代码开发 在使用DTO与实体类交接的…

国科大体系结构习题 | 第三章 二进制与逻辑电路

第三章 Q1: A1:(1) 原码&#xff1a; [ − ( 2 63 − 1 &#xff0c; 2 63 − 1 ] [-(2^{63}-1&#xff0c;2^{63}-1] [−(263−1&#xff0c;263−1] 补码&#xff1a; [ − ( 2 63 &#xff0c; 2 63 − 1 ] [-(2^{63}&#xff0c;2^{63}-1] [−(263&#xff0c;263−1] …

flex 布局:元素对齐

前言 略 使用flex的align-items属性控制元素对齐 上下对齐并铺满 <view class"more">展开更多<text class"iconfont20231007 icon-zhankai"></text></view>.more {display: flex;flex-direction: row;color: #636363;justify-co…

12P4375X042-233C KJ2005X1-BA1 CE3007 EMERSON servo controller

12P4375X042-233C KJ2005X1-BA1 CE3007 EMERSON servo controller 我们提供三种不同类别的EDGEBoost I/O模块供选择&#xff0c;以实现最大程度的I/O定制: 数字和模拟输入/输出网络和连接边缘人工智能和存储 利用EDGEBoost I/O实现变革性技术 EBIO-2M2BK EBIO-2M2BK载板支持…

IGH码云克隆包

1.gitlab仓库比较慢 该开源包是由德国的团队维护在gitlab上的&#xff0c;但是在国内下载的比较慢&#xff0c;时常打不开。故而把这个包通过码云克隆了一遍。 地址如下&#xff1a; ​​​​​​​ethercat-igh: 这个是gitlab上比较新的仓库&#xff0c;因为从原地址一直无法…

SAP从入门到放弃系列之QM物料规范(Material Specification-物料说明)

目录 一、概述1.1 物料规范的结构1.2 物料规范的使用 二、操作2.1、物料主数据设置2.2、物料说明创建2.3 效果 一、概述 Material Specification-可以翻译为物料说明或者物料规格或物料规范&#xff0c;物料的检验相对简单的时候也可以在系统中使用物料规范&#xff0c;相对于…