transferto 文件不存在_上传文件springboot所需的请求部分“文件”不存在

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 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, headers, HttpStatus.OK);

} catch (Exception e) {

return new ResponseEntity(HttpStatus.BAD_REQUEST);

}

} else {

return new ResponseEntity(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

//...

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

....

org.springframework.boot

spring-boot-starter-web-services

//...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值