pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

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

包 org.sid.web;導入 java.io.BufferedOutputStream;導入java.io.File;導入 java.io.FileOutputStream;導入 java.io.IOException;導入 java.nio.file.Files;導入 java.nio.file.Path;導入 java.nio.file.Paths;導入 java.util.ArrayList;導入 java.util.List;導入 javax.servlet.ServletContext;導入 org.springframework.beans.factory.annotation.Autowired;導入 org.springframework.http.HttpEntity;導入 org.springframework.http.HttpHeaders;導入 org.springframework.http.HttpStatus;導入 org.springframework.http.MediaType;導入 org.springframework.http.ResponseEntity;導入 org.springframework.stereotype.Controller;導入 org.springframework.util.LinkedMultiValueMap;導入 org.springframework.web.bind.annotation.GetMapping;導入 org.springframework.web.bind.annotation.PostMapping;導入 org.springframework.web.bind.annotation.RequestMapping;導入 org.springframework.web.bind.annotation.RequestMethod;導入 org.springframework.web.bind.annotation.RequestParam;導入 org.springframework.web.bind.annotation.ResponseBody;導入 org.springframework.web.bind.annotation.RestController;導入 org.springframework.web.client.RestTemplate;導入 org.springframework.web.multipart.MultipartFile;導入 org.springframework.web.servlet.mvc.support.RedirectAttributes;導入 org.springframework.core.env.Environment;導入 org.springframework.core.io.ClassPathResource;導入 org.springframework.core.io.FileSystemResource;導入 org.sid.entities.FileInfo;@RestController公共類 UploadController {@自動連線ServletContext 上下文;@RequestMapping(value = "/fileupload/file", headers = ("content-type=multipart/*"), method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)公共響應實體<文件信息>上傳(@RequestParam(文件")MultipartFile inputFile){文件信息文件信息 = 新文件信息();HttpHeaders 標頭 = 新的 HttpHeaders();if (!inputFile.isEmpty()) {嘗試 {String originalFilename = inputFile.getOriginalFilename();文件目標文件 = 新文件(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);}}}

但是當在郵遞員中插入

我的示例代碼:

application.properties

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

主要應用類:

Application.java

import org.springframework.boot.SpringApplication;導入 org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication公共類應用程序{公共靜態無效主要(字符串[]參數){SpringApplication.run(Application.class, args);}}

Rest 控制器類:

import org.springframework.http.MediaType;導入 org.springframework.stereotype.Controller;導入 org.springframework.ui.Model;導入 org.springframework.web.bind.annotation.RequestBody;導入 org.springframework.web.bind.annotation.RequestMapping;導入 org.springframework.web.bind.annotation.RequestMethod;導入 org.springframework.web.bind.annotation.RequestParam;導入 org.springframework.web.bind.annotation.ResponseBody;導入 org.springframework.web.multipart.MultipartFile;@控制器@RequestMapping("/文件上傳")公共類 MyRestController {@RequestMapping(值 = "/file", 方法 = RequestMethod.POST, 產生 = 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>

//...

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
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 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 滁州高低温冲击试验箱厂家_安徽高低温试验箱价格|安徽希尔伯特 | 发光字|标识设计|标牌制作|精神堡垒 - 江苏苏通广告有限公司 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 自清洗过滤器_全自动过滤器_全自动反冲洗过滤器_量子过滤器-滑漮滴 | OLChemim试剂-ABsciex耗材-广州市自力色谱科仪有限公司 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 365文案网_全网创意文案句子素材站| 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 威客电竞(vk·game)·电子竞技赛事官网 | 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 注塑_注塑加工_注塑模具_塑胶模具_注塑加工厂家_深圳环科 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 碳刷_刷握_集电环_恒压簧_电刷厂家-上海丹臻机电科技有限公司 | 大连海岛旅游网>>大连旅游,大连海岛游,旅游景点攻略,海岛旅游官网 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 洁净实验室工程-成都手术室净化-无尘车间装修-四川华锐净化公司-洁净室专业厂家 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 济南宣传册设计-画册设计_济南莫都品牌设计公司 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | 小型气象站_车载气象站_便携气象站-山东风途物联网 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 |