文件上传API失败,从angularjs后调用。以下是文件上传API代码, FileUploadController.java弹出文件上传失败的'访问控制允许来源'标题出现在请求的资源
@RestController
@RequestMapping(value="/v1")
public class FileUploadController {
@Autowired
private ServletContext context;
@RequestMapping(value = "/fileUpload", headers = "content-type=multipart/*", method = RequestMethod.POST)
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("/WEB-INF/images") + File.separator + originalFileName);
inputFile.transferTo(destinationFile);
fileInfo.setFileName(destinationFile.getPath());
fileInfo.setFileSize(inputFile.getSize());
headers.add("FileUploaded Successfully - ", originalFileName);
return new ResponseEntity<FileInfo>(fileInfo, headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST);
}
}
SimpleCorsFilter.java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class SimpleCorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, X-Auth-Token, Content-Type");
response.setHeader("Access-Control-Allow-Credentials","false");
chain.doFilter(req, res);
}
}
堆栈跟踪:
SEVERE [http-nio-8080-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
Angularjs测试代码:
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);
myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ');
console.dir(file);
var uploadUrl = "http://xxx/v1/fileUpload";
//var uploadUrl = "http://localhost:8080/xxx/v1/fileUpload";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
问题: 请求的资源上是否存在“Access-Control-Allow-Origin”标头。因此不允许访问原产地'http://localhost'。响应有HTTP状态代码500。
请帮我解决这个问题,
您正在使用哪个版本的angularjs? –
Angular 1.1.1我正在使用。 – user3113732
你可以发布你的整个'SimpleCorsFilter.java'代码。我认为你的春天应用程序无法扫描你的过滤器 –