Spring Boot - метод запроса «POST» не поддерживается - PullRequest
0 голосов
/ 10 февраля 2019

Когда я выбрал файл на своем компьютере (exampleImage.png) и нажал кнопку загрузки.Я получил эту ошибку: «Произошла непредвиденная ошибка (тип = метод не разрешен, статус = 405). Метод запроса« POST »не поддерживается».Пожалуйста, помогите мне, спасибо ...

MyUploadForm.java

package com.moda.form;

import org.springframework.web.multipart.MultipartFile;

public class MyUploadForm {
 
    private String description;
 
    // Upload files.
    private MultipartFile[] fileDatas;
 
    public String getDescription() {
        return description;
    }
 
    public void setDescription(String description) {
        this.description = description;
    }
 
    public MultipartFile[] getFileDatas() {
        return fileDatas;
    }
 
    public void setFileDatas(MultipartFile[] fileDatas) {
        this.fileDatas = fileDatas;
    }
 
}

MyFileUploadController.java

@Controller
public class MyFileUploadController {
 
   @RequestMapping(value = "/")
   public String homePage() {
 
      return "index";
   }
 
   // GET: Show upload form page.
   @RequestMapping(value = "/uploadOneFile", method = RequestMethod.GET)
   public String uploadOneFileHandler(Model model) {
 
      MyUploadForm myUploadForm = new MyUploadForm();
      model.addAttribute("myUploadForm", myUploadForm);
 
      return "uploadOneFile";
   }
 
   // POST: Do Upload
   @RequestMapping(value = "/uploadOneFile", method = RequestMethod.POST)
   public String uploadOneFileHandlerPOST(HttpServletRequest request, //
         Model model, //
         @ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
 
      return this.doUpload(request, model, myUploadForm);
 
   }
 
   // GET: Show upload form page.
   @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.GET)
   public String uploadMultiFileHandler(Model model) {
 
      MyUploadForm myUploadForm = new MyUploadForm();
      model.addAttribute("myUploadForm", myUploadForm);
 
      return "uploadMultiFile";
   }
 
   // POST: Do Upload
   @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
   public String uploadMultiFileHandlerPOST(HttpServletRequest request, //
         Model model, //
         @ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
 
      return this.doUpload(request, model, myUploadForm);
 
   }
 
   private String doUpload(HttpServletRequest request, Model model, //
         MyUploadForm myUploadForm) {
 
      String description = myUploadForm.getDescription();
      System.out.println("Description: " + description);
 
      // Root Directory.
      String uploadRootPath = request.getServletContext().getRealPath("upload");
      System.out.println("uploadRootPath=" + uploadRootPath);
 
      File uploadRootDir = new File(uploadRootPath);
      // Create directory if it not exists.
      if (!uploadRootDir.exists()) {
         uploadRootDir.mkdirs();
      }
      MultipartFile[] fileDatas = myUploadForm.getFileDatas();
      //
      List<File> uploadedFiles = new ArrayList<File>();
      List<String> failedFiles = new ArrayList<String>();
 
      for (MultipartFile fileData : fileDatas) {
 
         // Client File Name
         String name = fileData.getOriginalFilename();
         System.out.println("Client File Name = " + name);
 
         if (name != null && name.length() > 0) {
            try {
               // Create the file at server
               File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);
 
               BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
               stream.write(fileData.getBytes());
               stream.close();
               //
               uploadedFiles.add(serverFile);
               System.out.println("Write file: " + serverFile);
            } catch (Exception e) {
               System.out.println("Error Write file: " + name);
               failedFiles.add(name);
            }
         }
      }
      model.addAttribute("description", description);
      model.addAttribute("uploadedFiles", uploadedFiles);
      model.addAttribute("failedFiles", failedFiles);
      return "uploadResult";
   }
 
}

uploadOneFile.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
 
   <head>
      <meta charset="UTF-8">
      <title>Upload One File</title>
   </head>
   
   <body>
      <th:block th:include="/_menu"></th:block>
       
      <h3>Upload One File:</h3>
       
      <!-- MyUploadForm -->
       
      <form th:object="${myUploadForm}" method="POST"
         action="" enctype="multipart/form-data">
         Description:
         <br>
         <input th:field="*{description}" style="width:300px;"/>                
         <br/><br/>  
         File to upload: <input th:field="*{fileDatas}" type="file"/>
         <br />  
         <input type="submit" value="Upload">
      </form>
       
   </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...