gpt4 book ai didi

spring - Spring MVC REST 的文件上传问题

转载 作者:行者123 更新时间:2023-12-04 07:14:40 26 4
gpt4 key购买 nike

我有基于 Spring MVC 的 REST 应用程序。正在尝试上传文件。这是代码片段。

webmvc-config.xml

<bean
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver">
<property name="maxUploadSize" value="2000000" />
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0" />
<property name="defaultContentType">
<ref bean="htmlMediaType" />
</property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="file" value="multipart/form-data"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.castor.CastorMarshaller" />
</property>
</bean>
</list>
</property>

FileUploadController.java

@Controller
@RequestMapping("/appfiles")
public class FileUploadController {

private String saveDirectory = "C:/Downloads/";
@RequestMapping(value = "/appfiles", method = RequestMethod.POST, consumes="multipart/form-data", produces="application/json")
public ResponseEntity<List<Map<String, String>>> save(
@RequestParam("file") MultipartFile file) {
System.out.println("received file with original filename: " + file.getOriginalFilename());

// List<MultipartFile> files = uploadForm.getFiles();
List<Map<String, String>> response = new ArrayList<Map<String, String>>();
Map<String, String> responseMap = new HashMap<String, String>();

List<String> fileNames = new ArrayList<String>();

if (null != file) {
// for (MultipartFile multipartFile : files) {

String fileName = file.getOriginalFilename();
fileNames.add(fileName);

try {
file.transferTo(new File(saveDirectory
+ file.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
responseMap.put("displayText", file.getOriginalFilename());
responseMap.put("fileSize", ""+file.getSize());
response.add(responseMap);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json");
return new ResponseEntity<List<Map<String, String>>>(response, httpHeaders, HttpStatus.OK);
}
}

当我使用 chrome 扩展程序“postman”时出现以下错误

Could not parse multipart servlet request; nested exception is     org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1020)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:883)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

postname 提交使用以下详细信息完成:

Headers
conent-type: multipart/form-data
Form-data:
file=<filename.txt>
Method=POST

你能告诉我我的代码有什么问题吗?

感谢和问候

最佳答案

我也遇到了同样的问题,但是当我在没有任何 Header 值的情况下尝试它时它工作正常。

关于spring - Spring MVC REST 的文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com