SpringMVC上传文件

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/12/20
  Time: 23:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传 </title>
</head>
<body>
<form action="/user/fileupload1" method="post" enctype="multipart/form-data">
    文件上传:<input type="file" name="upload"/><br/>
    <input type="submit" value="传统提交"/><br/>
</form>
<form action="/user/fileupload2" method="post" enctype="multipart/form-data">
    文件上传:<input type="file" name="upload1"/><br/>
    <input type="submit" value="SpringMVC提交"/><br/>
</form>

</body>
</html>

package com.itheima.controller;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/user")
public class UserContreller {

    @RequestMapping(value = "/fileupload1")
    public String fileupload1(HttpServletRequest request) throws Exception {
        System.out.println("文件上传111");
        //使用fileupload组件完成文件上传
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        System.out.println(path);
        //判断路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        //解析request对象,获取上传文件项
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> fileItems = upload.parseRequest(request);
        for (FileItem item : fileItems) {
            //判断当前的 item是否是一个上传文件的文件箱
            if (item.isFormField()) {
                //普通表单项
            } else {
                //上传文件项
                //获取上传文件名称
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String filename = uuid + item.getName();
//                UUID uuid = new UUID();
                //完成文件上传
                item.write(new File(path, filename));
                // 删除临时文件
                item.delete();

            }
        }
        return "success";
    }

    @RequestMapping(value = "/fileupload2")
    public String fileupload2(HttpServletRequest request, MultipartFile upload1) throws Exception {
        System.out.println("文件上传222");
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String name = upload1.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String filename = uuid + name;
        upload1.transferTo(new File(path, filename));
        return "success";
    }


}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
    <!--    配置扫描-->
    <context:component-scan base-package="com.itheima"/>
    <!--    视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 文件解析器对象-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"/>
    </bean>
    <!-- 前段控制器,哪些静态资源不拦截 !!! location 可以没有** 但是mapping 一定要有** springmvc注解-->
    <mvc:resources mapping="/js/**" location="/js/**"/>
    <mvc:resources mapping="/css/**" location="/css/**"/>
    <mvc:resources mapping="/images/**" location="/images/**"/>
    <mvc:annotation-driven/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值