我想上传带有restservice的文件。我在网上发现了很多,但仍无法解决我的问题。也许是因为我在休息服务中使用Spring?! 这是我的代码:
HTML
if (!!Meteor.user())
JQUERY
<input type="button" value="Upload document" id="button">
<input id="fileToUpload" type="file">
JAVA(RESTSERVICE)
jQuery("document").ready(function () {
$("#button").on("click", function () {
var fileInput= $('input[name="fileInput"]')[0].files[0];
var data = new FormData();
data.append('file', fileInput);
$.ajax({
url: '.../upload',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert("successfully.");
},
error: function(data){
alert("failed.");
}
});
});
});
异常
@RequestMapping(value = "/upload", method = { RequestMethod.POST }, produces = { MediaType.MULTIPART_FORM_DATA_VALUE,
MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.APPLICATION_JSON_VALUE })
public void uploadingFile(@RequestBody InputStream uploadedInputStream) throws IOException {
System.out.println("uploadedInputStream: " + uploadedInputStream);
}
我的休息服务好吗?找不到我的错误:(
的pom.xml
Servlet.service() for servlet REST API Dispatcher threw exception: java.lang.IllegalStateException: Parameters processing failed.
APPLICATION-context.xml中
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
JAVA(RESTSERVICE)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000" />
</bean>
JAVA(SPRING CONFIG)
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadingFile(@RequestParam("file") MultipartFile uploadedInputStream) throws IOException {
System.out.println("...the parameter binding works now");
}
(替代 - APPLICATION.XML)
@Bean
public static CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver cmr = new CommonsMultipartResolver();
cmr.setMaxUploadSize(50000000);
return cmr;
}
答案 0 :(得分:1)
您的处理程序方法应如下所示:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadingFile(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next());
System.out.println(file.getOriginalFilename() +" uploaded!");
}
不要忘记在应用程序上下文中添加以下行:
<mvc:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"/>
</bean>