Spring MVC - RequestMethod" Post"不支持

时间:2017-07-11 13:19:27

标签: html spring angular http

在我写的HTML中

<form method="POST" enctype="multipart/form-data" action="http://localhost:8080/uploadXML"> 

    <input type="file" id="importFile" /> <br>
    <button type="submit" class="buttonUpload" >Upload</button>
</form>

我的控制器看起来像:

@RestController
public class Controller {



@RequestMapping(value="/uploadXML", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload( 
        @RequestParam("file") MultipartFile file){
        String name = "test11";
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = 
                    new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

按下提交按钮时,我总是收到错误代码405: &#34;出现意外错误(type = Method Not Allowed,status = 405)。 请求方法&#39; POST&#39;不支持&#34;。 This is what my package explorer looks like

我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<groupId>springboot-angularjs</groupId>
<artifactId>springboot-angularjs</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>angularjs</artifactId>
        <version>1.4.9</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.6</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.5</version>
 <exclusions>
<exclusion>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bcmail-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>bouncycastle</groupId>
                <artifactId>bcmail-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bctsp-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>bouncycastle</groupId>
                <artifactId>bcprov-jdk14</artifactId>
 </exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
 <exclusions>
<exclusion>
            <groupId>org.bouncycastle</groupId>
                <artifactId>bcmail-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>bouncycastle</groupId>
                <artifactId>bcmail-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bctsp-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>bouncycastle</groupId>
                <artifactId>bcprov-jdk14</artifactId>
     </exclusion>
    </exclusions>
     </dependency>


   </dependencies>
    </project>

有人可以看到我的错误,并可以向我解释文件传输如何工作?非常感谢你!

1 个答案:

答案 0 :(得分:0)

尝试在url中添加根上下文,即动态项目名称,如下所示

http://localhost:8080/XXXXX/uploadXML

XXXXX将是根上下文名称。