小理科 2019-11-14 18:02 采纳率: 75%
浏览 856

springMVC上传文件报错404

学习springMVC文件上传的时候,选择了文件提交以后报错404
jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
    <h3>文件上传</h3>


    <form action="/User/fileUpload" method="post" enctype="multipart/form-data">
        <input type="file" name="upload"><br/>
        <input type="submit" value="上传">
    </form>


</body>
</html>

spring-MVC配置如下

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="com.ryn"/>

<!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置解析器-->
        <property name="suffix" value=".jsp"/>
        <!--配置目录-->
        <property name="prefix" value="/WEB-INF/pages/"/>
    </bean>

    <!--配置静态资源-->
    <mvc:resources location="/upload/" mapping="/upload/**" />
    <mvc:resources mapping="/js/**" location="js/"/>

    <!--配置文件解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760"/>
    </bean>

    <!--开启springMVC框架注解的支持-->
    <mvc:annotation-driven/>

</beans>

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--配置前端控制器-->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

uploadController

/**
     * 文件上传
     * @return
     */
    @RequestMapping("/fileUpload")
    public String fileUpload(HttpServletRequest request, @RequestParam("upload") MultipartFile upload) throws Exception{
        System.out.println("文件上传");
        //指定上传位置
        String path = request.getSession().getServletContext().getRealPath("/upload/");
        //判断文件夹是否存在
        File file = new File(path);
        if (!file.exists()){
            //若不存在,则创建文件夹
            file.mkdirs();
        }
        String filename = upload.getOriginalFilename();
        //使用write方法上传文件
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        filename = uuid + "_" + filename;
        upload.transferTo(new File(path,filename));
        return "success";
    }

报错信息如下
图片说明
求各位大佬帮我看看问题到底出在哪了!!!弄了半个小时不知道怎么回事

  • 写回答

5条回答 默认 最新

  • zyydomain 2019-11-14 18:13
    关注

    controller上加注解/uesr 没

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符