久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

上傳文件springboot所需的請求部分“文件"不存

upload file springboot Required request part #39;file#39; is not present(上傳文件springboot所需的請求部分“文件不存在)
本文介紹了上傳文件springboot所需的請求部分“文件"不存在的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想在我的 Spring Boot 應(yīng)用程序中添加一個上傳功能;這是我上傳的 Rest Controller

包 org.sid.web;導(dǎo)入 java.io.BufferedOutputStream;導(dǎo)入java.io.File;導(dǎo)入 java.io.FileOutputStream;導(dǎo)入 java.io.IOException;導(dǎo)入 java.nio.file.Files;導(dǎo)入 java.nio.file.Path;導(dǎo)入 java.nio.file.Paths;導(dǎo)入 java.util.ArrayList;導(dǎo)入 java.util.List;導(dǎo)入 javax.servlet.ServletContext;導(dǎo)入 org.springframework.beans.factory.annotation.Autowired;導(dǎo)入 org.springframework.http.HttpEntity;導(dǎo)入 org.springframework.http.HttpHeaders;導(dǎo)入 org.springframework.http.HttpStatus;導(dǎo)入 org.springframework.http.MediaType;導(dǎo)入 org.springframework.http.ResponseEntity;導(dǎo)入 org.springframework.stereotype.Controller;導(dǎo)入 org.springframework.util.LinkedMultiValueMap;導(dǎo)入 org.springframework.web.bind.annotation.GetMapping;導(dǎo)入 org.springframework.web.bind.annotation.PostMapping;導(dǎo)入 org.springframework.web.bind.annotation.RequestMapping;導(dǎo)入 org.springframework.web.bind.annotation.RequestMethod;導(dǎo)入 org.springframework.web.bind.annotation.RequestParam;導(dǎo)入 org.springframework.web.bind.annotation.ResponseBody;導(dǎo)入 org.springframework.web.bind.annotation.RestController;導(dǎo)入 org.springframework.web.client.RestTemplate;導(dǎo)入 org.springframework.web.multipart.MultipartFile;導(dǎo)入 org.springframework.web.servlet.mvc.support.RedirectAttributes;導(dǎo)入 org.springframework.core.env.Environment;導(dǎo)入 org.springframework.core.io.ClassPathResource;導(dǎo)入 org.springframework.core.io.FileSystemResource;導(dǎo)入 org.sid.entities.FileInfo;@RestController公共類 UploadController {@自動連線ServletContext 上下文;@RequestMapping(value = "/fileupload/file", headers = ("content-type=multipart/*"), method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)公共響應(yīng)實體<文件信息>上傳(@RequestParam(文件")MultipartFile inputFile){文件信息文件信息 = 新文件信息();HttpHeaders 標(biāo)頭 = 新的 HttpHeaders();if (!inputFile.isEmpty()) {嘗試 {String originalFilename = inputFile.getOriginalFilename();文件目標(biāo)文件 = 新文件(context.getRealPath("C:/Users/kamel/workspace/credit_app/uploaded") + File.separator + originalFilename);inputFile.transferTo(destinationFile);fileInfo.setFileName(destinationFile.getPath());fileInfo.setFileSize(inputFile.getSize());headers.add("文件上傳成功-", originalFilename);return new ResponseEntity(fileInfo, headers, HttpStatus.OK);} 捕捉(異常 e){返回新的 ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);}} 別的 {返回新的 ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);}}}

但是當(dāng)在郵遞員中插入

我的示例代碼:

application.properties

#max 文件和請求大小spring.http.multipart.max-file-size=10MBspring.http.multipart.max-request-size=11MB

主要應(yīng)用類:

Application.java

import org.springframework.boot.SpringApplication;導(dǎo)入 org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication公共類應(yīng)用程序{公共靜態(tài)無效主要(字符串[]參數(shù)){SpringApplication.run(Application.class, args);}}

Rest 控制器類:

import org.springframework.http.MediaType;導(dǎo)入 org.springframework.stereotype.Controller;導(dǎo)入 org.springframework.ui.Model;導(dǎo)入 org.springframework.web.bind.annotation.RequestBody;導(dǎo)入 org.springframework.web.bind.annotation.RequestMapping;導(dǎo)入 org.springframework.web.bind.annotation.RequestMethod;導(dǎo)入 org.springframework.web.bind.annotation.RequestParam;導(dǎo)入 org.springframework.web.bind.annotation.ResponseBody;導(dǎo)入 org.springframework.web.multipart.MultipartFile;@控制器@RequestMapping("/文件上傳")公共類 MyRestController {@RequestMapping(值 = "/file", 方法 = RequestMethod.POST, 產(chǎn)生 = MediaType.APPLICATION_JSON_VALUE)public @ResponseBody String myService(@RequestParam("file") MultipartFile 文件,@RequestParam("id") String id) 拋出異常 {如果(!file.isEmpty()){//你的邏輯}返回一些json";}}

pom.xml

//...<父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><相對路徑/><!-- 從存儲庫中查找父級--></父母>……<依賴性><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></依賴>//...

I want to add an upload function to my spring boot application; this is my upload Rest Controller

package org.sid.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.sid.entities.FileInfo;

@RestController
public class UploadController {
  @Autowired
  ServletContext context;

  @RequestMapping(value = "/fileupload/file", headers = ("content-type=multipart/*"), method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResponseEntity<FileInfo> upload(@RequestParam("file") MultipartFile inputFile) {
    FileInfo fileInfo = new FileInfo();
    HttpHeaders headers = new HttpHeaders();
    if (!inputFile.isEmpty()) {
      try {
        String originalFilename = inputFile.getOriginalFilename();
        File destinationFile = new File(
            context.getRealPath("C:/Users/kamel/workspace/credit_app/uploaded") + File.separator + originalFilename);
        inputFile.transferTo(destinationFile);
        fileInfo.setFileName(destinationFile.getPath());
        fileInfo.setFileSize(inputFile.getSize());
        headers.add("File Uploaded Successfully - ", originalFilename);
        return new ResponseEntity<FileInfo>(fileInfo, headers, HttpStatus.OK);
      } catch (Exception e) {
        return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
      }
    } else {
      return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
    }
  }
}

but when testing this in postman with inserting http://localhost:8082/fileupload/file and adding a file to the body i got this error: "exception": org.springframework.web.multipart.support.MissingServletRequestPartException", "message": "Required request part 'file' is not present,

解決方案

This is how your request in Postman should look like:

My sample code:

application.properties

#max file and request size 
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=11MB

Main Application Class:

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Rest controller class:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


    @Controller
    @RequestMapping("/fileupload")
    public class MyRestController {

    @RequestMapping(value = "/file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        public @ResponseBody String myService(@RequestParam("file") MultipartFile file,
                @RequestParam("id") String id) throws Exception {

    if (!file.isEmpty()) { 

           //your logic
                        }
return "some json";

                }
    }

pom.xml

//...

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

....



<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

//...

這篇關(guān)于上傳文件springboot所需的請求部分“文件"不存在的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 不卡在线视频 | 亚洲97 | 精品婷婷| 国产免费va | 91精品国产综合久久精品 | 精品国产乱码久久久久久闺蜜 | 蜜臀网| 精品一区电影 | 99在线免费观看 | www.久久 | 久久综合九色综合欧美狠狠 | 国产成人91视频 | 欧美激情视频一区二区三区在线播放 | 粉嫩国产精品一区二区在线观看 | 成人在线精品视频 | 国产精品18久久久久久白浆动漫 | 欧美在线| 欧美一区二区在线观看 | 日韩精品免费 | 一级全黄少妇性色生活免费看 | 黑人巨大精品欧美一区二区一视频 | 免费黄网站在线观看 | 精品欧美视频 | 日日干日日操 | 国产婷婷精品av在线 | 国产美女黄色片 | 中文字幕欧美在线观看 | 综合久久av| 国产精品国产精品国产专区不片 | 岛国av一区二区 | 亚洲精品久久久一区二区三区 | 久久久久久久综合 | 国产视频观看 | 亚洲高清免费观看 | 午夜免费视频观看 | 欧美午夜激情在线 | 欧美精品一区三区 | 欧美一区视频 | 国产精品久久777777 | 国产美女视频黄 | 国产日韩欧美一区 |