【发布时间】:2016-09-10 22:40:03
【问题描述】:
我正在尝试使用基于 Spring 的 REST 服务的 POC 来按照 tutorial 的教程上传文件。但是,当我尝试使用上传文件时,出现以下错误
错误 404:javax.servlet.UnavailableException:SRVE0319E:对于 [SpringRestWebservice] servlet, org.springframework.web.servlet.handler.DispatcherServletWebRequest 已找到 servlet 类,但资源注入失败 发生了。 java.lang.NoSuchMethodException: org.springframework.web.servlet.handler.DispatcherServletWebRequest.()
控制器代码如下
@RestController
@RequestMapping(value = "/restService")
// Max uploaded file size (here it is 20 MB)
@MultipartConfig(fileSizeThreshold = 20971520)
public class RestServiceController {
@RequestMapping(value = "/fileUpload")
public String uploadFile(@RequestParam("uploadedFile") MultipartFile uploadedFileRef){
System.out.println("Entering RestServiceController.uploadFile");
// Get name of uploaded file.
String fileName = uploadedFileRef.getOriginalFilename();
System.out.println("File to upload : " + fileName);
// Path where the uploaded file will be stored.
String path = "C:/SpringRestService/" + fileName;
// This buffer will store the data read from 'uploadedFileRef'
byte[] buffer = new byte[1000];
FileInputStream reader = null;
//FileOutputStream writer = null;
int totalBytes = 0;
try {
// Now create the output file on the server.
//File outputFile = new File(path);
//outputFile.createNewFile();
// Create the input stream to uploaded file to read data from it.
reader = (FileInputStream) uploadedFileRef.getInputStream();
// Create writer for 'outputFile' to write data read from
// 'uploadedFileRef'
//writer = new FileOutputStream(outputFile);
// Iteratively read data from 'uploadedFileRef' and write to
// 'outputFile';
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) != -1) {
//writer.write(buffer);
totalBytes += bytesRead;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
reader.close();
//writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Leaving RestServiceController.uploadFile");
return "File uploaded successfully! Total Bytes Read="+totalBytes;
}
}
web.xml sn-p
<servlet>
<servlet-name>SpringRestWebservice</servlet-name>
<servlet-class>org.springframework.web.servlet.handler.DispatcherServletWebRequest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRestWebservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
HTML 内容提交文件上传
<body>
<form method="POST" enctype="multipart/form-data"
action="http://localhost:9080/SpringRestWebservice/restService/fileUpload">
File to upload: <input type="file" name="uploadedFile"><br />
<input type="submit" value="Upload">
</form>
</body>
环境
Eclipse Neon Release (4.6.0)
Spring 4.2.5
WAS Liberty v16
不确定缺少什么?请帮忙
【问题讨论】:
标签: spring file rest model-view-controller upload